EEPROM problem

Hi
I am trying to save a 2 variables i and j in EEPROM, I can change those values by buttons, but after disconnecting the power both values are not saved and on LCD I have i=0 and j=0.

Please help me to fix the problem.

#include <LiquidCrystal.h>
#include <EEPROM.h>
LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);
int i;
int ovfi;
int j;
int ovfj;

void setup()
{
  pinMode(PB6, INPUT_PULLDOWN);
  pinMode(PB9, INPUT_PULLDOWN);
  pinMode(PB5, INPUT_PULLDOWN);
  pinMode(PB8, INPUT_PULLDOWN);
  lcd.begin(16, 2);

  EEPROM.read(256);
  EEPROM.read(0);
}


void loop()
{
  ovfi = 3000 - i;
  if (digitalRead(PB8) == HIGH)
  {
    if (i < 3000)
    {
      i++;
    }

  }
  if (digitalRead(PB5) == HIGH)
  {
    if (i > 0)

    {
      i--;
    }
  }

  EEPROM.write(256, i);
  //////////////////////////////////
  ovfi = 3000 - j;
  if (digitalRead(PB9) == HIGH)
  {
    if (i < 3000)
    {
      j++;
    }

  }
  if (digitalRead(PB6) == HIGH)
  {
    if (i > 0)

    {
      j--;
    }
  }
  EEPROM.write(0, i);
  /////////////////////////////////
  lcd.setCursor(0, 0);
  lcd.print(EEPROM.read(256));

  lcd.setCursor(0, 1);
  lcd.print(EEPROM.read(0));
}

Have run your code and suggest the following changes.

Initialize variables when they are declared.

int i = 0;
int ovfi = 0;
int j = 0;
int ovfj = 0;

The EEPROM values are read in setup() but not assigned to any variable, so when the code runs i and j are zero every time.

i = EEPROM.read(256);
j = EEPROM.read(0);

In the j loop you use ovfi not ovfj (I think this is what you intend)
also the if statements refer to i not j but increment or decrement j

Regards
Jim

PS I didnt have an LCD but after the changes the EEPROM values are updated correctly.

3 Likes

Thanks, it is working

Great fix James!