EEPROM counter writing issue

Hello everyone ,
How to save a counter value in EEPROM before supply break and display it again after alimentation break
The saving values of last steps was saved , i don’t know how to save my counter value

#include <EEPROM.h>
const int  buttonPin = 2;
const int led_1 = 13;
const int led_2 = 12;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
byte ee_valeur0;
byte ee_valeur1;
byte ee_valeur2;
byte ee_valeur3;
void setup() {
  ee_valeur0 = EEPROM.read(0);
  ee_valeur1 = EEPROM.read(1);
  pinMode(buttonPin, INPUT);
  pinMode(led_1, OUTPUT);
  pinMode(led_2, OUTPUT);
  Serial.begin(9600);
  if (ee_valeur0 == 1) {
    digitalWrite(led_1, HIGH);
  }

  if (ee_valeur0 == 0) {
    digitalWrite(led_1, LOW);
  }
  if (ee_valeur1 == 1) {
    digitalWrite(led_2, HIGH);
  }
  if (ee_valeur1 == 0) {
    digitalWrite(led_2, LOW);
  }
}
void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      buttonPushCounter++;
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);

    }
  }

  lastButtonState = buttonState;
  if (buttonPushCounter == 1) {
    digitalWrite(led_1, HIGH);
    EEPROM.write(0, 1);

  } else if (buttonPushCounter == 2) {
    digitalWrite(led_1, LOW);
    EEPROM.write(0, 0);

  }
  else if (buttonPushCounter == 3) {
    digitalWrite(led_2, HIGH);
    EEPROM.write(1, 1);

  }
  else if (buttonPushCounter == 4) {
    digitalWrite(led_2, LOW);
    EEPROM.write(1, 0);

  }
  if (buttonPushCounter > 3)
    buttonPushCounter = 0;

}

Thank you very much for any propositions
Regards

any suggestion please!

zirus109:
any suggestion please!

Yes. I suggest that explain what the problem is with the code you posted.

Writing to EEPROM on every pass through loop() will wear out your EEPROM very quickly. You should only write when the data has changed.

I can see that you are trying to learn EEPROM use. You want to recover from a power-off event and have your arduino "remember" the state it was in before the power got turned off...

a couple of observations:

  1. Since each EEPROM location can take a byte, you could store your four different states in a single EEPROM location.
void setup(void)
{
  byte initialState = 0;
  EEPROM.get(0, initialState);
  switch (initalState)
  {
    case 0:
      // set your first option
      break;
    case 1:
      // set your second options
      break;
    case 2:
      // set your third options
      break;
    case 3:
      // set your fourth options
      break;
    default:
      // set your 'unexpected' value state
      break;
  }
}

If you use EEPROM.put() instead of EEPROM.write() it will write the value to EEPROM if and only if the value has changed. So (while unconventional) changing your EEPROM.write() to EEPROM put() in loop() will save you from frying your EEPROM.

But: Don't do that!

Just make your loop() detect the button change using the StateChangeDetection example supplied to you in the IDE examples. Then, affect all of the state variables and write to EEPROM in one block of code, kind of like this (not tested):

void loop() 
{
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState) 
  {
    if (buttonState == HIGH) 
    {
      ++buttonPushCounter %= 4;  // increment counter mod 4
      EEPROM.put(0,buttonPushCounter);
      //set the state:
      switch(buttonPushCounter)
      {
        case 0:
          // set your first option
          break;
        case 1:
          // set your second options
          break;
        case 2:
          // set your third options
          break;
        case 3:
          // set your fourth options
          break;
      }
        
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;

  // do your other loop() stuff
}

If you require multiple EEPROM storage things, then use a similar loop() structure and write to your values accordingly.

hope that helps.

With respect to EEPROM.put(), would it make sense to use EEPROM.update() instead, since put() calls update()?

econjack:
With respect to EEPROM.put(), would it make sense to use EEPROM.update() instead, since put() calls update()?

good question, I believe that (after reading @pYro_65's legendary post) the difference in the two methods to be simply that put() returns a reference to the object being put and update(), well... doesn't.

To me they are (in this context) synonymous, we are not using put()'s return.

I use put() because it is easier to type. :wink:

I'm trying to think of a use case (vis à vis EEPROM) that we would need a reference to an object that we are putting into EEPROM (since the object is tangible already).

I'm trying to think of a use case (vis à vis EEPROM) that we would need a reference to an object that we are putting into EEPROM (since the object is tangible already).

So you can do something silly like Serial.print(EEPROM.put(someAddr, someValuie));.

PaulS:
So you can do something silly like Serial.print(EEPROM.put(someAddr, someValuie));.

good yeah if I suppose, someValue is really someArgument, for example. yeah.