Go Down

Topic: HELP me saving the odometer reading into EEPROM (Read 1 time) previous topic - next topic

Joy

Mar 06, 2012, 10:36 am Last Edit: Mar 06, 2012, 01:15 pm by Joy Reason: 1
This is the odometer code
Code: [Select]
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 9, 8);

const int odometerPin = 3;
const int odometerInterrupt = 1;

const float wheelCircumference = 0.002035;  // wheel circumference in km

volatile unsigned long wheelRevs;       // total wheel revolutions counted, will overflow after 85 million km
unsigned long lastDisplayedMs = 0UL;    // time when we last refreshed the display


void setup()
{
 // Set up the odometer sensor pin and interrupt
 pinMode(odometerPin, INPUT);
 attachInterrupt(odometerInterrupt, odometerIsr, FALLING);  
}

void odometerIsr()
{
 ++wheelRevs;
}

void loop()
{
 // Capture the wheel revs with interrupts disabled in case it changes while we read it
 noInterrupts();
 unsigned long savedWheelRevs = wheelRevs;
 interrupts();
 
  unsigned long now = millis();
   if (now - lastDisplayedMs >= 200)    // update display every 200ms
   {
     lastDisplayedMs = now;
     float km = wheelCircumference * savedWheelRevs;
     lcd.setCursor(7, 0);
     lcd.print(km, 1);
     lcd.setCursor(14, 0);
     lcd.print("Km");
     
   }
  }



I would like to save the wheelRevs every time the power goes off..

I am only bothered as the wheelRevs will gradually keep increasing the more I ride the vehicle, so how will I save it into the eeprom...??

for example 491 wheelRevs = 1 km

so if I have driven 10,000 kms that is 490000 wheelRevs.
How doI save such a big number into the EEPROM...??

moreover I will be having additional 4 A, B, C, D Trip meters.. So I will have to save wheelRevs for them too... :(

How..??

PaulS

Quote
I would like to save the wheelRevs every time the power goes off..

How will you detect that power is going to go away, prior to it going away and terminating the sketch?

wheelRevs is an unsigned long. It is easy enough to extract the 4 bytes that make up that long, with bit shifting and masking or using a union. Once you have the 4 bytes, store them in 4 addresses in the EEPROM.

On start up, read the 4 addresses, and reassemble the unsigned long, again with bit shifting and masking or a union.

One thing you could do is to measure the time between ISR calls. When the time exceeds some value, the vehicle is traveling very slowly/stopped. Save the data then. If the power is lost, you have saved data. If the vehicle starts moving again, you've wasted a write cycle, but that might be OK.

Quote
moreover I will be having additional 4 A, B, C, D Trip meters.. So I will have to save wheelRevs for them too...

You save these exactly the same way. Setting a trip meter to 0 simply means recording the number of wheel revs at that time. Each time you want to display that trip distance, subtract the starting wheelRevs value from the current wheelRevs value to get revs since reset, then multiply by circumference to get distance.

These are even easier to deal with as they only need to be saved when the set/reset button is pressed, not on power down.

Joy

Quote
How will you detect that power is going to go away, prior to it going away and terminating the sketch?

ohhh..
thats not a big deal..
I have the A0 of the atmega measuring the main voltage.. and a big capacitor is also connected in parallel to the main supply..
So when I will detect that the A0 has gone below a certain value the code will write the current wheelRevs into the EEPROM with the existing power stored in the capacitor..


Hummm now the write to EEPROM part..

will you please show me with an example please..??
I am not able to understand much from the bitshift reference page.. :(

AWOL

Quote
I am not able to understand much from the bitshift reference page

So try a union.
Or try this
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Joy


Quote
I am not able to understand much from the bitshift reference page

So try a union.
Or try this


:) I just went through that page and it is possible to write anything to the EEPROM..

but at the same time I do not want to run away from learning about bitshift..
:P
Please teach me about this bitshift as how to use it to write such big nos. to EEPROM...??

Go Up