EEPROM reading and writing

I’m having a great deal of trouble with something that should be straightforward - I’m obviously missing something. Hoping someone out there has a second to help.

I am trying to read and write to the EEPROM. However, it appears writes lock up my arduino or garbles the serial output and runs irregularly. Am I trashing program memory/code somehow?

Here is my sample program:

#include <EEPROM.h>

int address = 0;
byte value;

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

void loop()
{  
  EEPROM.write(address,(byte)5);
  delay(100);
  value = EEPROM.read(address);
  
  Serial.println(value);
  
  address = address + 1;
  
  if (address == 512)
    address = 0;
    
  delay(100);
}

I’ve tried small delays, big delays, no delay. If I remove the line that writes, the program runs fine. Reading is not a problem.

Thank you in advance,
Chris

Here is another blob of code that illustrates the problem. The Serial.println(“test”) is never executed. There is no tx light activity either. It just appears to die on the previous line: EEPROM.write(addr, val);

#include <EEPROM.h>

int addr = 0;

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

void loop()
{
  int val = analogRead(0) / 4;
  EEPROM.write(addr, val);
   Serial.println("test");

  addr = addr + 1;
  if (addr == 512)
    addr = 0;
  
  delay(100);
}

Just wanted to follow up on my post since no one ever responded and I finally found a solution. For what ever reason, the arduino wrapper is busted. However, using the native libraries works…

you’ll need to #include <avr/eeprom.h>

void setVal(int index, int value)
{
   eeprom_busy_wait();
   eeprom_write_word((uint16_t*)(index*2),(uint16_t)value);  //mult by 2 to store uint16 (2 bytes)
}

int getVal(int index)
{
   eeprom_busy_wait();
   return (int)eeprom_read_word((uint16_t*)(index*2)); //mult by 2 to store uint16 (2 bytes)
}

The EEPROM library examples are working fine for me using an Diecimila and Arduino 0010. Perhaps you should post more details to track down the problem.

You might need to the delete the .o files in the EEPROM directory (in lib/targets/arduino or hardware/libraries).

Is that the problem mentioned in the arduino 10 release notes where switching CPU chips doesn't remember to recompile the libraries for the new CPU ?

westfw: That particular problem was fixed in Arduino 0008, but it seemed worth deleting the .o file just in case something similar was happening. I just tried switching microcontrollers using the Boards menu in Arduino 0010 and it worked fine.