EEPROM read and write

My pinball project is almost completed now. At least when it comes to wiring and setting up all the hardware. One of the things that remain is programming the code for the Highscore tables.

The 8 highscores will be stored here:

unsigned long int hiscore[8];

On startup, I want load the highscores from the EEPROM area into the hiscore RAM array with a function named "void loadhiscores()".
And when the highscore table changes, a function named "void updatehighscores()" is called to write the new table into the EEPROM.

But EEPROM.read and EEPROM.write only handles single bytes and not long int's, and the information on Arduino Playground - EEPROMWriteAnything is way too advanced for me to understand and apply on my problem. Would anybody like to help me write the code to these two small functions? I would be very thankful, and of course give proper credits when I present my Pinball Project. (I know people are asked to attach the whole sketch and not just a snippet, but so far I have not written any high score related code and I believe I supplied enough information. Btw, the sketch for the Playfield Unit is already 17kb, and of no help for this question).

TIA,
SimLego

An unsigned long is 4 bytes.
Mask each out byte and write them to the EEPROM that way.

That library is fairly easy to use.

unsigned long int hiscore[8];

//Write
for( int index = 0 ; index < 8 ; ++index ){
  EEPROM_writeAnything( index * sizeof( unsigned long int ), hiscore[ index ] );
}

//Read
for( int index = 0 ; index < 8 ; ++index ){
  EEPROM_readAnything( index * sizeof( unsigned long int ), hiscore[ index ] );
}

Edit: you could extend the life of your eeprom when writing by using the method below:

for( int index = 0 ; index < 8 ; ++index ){

  unsigned long int temp;
  EEPROM_readAnything( index * sizeof( unsigned long int ), temp );

  if( temp != hiscore[ index ] ){

    EEPROM_writeAnything( index * sizeof( unsigned long int ), hiscore[ index ] );
  }
}

Thank you, both of you!

you could extend the life of your eeprom when writing by using the method below:

And when the highscore table changes, a function named "void updatehighscores()" is called to write the new table into the EEPROM.

It won't overwrite the list with an identical one at every Game Over. :slight_smile:

Oh, sorry. Now I understand. If you enter the third place on the list, there is no point in overwriting the first two entries.

Pardon me.

I got it working now. Thanks again!

hi, sorry but gotta bump it, in the examples you have given,

i have looked at it and tried to break it down but what do i need to change to be able to use this within my sketch, i have an a 4 digit number or 2 bytes to read and write, looking at your code

unsigned long int lastH;
unsigned long int lastD


//Write lastH
for( int index = 0 ; index < 1 ; ++index ){ //this would write to start of eeprom
  EEPROM_writeAnything( index * sizeof( unsigned long int ), lastH[ index ] );
}
//write lastD
for( int index = 1 ; index < 2 ; ++index ){ //this would write to start of eeprom
  EEPROM_writeAnything( index * sizeof( unsigned long int ), lastD[ index ] );
}

//Read lastH
for( int index = 0 ; index < 1 ; ++index ){
  EEPROM_readAnything( index * sizeof( unsigned long int ), lastH[ index ] );

//Read lastD
for( int index = 1 ; index < 2 ; ++index ){
  EEPROM_readAnything( index * sizeof( unsigned long int ), lastD[ index ] );
}

if i had 2 bytes written so 4 numbers in total and i wanted to read a byte at a time, would this be correct?

if not, can you please advise on what im not getting?

thanks i hope the pinball table is working well :slight_smile:

i have looked at it and tried to break it down but what do i need to change to be able to use this within my sketch, i have an a 4 digit number or 2 bytes to read and write,

What is the relevance of the 4 digits? How do you know that the value has 4 digits? Rather than 3 or 5 or 6.

Post YOUR code!

It's silly to use a for loop to execute a block of code exactly once.

how rude are you? read my post, the code is there? i merely changed the examples previous as i haven't implemented it in my code yet. seems pointless to post a 56k sketch which has zero relevance. the 4 digit number you seemed to have trouble understanding will only be a 4 digit number as set by the user.

the read function would be used once, and if the code i have presented works then i can change it, like i already said, i have used the examples as previously shown.

the write function would happen once every so often as set by user. if the code from the example code works then i can change the format to match the usage.

as for your attitude, i expect an apology. your post was unhelpful, rude, offensive and just posed no useful reason for saying it?

as for your attitude, i expect an apology

Don't hold your breath. You hijacked another thread, posted an incomplete question, some out of context snippets of code that a 4th grader would have recognized as silly, and demanded an answer. You are the one that needs to apologize.

phillmybuttons:
if you can offer constructive advice then please do.

Certainly.

PaulS has helped more people than probably most of us combined. So my constructive advice would be to grow thicker skin and take his advice/answer his questions.