Saving data in your Arduino when turn it off

Hello,

I'm trying to save data (count of pulses of a push button) in my Arduino, when I reset the program or for some reason the Arduino could turn off for some reason.

This way, to have my data still in the same count it was.

I hope someone could help me.

Check out the EEPROM library:
www.arduino.cc/en/Reference/EEPROM
--Michael

Look for Arduino tutorials about EEPROM, but be careful because it is really easy to use up the lifetime of your Arduino EEPROM by mistakes in the program.

I'm still searching for something, but nothing yet u.u

This is my code:
// this constant won't change:
const int Crimp_Right = 2; // the pin for the crimp signal R
const int Crimp_Left = 3; // the pin for the crimp signal F

// Variables will change:
int Hits_R = 0; // counter for the number of hits
int State_Right = 0; // current state of the crimp
int Last_Right = 0; // previous state of the crimp
int val = 1;
int Hits_L = 0; // counter for the number of hits
int State_Left = 0; // current state of the crimp
int Last_Left = 0; // previous state of the crimp

void setup() {
// initialize the button pin as a input:
pinMode(Crimp_Right, INPUT);
pinMode(Crimp_Left, INPUT);
// initialize serial communication:
Serial.begin(9600);
}

void loop() {
State_Right = digitalRead(Crimp_Right);
State_Left = digitalRead(Crimp_Left);

// compare the buttonState to its previous state
if (State_Right != Last_Right) {
// if the state has changed, increment the counter
if (State_Right == HIGH) {
// if the crimp state is HIGH then add a 1 to the count
Hits_R++;
Serial.println(Hits_R);
}
// save the current state as the last state,
//for next time through the loop
Last_Right = State_Right;
}
}

I want to have my count it the same number when I reset or turn off my Arduino.

Your code doesn't have any reference to EEPROM.
Or any code tags.

To post code and/or error messages:

  1. Use CTRL-T in the Arduino IDE to autoformat your complete code.
  2. Paste the complete autoformatted code between code tags (the </> button)
    so that we can easily see and deal with your code.
  3. Paste the complete error message between code tags (the </> button)
    so that we can easily see and deal with your messages.
  4. If you already posted without code tags, you may add the code tags by
    editing your post. Do not change your existing posts in any other way.
    You may make additional posts as needed.

Before posting again, you should read the three locked topics at the top of the Programming Questions forum, and any links to which these posts point.

If your project involves wiring, please provide a schematic and/or a wiring diagram and/or a clear photograph of the wiring.

Good Luck!

Simplistic example :

In setup()

EEPROM.get(0, Hits_R);  //get the previously saved value from EEPROM address zero

In loop()

Hits_R++;
EEPROM.write(0, Hits_R);  //save the new value to EEPROM address zero

Shouldn't that be "put", not "write"?

Thank you UKHeliBob!

It already worked for me :slight_smile:

AWOL:
Shouldn't that be "put", not "write"?

Yes it should.

I am sure that I wrote put not write but I am obviously wrong :slight_smile: