Hi,
Im building a kilometer reader based on a wheel sensor which is hall effect. Im reading the signal not straight out of the sensor, but after he came out a several process units (based on the schematic of the car). The arduino save the kilometer reading to the eeprom and when it wakes up, it read the value back from the eeprom. when im connecting my laptop to read the value of the kilometer from the eeprom (when the car is parked) it shows me a higher reading the what the actual travel distance was.
I would be glad to get your advice if there's something wrong with the code.
this my first project with arduino so please forgive my newbie mistakes.
attached is the code:
#include <EEPROMEx.h>
volatile byte pulse;
float Diameter =0.66;
float WHLdistance=0; // count the wheel distance to 1 KM
//int SQRout = 11; //assign pin 11 to be the output of the square wave for the interupt
byte PowerOnOff = 7; // detects when the switch power is off.
byte eepromDISPLAY = 5;// pin for detecting button pressed
boolean eepromPressed = false;// storing the reading from the button
boolean lasteeprom = false ; // storing the previuos value of the pressed button
volatile long KM;
int oldPulse = 0;
byte oldhundredm;
void setup() {
pinMode (eepromDISPLAY, INPUT);
digitalWrite(eepromDISPLAY, HIGH);// the buttom short to ground
pinMode (PowerOnOff, INPUT);
digitalWrite(PowerOnOff, HIGH);// high so the arduino will stay on when nothing's connected
digitalWrite (2,HIGH); //puts the interrupts pin in high impadence
//pinMode (SQRout, OUTPUT);
Serial.begin(115200);
attachInterrupt(0, rpm_fun, RISING);// increase pulse count
//attachInterrupt(1,WakeUP,RISING); //wakes up the arduino
pulse = 0;
KM = EEPROM.readLong(0); // need to read from the eeprom the value of KM
} //end setup
void loop()
{
//checks the EEPROM button to diaplay current KM on screen.
boolean eeprom = debounce (eepromPressed);
if (eeprom == false && lasteeprom == true){
DiaplsyEEPROMval();
}//end EEPROM if
lasteeprom = eeprom;
//this condition checks if the switch power is off
// if off it saves the KM to the eeprom.
if (!(digitalRead (PowerOnOff))){
EEPROM.writeLong(0,KM);
}
/*digitalWrite (SQRout,HIGH);
delay(10);
digitalWrite (SQRout,LOW);
delay (10);*/
byte hundredM = (WHLdistance / 100);
if (pulse >= 4 ) {
//Update WHLdistance count every 4 pulses.
WHLdistance = WHLdistance + Diameter*PI;
Serial.print (" Wheel Distance:");
Serial.println ( WHLdistance);
pulse = 0;
if (hundredM !=oldhundredm) { // calculate the distance to diaplay 100 meters incarments.
Serial.print ("small trip ");
Serial.println (hundredM);
oldhundredm = hundredM;
} //end display 100 meter
}//end of pulse if
if (WHLdistance >1000) {
WHLdistance =0;
Serial.print ("zeroing WHLdistance");
Serial.println (WHLdistance);
KM++;// increase KM count
Serial.print(" Kilometer:");
Serial.println(KM,DEC);
}//end of kilometer if
}//end loop
void rpm_fun()
{
oldPulse = pulse;
pulse++;
//Each rotation, this interrupt function runs 4 times
}// end rpm_fun
void DiaplsyEEPROMval()
{
Serial.print (" Current kilomtrage is: ");
Serial.println (EEPROM.readLong(0));
}//end DiaplsyEEPROMval
boolean debounce (boolean eeprom)
{
boolean current = digitalRead (eepromDISPLAY);
if (eeprom != current){
delay (5);
current = digitalRead (eepromDISPLAY);
}
return current;
}