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.

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..

If you mask a value with another value, the only bits that will remain are those that are true (1) in both values.

So:unsigned long val = 0x3456;

unsigned long mask = 0xF000;

unsigned long masked = val & mask;

will result in masked being 0x3000. Now, if you shift ( >> ) this 8 times to the right, you get 0x0300. If you shifted 16 times, instead, you'd get 0x0030. If you shifted 24 times, instead, you'd get 0x0003.

If you store this last value in a byte, you get the same value as the 1st byte of the long.

If you use a mask of 0x0F00 and shift 16 times, you get the 2nd byte of the long.

If you use a mask of 0x00F0 and shift 8 times, you get the 3rd byte of the long.

If you use a mask of 0x000F, you get the 4th byte of the long.

Putting the long back together is just as easy. Simply shift in the other direction ( << ).

Search for VirtualWire posts, where people want to send longs using virtual wire.