How to use EEPROM correctly?

I wrote correctly because my EEPROM doesn't seem to read EEPROM correctly every time. Sometimes it fails to read the data that is written or it doesn't write at all. How should I use it? Delays?

I'm using built in .put .get functions and structs all the time. Size of the struct is about 512chars :o

Thanks in advance.

PS: I'm using a button that is attached as a interrupt, which calls a function to clear eeprom. This sometimes fails to clear eeprom too. Any suggestions?

PSS: I've already tried out few other microcontrollers (atmegas)

Don't try to write to the EEPROM (or erase) within an interrupt. The EEPROM uses interrupts to do its work, which would be disabled if called from within an interrupt context.

For a button being pressed by a human, there is never a reason to use an interrupt. Use digitalRead() like everyone else.

I'm doing tons of web access work in loop(), therefore it requires a human to press that eeprom reset button for like 10 seconds, which lacks user experience at some point. Do you recommend anything else to reset eeprom?

Would it be too much to ask to see some (well, all, really) of your code?

(deleted)

spycatcher2k:
set a flag in the interrupt, and check for it in loop - remember to make it volatile.

YES! I've just done the same. Logic rocks mate.

Thanks guys, cheers.

edencakir:
I wrote correctly because my EEPROM doesn't seem to read EEPROM correctly every time. Sometimes it fails to read the data that is written or it doesn't write at all. How should I use it? Delays?

I'm using a button that is attached as a interrupt, which calls a function to clear eeprom.
Bad idea!

Interrupt handling routines should be FAST! I.e. less than one microsecond or just a few microseconds.
But writing to EEPROM ist slow, I think with Atmega controllers the duration is something like 5 milliseconds per byte with an Arduino UNO (Atmega328).

That's 1000 times as long as an interrupt handling should last at most.

jurs:

edencakir:
I wrote correctly because my EEPROM doesn't seem to read EEPROM correctly every time. Sometimes it fails to read the data that is written or it doesn't write at all. How should I use it? Delays?

I'm using a button that is attached as a interrupt, which calls a function to clear eeprom.
Bad idea!

Interrupt handling routines should be FAST! I.e. less than one microsecond or just a few microseconds.
But writing to EEPROM ist slow, I think with Atmega controllers the duration is something like 5 milliseconds per byte with an Arduino UNO (Atmega328).

That's 1000 times as long as an interrupt handling should last at most.

Why aren't they just include this kind of information on their eeprom website? or its just me missing the details.

Because everybody (?) knows that writing EEPROM is slow :wink: Nah seriously, EEPROMWrite actually gives the number. And the 328 datasheet as well.

Using the principles in Demonstration code for several things at the same time can allow you to write the eeprom while doing other stuff; just don't use delay :smiley:

sterretje:
Because everybody (?) knows that writing EEPROM is slow :wink: Nah seriously, EEPROMWrite actually gives the number. And the 328 datasheet as well.

Using the principles in Demonstration code for several things at the same time can allow you to write the eeprom while doing other stuff; just don't use delay :smiley:

That example seems really complicated and I don't seem to understand it fully :slight_smile:

Off-topic: What did you do that 15 years?

Mostly software (Linux and Windows), software testing, 3rd line support for 30 year old systems and little bit of project management.

That example seems really complicated and I don't seem to understand it fully :slight_smile:

Really complicated? Then read first this: https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay.

Several things ... isn't that complicated, but it has many examples rolled into one sketch. Here is a super-simplified demonstration of non-blocking timing that just does two things - reads a switch and counts up numbers on the serial monitor. You will need to wire a normally open switch to pin 8 (or the pin of your choice.) Momentarily close the switch, and the count resets. The switch is continuously monitored, the count code executes once a second. No delays are used - you can make this as complicated as you like.

// Simplest counter, output to serial

unsigned long previousSecondMillis;
const long oneSecond = 1000UL;
int counter = 0;

void setup()
{
  pinMode(8, INPUT_PULLUP); // pin -- button -- gnd
  Serial.begin(9600);
  previousSecondMillis = millis();
}

void loop() {

  // this section executes every time through the loop
  if (!digitalRead(8)) counter = 0;

  // this section gets executed every oneSecond millis
  if (millis() - previousSecondMillis >= oneSecond) {
    previousSecondMillis += oneSecond; // do this once a second
    Serial.println(counter++);
  }
}