Pages: [1]   Go Down
Author Topic: EEPROM read and write  (Read 849 times)
0 Members and 1 Guest are viewing this topic.
Sweden
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Be patient, I'm a total newbie.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
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 http://playground.arduino.cc/Code/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
« Last Edit: April 15, 2013, 11:00:28 am by SimLego » Logged

Offline Offline
Edison Member
*
Karma: 64
Posts: 2465
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

North Queensland, Australia
Online Online
Edison Member
*
Karma: 66
Posts: 2113
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

That library is fairly easy to use.

Code:
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:
Code:
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 ] );
  }
}
« Last Edit: April 15, 2013, 10:55:14 am by pYro_65 » Logged


Sweden
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Be patient, I'm a total newbie.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you, both of you!

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

Quote
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. smiley
« Last Edit: April 15, 2013, 11:05:48 am by SimLego » Logged

Sweden
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Be patient, I'm a total newbie.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Sweden
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Be patient, I'm a total newbie.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I got it working now. Thanks again!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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 smiley
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48566
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48566
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: