Couldn't save counter to eeprom

I created a counter that will count from 1 to 10 , and I want this counter to resume from the last count whenever the power is interrupted, but unfortunately the eeprom couldn't saved the counter and it always restart the counting from 1, while I want it to continue from where it stoped before the power goes off.
Any help i will appreciate.

Here is my code

#include <EEPROM.h>

uint8_t EEPROMaddress = 0;

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);  // I2C address 0x27, 16 column and 2 rows

#include <Servo.h>
int TIME = 1000;
int servopin = 9;
int degree0 = 0;
int degree180 = 180;

int redled = 8;
int j;

Servo myservo;

void setup() {
  pinMode(redled, OUTPUT);
  Serial.begin(9600);
  myservo.attach(servopin);

  lcd.init();
  lcd.backlight();
}

void loop() {
  lcd.setCursor(3, 0);
  lcd.print("JC-INC");

  for (j = 1; j <= 10; j = j + 1) {
    if (j < 6) {
      digitalWrite(redled, HIGH);
      myservo.write(degree0);
      delay(TIME);
    }

    if (j >= 6) {
      digitalWrite(redled, LOW);
      myservo.write(degree180);
      delay(TIME);
    }
    Serial.println(j);

    lcd.setCursor(6, 1);
    lcd.print(j);
    delay(TIME);
  }
  lcd.clear();
  j = EEPROM.read(EEPROMaddress);
  EEPROM.write(EEPROMaddress, j);
}

If you are trying to resume the condition at the time (more or less) that the devices stopped, you should read the eeprom in setup().
Don't write to the eeprom every loop iteration or you will destroy it. Only write occasionally because it can tolerate only a limited number of write operations.

Please post your code properly, it is in the instructions. Also comments help all of us, please use them. What Arduino are you using, they are not all the same? If you want to keep writing to EEPROM consider FRAM instead, 32K x 8 modules can be gotten for less then $5.oo and they are available in SPI or I2C.

Hi , thanks for your comment.
I'm using the Arduino uno for this project.
And also I'm a newbie, I don't know much about the fram you are talking about.
However I will appreciate your support if you can help in anyway to get through the project. Thanks.

@ogorry
1. What events are being counted by your counter?
2. What is the time gap between the occurrence of the two events of Step-1?
3. How are you sensing the power interruption?
4. When power fails, the MCU must go to an ISR and save the count value of counter into the internal EEPROM of the MCU.

5. When power resumes, the MCU begins program execution from the setup() functoion. So, you should read the EEPROM in the setup() function as suggested by @6v6gt and update the counter accordingly.

FRAM (Ferroelectric Random Access Memory) which is read write non volatile memory. It is fast, requires no delays with the Arduino, remembers even during power fail. This will allow you to write each cycle of your program, but there is a limit, in the range of 10^14 cycles. Do a google search for "FRAM memory", there is a lot of information.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.