How to Save Value from Counter to EEPROM in Arduino Uno [solved]

greetings folks, so i was started my project for a long time ago and im kinda stuck right now
im making this counter for flight cycle using arduino uno with sharpIR sensor, and my problem is when the arduino is restarting, the value is reset to 0 so it makes all the counter proccessing is useless.
and i just realize this EEPROM stuff and i dont know how to apply it on my project to make the counter keep the value before it got restarted.

  • so my question is how to save my counter value in EEPROM (im newbie and i already try several tests after watch some tutorial on youtube but ended up nothing)
  • im using lcd 16x2 but its broken and dont have time to buy it so im using serial monitor instead for now ,my 2nd question is if i apply EEPROM in it, will it works as well in serial monitor? i mean will the valueis saved in EEPROM (in serial monitor)? i already try to use to counter but if i unplug and plug again, the serial monitor wont count anything but if i close the serial monitor and open it again,it will work again and restart from 0 again

here is my code

const int sensorPin = A0; //analog pin 0
const int ledPin = 13;  // the pin that the LED is attached to
const int buttonPin = 2;


int sensorCounter = 0;   // counter for the number of button presses
int sensorState = 0;     // current state of the sensor
int resetButton = 0;
int addr = 0;

void setup() {

  pinMode(sensorPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);


  }


void loop() {
  sensorState = analogRead(sensorPin);
  if (buttonPin == HIGH) sensorState =0;

  if (sensorState == 400 ) {
  
    sensorCounter++;
    
    Serial.println(sensorState);
    Serial.print("Flight Cycle Counter:  ");
    Serial.println(sensorCounter / 2);
    int val = analogRead(0) / 4;
    
      
      addr = addr + 1;

    addr = 0;
  }
    
    delay(2000);
  } 

  
  }

and this with my code with include EEPROM

const int sensorPin = A0; //analog pin 0
const int ledPin = 13;  // the pin that the LED is attached to
const int buttonPin = 2;
#include <EEPROM.h>
// vars
int sensorCounter = 0;   // counter for the number of button presses
int sensorState = 0;     // current state of the sensor
int resetButton = 0;
int addr = 0;

void setup() {
  // initialize the sensor pin and input
  pinMode(sensorPin, INPUT);
  // initialize the LED as an output
  pinMode(ledPin, OUTPUT);
  // initialize serial communication
  Serial.begin(9600);
  EEPROM.write(sensorState, sensorState);
// set the matrix 

  }


void loop() {
  // read the sensor input pin:
  sensorState = analogRead(sensorPin);
  if (buttonPin == HIGH) sensorState =0;
  // if sensor is closer than than x distance
  if (sensorState == 400 ) {
  
    sensorCounter++;
    
    Serial.println(sensorState);
    Serial.print("Flight Cycle Counter:  ");
    Serial.println(sensorCounter / 2);
    int val = analogRead(0) / 4;
    // show output on the digital display
      
      addr = addr + 1;
  if (addr == EEPROM.length()) {
    addr = 0;
  }
    // slow down the output
    delay(2000);
  } 

  
  }
 EEPROM.write(sensorState, sensorState);

This will write 0 to EEPROM address 0 every time you start or reset the Arduino. Is that what you want ?

Then you never read from the EEPROM so why bother writing to it ?

  sensorState = analogRead(sensorPin);
  if (buttonPin == HIGH) sensorState = 0;

buttonPin has been set to 8 (ie HIGH) when declared and its value will never change so why test it ? Did you mean to digitalRead() the pin ?

Your program should write sensorCounter to EEPROM address 0 when its value changes and set the value of sensorCounter to the value at EEPROM address 0 in setup() to restore the previously written value after a reset or restart.

NOTE : The EEPROM write() and read() functions write a single byte to EEPROM and your counter is a 2 byte int. You need to look at the EEPROM get() and put() functions to read and write an int.

address

Keep in mind that EEPROM has a finite number of write cycles.
One method to avoid using them up too quickly - is to look at the power supply, and if it is going down, save the value to EEPROM... otherwise keep your live counter in RAM, and reload it from the saved value next run - during setup()

UKHeliBob:

 EEPROM.write(sensorState, sensorState);

This will write 0 to EEPROM address 0 every time you start or reset the Arduino. Is that what you want ?

Then you never read from the EEPROM so why bother writing to it ?

no, what i want is when the counter is running like 1,2,3,4,5,6. so i turn off my arduino at 6 value, when i turn it on again it still at 6 instead of 0.

lastchancename:
Keep in mind that EEPROM has a finite number of write cycles.
One method to avoid using them up too quickly - is to look at the power supply, and if it is going down, save the value to EEPROM... otherwise keep your live counter in RAM, and reload it from the saved value next run - during setup()

i understand what you saying but im suck at programming, im still trying to get to know the programming code. i just learn it so it will takes time

Don't try to write it all at once.

Write one program that puts a fixed int value into a known EEPROM location in the setup() function.

Write a second program that gets the int value from the same EEPROM location in setup() and prints its value to the Serial monitor.

Note the use of put() and get() rather than write() and read()

There are examples of both included with the EEPROM library.

UKHeliBob:
Don't try to write it all at once.

Write one program that puts a fixed int value into a known EEPROM location in the setup() function.

Write a second program that gets the int value from the same EEPROM location in setup() and prints its value to the Serial monitor.

Note the use of put() and get() rather than write() and read()

There are examples of both included with the EEPROM library.

i cant use put and get because after i learn it im kinda confuse how to apply it but i use write and read instead because its pretty simple to use, and now another problem is when the arduino is start,the value is null but if sensor is sense something, it goes 0 instead 1.

#include <LiquidCrystal.h>
#include <EEPROM.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int sensorPin = A0; //analog pin 0
const int ledPin = 13;  // the pin that the LED is attached to


// vars
int sensorCounter = 0;   // counter for the number of button presses
int sensorState = 0;     // current state of the sensor
int resetButton = 0;
int addr = 0;
int debounceTime = 20;



void setup() {
  // initialize the sensor pin and input
  pinMode(sensorPin, INPUT);
  // initialize the LED as an output
  pinMode(ledPin, OUTPUT);
  // initialize serial communication
  Serial.begin(9600);

// set the matrix 
  lcd.begin(16,2);
  lcd.print("Flight Cycle :");
  
sensorCounter = EEPROM.read(0);
EEPROM.write (0,0);
  for (int i = 0 ; i < EEPROM.length() ; i++) {
    EEPROM.write(i, 0);
}
}

void loop() {
  // read the sensor input pin:
 int sensorState = analogRead(sensorPin);
  // if sensor is closer than than x distance
  if (sensorState == 400 ) {
  
    sensorCounter++;
    EEPROM.write(0, sensorCounter);

    lcd.setCursor (0, 1);
    lcd.print(sensorCounter / 2);
    int val = analogRead(0) / 4;
    // show output on the digital display
      
      addr = addr + 1;
  if (addr == EEPROM.length()) {
    addr = 0;

  }
    // slow down the output
    delay(2000);
  } 

  
  }

thank you by the way guys

put() and get() work basically like write() and read(); the difference is that you don't have to worry about the type of the variable. You're storing an int but when using write you only write half of that information; some for the read.

put example

...
...
int sensorCounter = 5;
...
...

void setup()
{
    Serial.begin(9600);
    // save sensor counter
    EEPROM.put(0, sensorCounter);
    Serial.print("sensorCounter value ");
    Serial.print(sensorCounter);
    Serial.println(" saved to eeprom");
}

void loop()
{
}

get example

...
...
int sensorCounter = 0;
...
...

void setup()
{
    Serial.begin(9600);
    // read sensor counter
    EEPROM.get(0, sensorCounter);
    Serial.print("retrieved sensorCounter: ");
    Serial.println(sensorCounter);
}

void loop()
{
}

Not tested but should give the idea.

sterretje:
put() and get() work basically like write() and read(); the difference is that you don't have to worry about the type of the variable. You're storing an int but when using write you only write half of that information; some for the read.

put example

...

...
int sensorCounter = 5;
...
...

void setup()
{
    Serial.begin(9600);
    // save sensor counter
    EEPROM.put(0, sensorCounter);
    Serial.print("sensorCounter value ");
    Serial.print(sensorCounter);
    Serial.println(" saved to eeprom");
}

void loop()
{
}



**get example**


...
...
int sensorCounter = 0;
...
...

void setup()
{
    Serial.begin(9600);
    // read sensor counter
    EEPROM.get(0, sensorCounter);
    Serial.print("retrieved sensorCounter: ");
    Serial.println(sensorCounter);
}

void loop()
{
}




Not tested but should give the idea.

thank you mate, ill try :slight_smile:

I have same problem how you can solved

ahmedhodhod:
I have same problem how you can solved

By reading the replies.

1 Like