Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Pages: [1]   Go Down
Author Topic: Can't save data in Arduino EEPROM  (Read 1931 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi.

Can't save data in my Arduino EEPROM. I've try with a MEGA 1280 and a Duemilanove: same results
I went back to basics (EEPROM READ - EEPROM WRITE)  and can't obtain results:
even with the example (loop write - read)
it seems work, but when i remove the write command, the result of my reading is 256 for every data.

Here is my code:
Code:

#include <EEPROM.h>
int a = 0;
int value;
int i;


void setup()
{
Serial.begin(9600);
for ( i = 0; i < 256; i++)     // 256  because it holds 1 byte only
{
   EEPROM.write(i, i);   // Write 0 in position 0, 1 in position 1 and so on
   delay(5);
   value = EEPROM.read(i);   //read position
  Serial.print(i);
  Serial.print("\t");
  Serial.print(value);
  Serial.println();
  delay(50);
// Result: serial monitor should appear : 0   0 / next line 1  1 ... /next line 256  256
}

}
void loop()
{
 

}
I hope anyone can helpme !!!  smiley-cry
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 234
Posts: 14296
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

HI Gabo,

(right forum this time smiley-wink

The problem might be that EEPROM.write() uses two parameters and the value parameter should be a byte. - See http://www.arduino.cc/en/Reference/EEPROMWrite

You might cast the value like below.

Code:

#include <EEPROM.h>

int value;

void setup()
{
Serial.begin(9600);

for (int i = 0; i < 256; i++)     // 256  because it holds 1 byte only (BUT STILL IS AN INT!!)
{
   EEPROM.write(i, [color=red](byte)[/color] i);   // Write 0 in position 0, 1 in position 1 and so on
}

delay(100);

for (int i = 0; i < 256; i++)     // 256  because it holds 1 byte only
{
  value = EEPROM.read(i);      //read position
  Serial.print(i);
  Serial.print("\t ");
  Serial.println(value);
 }
}
void loop() {}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

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

Look at the EEPROM::write() function. Notice, in particular, what the type of the 2nd argument is. Then, compare that to the types of the arguments that you provide.

If you were to call the function with the correct type of argument, you would get correct results.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 213
Posts: 9060
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your sketch works fine for me on a Diecimila.  I upload the sketch, run it, see the expected output, comment out the write line, re-upload it, re-run it and see the same output.

Are you using something other then the IDE and USB port to upload your sketch?  Perhaps and Flash and EEPROM are being cleared as part of the upload.
« Last Edit: March 21, 2011, 01:30:26 pm by johnwasser » Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 318
Posts: 26927
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but when i remove the write command, the result of my reading is 256 for every data.
That's pretty remarkable.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm using the sparfun's AVR pocket programming...

I'll try using the USB.
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 362
Posts: 17320
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Look at the EEPROM::write() function. Notice, in particular, what the type of the 2nd argument is. Then, compare that to the types of the arguments that you provide.

If you were to call the function with the correct type of argument, you would get correct results.

If that is the root of the problem, then part of the blame might be the example sketch used in the Arduino reference section for EEPROM:

http://arduino.cc/en/Reference/EEPROMWrite

Code:
Example
#include <EEPROM.h>

void setup()
{
  for (int i = 0; i < 512; i++)
    EEPROM.write(i, i);
}

void loop()
{
}


And I ran the OP's sketch and it seemed to work fine writing to eeprom, and later after commenting out the writting to eeprom, still held and printed valid data from the eeprom.

Lefty
« Last Edit: March 21, 2011, 02:45:27 pm by retrolefty » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I upload the sketch using the bootloader instead the Pocket programmer and it works fine.

then, the pocket programmer clears the IC and rewrites it????

The example works fine but it's incorrect.
Then I have to tear apart the value (integer)  in two bytes to save... and when I need to retrieve the value I need to call both bytes and "paste them" ???

How to do that ????
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 213
Posts: 9060
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Write 16 bit integer 'value' into i and i+1:

  EEPROM.write(i, value>>smiley-cool;   // Write high byte
  EEPROM.write(i+1, value);  // Write low byte


Read 16-bit integer 'value' from i and i+1:

value = (EEPROM.read(i) << smiley-cool | EEPROM.read(i+1);
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 318
Posts: 26927
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://arduino.cc/en/Reference/HighByte
http://arduino.cc/en/Reference/LowByte
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Jump to: