Problem writing and reading to/from EEPROM

Hi to everyone, i'm having problems when i try to write and read from the eeprom (Arduino nano)
I write 8 bytes, but when I read it, I see different values and don't know why. I'm trying to store an unsigned long splitted in 4 bytes, and 2 ints, divided in 2 bytes each.

unsigned long codeValue;
int codeLen;
int codeType;

unsigned long codeByte4=0, codeByte3=0, codeByte2=0, codeByte1=0, lenByte1=0, lenByte2=0, typeByte1=0, typeByte2=0;

void saveEeprom()
{
  int posicion=(datoTv-1)*8;

  codeByte4=codeValue>>24 & 0xFF;
  codeByte3=codeValue>>16 & 0xFF;
  codeByte2=codeValue>>8  & 0xFF;
  codeByte1=codeValue>>0  & 0xFF; //menos significativo
  EEPROM.write(codeByte4, posicion);
  EEPROM.write(codeByte3, posicion+1);
  EEPROM.write(codeByte2, posicion+2);
  EEPROM.write(codeByte1, posicion+3);

  typeByte2=highByte(codeType);
  typeByte1=lowByte(codeType);
  EEPROM.write(typeByte2, posicion+4);
  EEPROM.write(typeByte1, posicion+5);  

  lenByte2=highByte(codeLen);
  lenByte1=lowByte(codeLen);
  EEPROM.write(lenByte2, posicion+6);
  EEPROM.write(lenByte1, posicion+7);
  printValues();

  posicion=(datoTv-1)*8;
  codeByte4 = EEPROM.read(posicion);
  codeByte3 = EEPROM.read(posicion+1);
  codeByte2 = EEPROM.read(posicion+2);
  codeByte1 = EEPROM.read(posicion+3);

  typeByte2 = EEPROM.read(posicion+4);
  typeByte1 = EEPROM.read(posicion+5);

  lenByte2 = EEPROM.read(posicion+6);
  lenByte1 = EEPROM.read(posicion+7);
  printValues();

}

I save this:

B4, B3, B2, B1 = 0, 0, 0, 11
T2, T1 = 0, 3
L2, L1 = 0, 12

But I read This:
B4, B3, B2, B1 = 6, FF, FF, 5
T2, T1 = 255, 0
L2, L1 = 255, 255

What I'm doing wrong??

Thank you very much.

This is much easier: http://playground.arduino.cc/Code/EEPROMWriteAnything

  EEPROM.write(codeByte4, posicion);

Why is codeByte4 a long? Perhaps you should be using names where the type in the name ACTUALLY matches the type, since you can’t seem to remember on your own what the type is. Or pass the right type to functions.

To read and write values longer than one byte, simply use EEPROM.get() and EEPROM.put(). No additional libraries are needed!

See the examples that come with the IDE, or view them here:

https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/libraries/EEPROM/examples/eeprom_get/eeprom_get.ino

https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/libraries/EEPROM/examples/eeprom_get/eeprom_put.ino

Ah I see they merged the library I mentioned into the EEPROM class. That is in newer versions of the IDE.

Thanks to everyone for answering. I know there are other methods, but I want to know why this is not working.

I reduced it to the simplest thing I can imagine, and still work wrong, or maybe I'm crazy!

I'm doing this:

void setup()
{
  Serial.begin(9600);
  Serial.println("HI");
  byte a = 80;
  byte b = 100;
  byte address = 20;
  EEPROM.write(a, address);
  b = EEPROM.read(address);
  Serial.println(b); //prints anything but 80
}


void loop()
{
}

I believe this should print the value of "a" (80) but instead it shows anything but 80, depending on the address I use. Maybe the EEPROM is wrong? I'm using and arduino nano 328

You swapped the order of arguments to the write function.

// void write( int idx, uint8_t val );

  EEPROM.write(a, address);

// should be:

  EEPROM.write(address, a);

:wink:

Whandall:
You swapped the order of arguments to the write function.

// void write( int idx, uint8_t val );

EEPROM.write(a, address);

// should be:

EEPROM.write(a, address);




;)

Thank you!! I don know if laugh or cry :slight_smile:

Shit happens.
So I used my EEProm fpr the first time.

I owe you a 0x80. :wink:

// should be:

Really?

Correct syntax is: EEPROM.write(address, value) !