Saving to EEPROM - Strange behaviour

Hello everyone

This issue is driving me up the wall. I've spent countless hours researching the issues and I've changed my code more times than I can remember. Any help would be greatly appreciated.

Scenario:

I have created a speedo for my classic mini. The arduino uses a GPS module for speed detection and it drives a stepper motor to move the speedo needle. The distance travelled is stored in variables and written to EEPROM at power down.

How it works:

GPS speed is converted into a stepper position. Count of distance travelled updates 7 variables. From 1/10ths of a mile up to hundred thousands of miles. I have an external circuit using a supercap to hold the power on long enough to update the eeprom. This is triggered by monitoring a digital input for a voltage drop.

The Issue:

I am seeing random values being written to eeprom when I write the contents of the variables to EEPROM. For example, I would expect 0000010 to be written representing 1 mile travelled however when I power back up I get something like 0080010. Subsequent power cycles might write different values again. If I force the write to eeprom to 1111111 by hard coding those numbers into the write function it will work every time.

I’ve posted all of my code as it’s quite long and I thought it would better illustrate what I am doing. I still have some code in to use an interrupt. I’m not using that method now as I read that using an interrupt to trigger the write to EEPROM conflicts with the interrupt used by the EEPROM.

Please forgive any errors I might have created. I am not a developer and I’ve been self-teaching how to do this stuff for last 3-4 years.

Many thanks

Luke.

Try to trim your code down to the shortest example which will compile and demonstrate the problem. 80% of the time that will show you the answer and you don't need to post the code here.

You will get more and better answers if you can post the code inline instead of attaching it.

Are you sure that it only attempts to write once during the power-down sequence? Writing to EEPROM takes a burst of power and maybe it's writing continuously while input voltage is dropping until it gets caught halfway through a write?

EEPROM doesn't use interrupts as far as I know. You can re-enable interrupts while you are in an interrupt handler. It's just not normally done because if you're not careful, you can get interrupted by the same interrupt again and that will tie your code up in knots in a microsecond.

@MorganS

Thanks for your reply. I will give that a go and report back.

I am also thinking of storing the value in one variable and storing that in an array to see if that speeds up the write to EEPROM.

I've made significant progress in solving the issue.

I think @MorganS was correct in saying the power was dropping whilst it was writing to eeprom. I've changed the code so I write to less memory location during a power down and it seems to work. I now write to 2 memory locations rather than 7.

I know I will get a power failure, what I do is write every few seconds with the update command. I then spread this out over several groups. To do that add one byte to the data and increment it every write. I actually use Unix Time instead of a counter. I checksum the data and add it to the data that is put into eeprom. I use 16 locations, it is easy to mask that way.
Good Luck & Have Fun!
Gil