## Saturday, 21 April 2012

### Week 13

It is only 1 week to go!. I am very worried. This week, i focused with the circuit and programming.For programming, i start to write a program to display the RPM and speed. Below is the coding for the programming:

/*

The circuit:
* LCD RS pin to digital pin 7
* LCD Enable pin to digital pin 8
* LCD D4 pin to digital pin 9
* LCD D5 pin to digital pin 10
* LCD D6 pin to digital pin 11
* LCD D7 pin to digital pin 12
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)

*/

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

int ledPin = 13;               // choose pin for the LED
float start, finished;
float elapsed, time;
float circMetric=0.04; // wheel circumference relative to sensor position (in meters)
float circImperial; // using 1 kilometer = 0.621371192 miles
float speedk, speedm;    // holds calculated speed vales in metric and imperial

void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
pinMode(2,INPUT);
pinMode(ledPin, OUTPUT);      // declare LED as output
attachInterrupt(0, speedCalc, HIGH); // interrupt called when sensors sends digital 2 high (every wheel rotation)
start=millis();
circImperial=circMetric*.62137; // convert metric to imperial for MPH calculations
// Print a message to the LCD.
lcd.print("    Welcome   ");
lcd.setCursor(0, 1);
lcd.print("  Mc-Ergocycle  ");
}

void speedCalc()
{
elapsed=millis()-start;
start=millis();
speedk=(3600*circMetric)/elapsed; // km/h
speedm=(3600*circImperial)/elapsed; // Miles per hour
}

void loop() {

if ( digitalRead(2)== HIGH)             // == digunakan bagi declare condition
{  digitalWrite(ledPin, HIGH);  // turn LED OFF
} else {
digitalWrite(ledPin, LOW); // turn LED ON
}

delay (1000);
Serial.println(elapsed);
Serial.println(speedk);
Serial.println(speedm);
Serial.println();
lcd.setCursor(0,0);
lcd.print(int(speedk));
lcd.print(" km/h ");
lcd.print(int(speedm));
lcd.print(" MPH   ");
lcd.setCursor(0,1);
lcd.print(int(elapsed));
lcd.print(" ms/rev      ");
delay(1000); //
}

Then, i uploading to Atmega328P-PU through Arduino board. At first, the LCD does not display anything and just light on. I troubleshoot the circuit and found that pin 7 on Atmega which is connect to 5Volt (VCC)  pin must be jumped straight to the Vout of Voltage Regulator. The result is as follow:

 Display result

Then, i make some modification on the bicycle which is change the colour from red to yellow. I spray it by using the paint spray that i have bought at Careffour Wangsa maju.

 Paint Spray

Then, i attached the display circuit to the bicycle and Below is the Video of the finiched cycle ergometer. This video has showed the cycle ergometer operation:

The demo and presentation of Final Year Project is on 24 April 2012 (next week).I make some preparation for next week by design the poster and troubleshoot the circuit.