How to save data after power off?

Hello,

I'm trying to save data (count of clip 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.

and this is my code:

void writeIntArrayIntoEEPROM(int address, int counterOutput[], int arraySize)
{
int addressIndex = address;
for (int i = 0; i < arraySize; i++)
{
EEPROM.write(addressIndex, counterOutput[i] >> 8);
EEPROM.write(addressIndex + 1, counterOutput[i] & 0xFF);
addressIndex += 2;
}
}
void readIntArrayFromEEPROM(int address, int counterOutput[], int arraySize)
{
int addressIndex = address;
for (int i = 0; i < arraySize; i++)
{
counterOutput[i] = (EEPROM.read(addressIndex) << 8) + EEPROM.read(addressIndex + 1);
addressIndex += 2;
}
}
void writeIntIntoEEPROM(int address, int number)
{
byte byte1 = number>>8;
byte byte2 = number & 0xFF;
EEPROM.write(address,byte1);
EEPROM.write(address + 1,byte2);
}
void writeIntIntoEEPROM1(int address1, int number1)
{
byte byte3 = number1>>8;
byte byte4 = number1 & 0xFF;
EEPROM.write(address1,byte3);
EEPROM.write(address1 + 1,byte4);
}
void writeIntIntoEEPROM2(int address2, int number2)
{
byte byte5 = number2>>8;
byte byte6 = number2 & 0xFF;
EEPROM.write(address2,byte5);
EEPROM.write(address2 + 1,byte6);
}
int readIntFromEEPROM(int address)
{
byte byte1 = EEPROM.read(address);
byte byte2 = EEPROM.read(address + 1);
return (byte1 << 8) + byte2;
}
int readIntFromEEPROM1(int address1)
{
byte byte3 = EEPROM.read(address1);
byte byte4 = EEPROM.read(address1 + 1);
return (byte3 << 8) + byte4;
}
int readIntFromEEPROM2(int address2)
{
byte byte5 = EEPROM.read(address2);
byte byte6 = EEPROM.read(address2 + 1);
return (byte5 << 8) + byte6;
}
void setup()
{
Serial.begin(9600);
lcd.begin(16,2);
lcd.clear();
lcd.backlight();
//writeIntArrayIntoEEPROM(STARTING_EEPROM_ADDRESS, counterOutput, ARRAY_SIZE);
readIntArrayFromEEPROM(STARTING_EEPROM_ADDRESS, counterOutput, ARRAY_SIZE);
//writeIntIntoEEPROM(3, 0);
//writeIntIntoEEPROM1(5, 0);
//writeIntIntoEEPROM2(9, 0);
a = readIntFromEEPROM(3);
b = readIntFromEEPROM1(5);
s = readIntFromEEPROM2(9);
servo1.attach(11); //Servo no. 1 connected to 11
servo2.attach(12); //Servo no. 2 connected to 12
servo1.write(0);
servo2.write(0);
digitalWrite(outputPin[0], HIGH);
digitalWrite(outputPin[1], HIGH);
for (int i=0; i<=6; i++)
{
pinMode(inputPin[i], INPUT_PULLUP);
}
for (int i=0; i<=1; i++)
{
pinMode(outputPin[i], OUTPUT);
}
lcd.setCursor(0,0);
lcd.print("Penghitung Klip");
lcd.setCursor(0,1);
lcd.print(" Kurihara ");
delay(1500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Oleh Engineering");
lcd.setCursor(0,1);
lcd.print(" PT. NKI ");
delay(1500);
lcd.clear();

}

void loop()
{
Serial.print("a=");
Serial.print(a);
Serial.print("b=");
Serial.print(b);
Serial.print("s=");
Serial.print(s);
writeIntIntoEEPROM(3, a);
writeIntIntoEEPROM1(5, b);
writeIntIntoEEPROM2(9, s);
for (int i=0; i<=6; i++)
{

edit your post, use CODE-tags

// Write an object to the EEPROM address
addr = 20;
struct MyObject {
  uint8_t version;
  float field1;
  uint16_t field2;
  char name[10];
};
MyObject myObj = { 0, 12.34f, 25, "Test!" };
EEPROM.put(addr, myObj);

Maybe you should include EEPROM.h.

And don't use EEPROM.write(), use EEPROM.put() and EEPROM.get() everywhere (bytes, ints, floats, structs etc); it might prevent unnecessary EEPROM wear and is so much easier, especially if you follow @kolaha's example (note that EEPROM.put() must be in a function).

I would use an SD card , maybe fram ?

There are limited read write cycles for a lot of Arduino chips , so not suitable for this task .

You might want to detect power loss and keep the Arduino running long enough to save data

I would save my data BEFORE the power goes off.

Perhaps you need to look at the power suppli to identify WHEN it’s going off, or implement a soft power switch, so you can save states before cutting the juice.

@5heroez
In the Arduino IDE, on the Menu bar click on Edit then Copy for Forum, then post your code here. Then it will be readable by all forum members