Problem using EEPROM solved

Hi
Where is the mistake, after turning power off/on i always is 157 ?

#include <EEPROM.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);



void setup()
{
  // int i;

  int i = EEPROM.read(0);

  Serial.begin(19200);
  lcd.begin(16, 2);
  pinMode(buzzer, OUTPUT); //setting up buzzer pin as output
  pinMode(A3, INPUT_PULLUP);
  pinMode(A4, INPUT_PULLUP);

}
void loop()
{
  int i;
  if (digitalRead(A3) == 0) { 
    i++ ;
  }
  if (digitalRead(A4) == 0) { 
    i-- ;

  }


  Serial.println(i);
  
  lcd.setCursor(0, 0);
  lcd.print("Hz ");
  lcd.setCursor(3, 0);
  lcd.print(i);
  lcd.print(' ');
  delay(20);
}

Stop using this program it constantly write to your EEPROM and at the same location, this will quickly burn it out.

The EEPROM only stores bytes not int values. Your code makes little sense. It could be that location zero is already burned out and that is why the value doesn’t change.

…and also, you’ve completely ignored the scope of i.
It is redecorated in all sorts of places, and no two instances will have the same expected value.

... and moreover, i is a terrible name choice for anything other than a loop counter, because it is extensively used and understood by everybody as such.

.... and furthermore, where the contents of the EEPROM is read and viewed?

EEPROM on/off Coding explain more please

@adamnav - better explanation of your problem, please.
(And why hijack a topic?)

You are doing it 50 times per second. You are correct only if your program only ever ran for a total time of 4 seconds (disregarding the time taken by the EEPROM write itself).

Why did you ignore Mike's advice?

If your Arduino's microcontroller is an 8-bit AVR, the int type variable is 2 bytes.
But EEPROM.write() function writes only 1 byte.

Also you write to the EEPROM at 50 times a second while these inputs are LOW.

Please read and UNDERSTAND this before trying to run any code.
Reference EEPROM
If not for your own good, then to prevent you from torturing any more hardware.

Also as you define the variable i at the start of the loop function it won't contain the changes in value you make for the code that follows the write. Being in a function it will be initialised at some random value found in a random address location, so it won't change anyway.

Are you actually looking at the serial print on the serial monitor? This will show you what value is being written, Try it at the start of your loop function as well.

I have also changed the title of this thread to something that is closer to what your real problem is.

Thanks for support, problem solved

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