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++)
{