Trying to use EEPROM to save data on power down. Floats Vs Ints?

Hi - hopefully some cleaver spark can throw some ideas my way.

I have been playing with a code for a bicycle speed'o'meter. I also wanted to add the function that when power is removed it saves the total mileage.

I am using a 42V battery and a converter to 5V for arduino. I use a voltage sense to trigger the EEPROM saves and this seems to be triggering just fine.

It seems to work, but when my mileage gets to about 2.5 miles, the recalled mileage on startup no longer adds up as expected.

I've tried converting my floating numbers to integers etc, but it still is unreliable.

While I could post the code here, it's massive and a mess at the moment. Can anyone offer any of the shelf tips, or a known working mile or km readout that saves total miles on power down.

Kind regards :slight_smile:

Are you using EEPROM.write() and read() which save and read one byte or EEPROM.put() and get() which can save any size of variable ?

Post a small but complete version of the program which shows the problem.

when power is removed it saves the total mileage.

How do you plan to write the mileage when power is removed, and by extension you don't have power?

Also mind the 100,000 write limit of the EEPROM (that's the guaranteed number; in practice you may reach a million or two but that's about it). Don't write too often.

I would only count turns of the wheel. You'll get an error margin when you multiply by circumference but it will be small.

Yes use integers. Use unsigned longs and make your units millimeters or smaller ( mm is meter to 3 places, get it?) since UL's can count 0 to 4294967295. That's good to 4294.967295 kilometers, I don't think you will ride that far in a week.

You could measure the wheel circumference to tenths of an inch (2.54 mm) and be good to 677867.312973 miles and still so far using integer math and not losing a digit.

A hard roller, very thin and light that your bike wheel turns will not have as much error. The bicycle wheel may have less pressure and not go as far in one turn, a hard roller will get exactly how much tread moved on the ground regardless.

A hard roller with one magnet and sensor will be sensed many times in a turn of the bicycle wheel, speed approximations will be closer to true when speed changes.