EEPROM storage corrupted while power "OFF and "ON" back

I am storing pulse counting figures into EEprom in ATMega164.
The problem is, the stored figure corrupted, once I Power OFF and ON back.
The figures changed to some other unexpected figures.

Need help to solve this issue.

Why the EEprom storage changed?
How to make it stable?

#define Pulse_In1 (20) //Pulse to be counted - generated from pulse generator.
#define Pulse_In2 (11) //Pulse to be counted - generated from pulse generator.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>  //startup internal memory.
unsigned long Pulse_count1;  
unsigned long Pulse_count2;  
unsigned long Pulse_count3;  




LiquidCrystal_I2C lcd(0x26,16,2);

bool out = LOW;
bool out1 = LOW;



void setup() {

lcd.begin(16,2);
lcd.backlight();
lcd.clear();
pinMode(Pulse_In1, INPUT);
pinMode(Pulse_In2, INPUT);


Pulse_count1 =EEPROM[ 0 ]; // to store the total pulse counted from pulse generator.
Pulse_count2 =EEPROM[ 1 ]; // to store the total pulse counted from pulse generator.
Pulse_count3 =EEPROM[ 3 ]; // to store the total pulse counted from pulse generator.

}

void loop() {

EEPROM.update(0, Pulse_count1) ;
EEPROM.update(1, Pulse_count2) ;
EEPROM.update(3, Pulse_count3) ;

if (digitalRead(Pulse_In1) != out){ //read pulse from Pulse generator.
 Pulse_count1++; //to count pulses1.
 Pulse_count2++; //to count pulses2.
    out = digitalRead(Pulse_In1);
}

if (digitalRead(Pulse_In2) != out1){ //read pulse from Pulse generator.
 Pulse_count3++; //to count pulses3.
    out1 = digitalRead(Pulse_In2);
}





lcd.backlight();
   lcd.setCursor(0, 0);
   lcd.print("Tl=");
       lcd.setCursor(5,0 );
   lcd.print(Pulse_count1/2);

lcd.setCursor(0, 1);
   lcd.print("T2=");
       lcd.setCursor(5,1 );
   lcd.print(Pulse_count2/2);

 lcd.setCursor(10, 0);
   lcd.print("Tkn=");
       lcd.setCursor(10,1 );
   lcd.print(Pulse_count3/2);  
   
}

sketch_jan29a.ino (1.64 KB)

Use EEPROM.put to store; EEPROM.update only writes a single byte.

Use EEPROM.get to read the values back.

I do tried to use EEPROM.put() and EEPROM.get(), but the results are the same. It still gets corrupted when i turn OFF and ON back, why does that happen?.

Show your latest version of the code please.

Before posting your code, read https://forum.arduino.cc/index.php?topic=148850.0, pay special attention to point #7 and apply it when posting the code.

Hi Sterretje,
Thanks for your help. Attached is the latest code:

#define Pulse_In1 (20) //Pulse to be counted - generated from pulse generator.
#define Pulse_In2 (11) //Pulse to be counted - generated from pulse generator.
#define Pulse_In3 (10) //Pulse to be counted - generated from pulse generator.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>  //startup internal memory.
unsigned long Pulse_count1;  
unsigned long Pulse_count2;  
unsigned long Pulse_count3;  

int address1 = 0;
int address2 = 1;
int address3 = 2;

LiquidCrystal_I2C lcd(0x26,16,2);

bool out = LOW;
bool out1 = LOW;
bool out2 = LOW;

void setup() {
Serial.begin(9600);
EEPROM.get(address1,Pulse_count1);
lcd.begin(16,2);
lcd.backlight();
lcd.clear();
pinMode(Pulse_In1, INPUT);
pinMode(Pulse_In2, INPUT);
 pinMode(Pulse_In3, INPUT);
EEPROM.get(address1 , Pulse_count1);
EEPROM.get(address2 , Pulse_count2);
EEPROM.get(address3 , Pulse_count3);
}

void loop() { 
EEPROM.put(address1,Pulse_count1);
EEPROM.put(address2 , Pulse_count2);
EEPROM.put(address3 , Pulse_count3);

if (digitalRead(Pulse_In1) != out){ //read pulse from Pulse generator.
 Pulse_count1++; //to count pulses1.
     out = digitalRead(Pulse_In1);
}

if (digitalRead(Pulse_In2) != out1){ //read pulse from Pulse generator.
 Pulse_count2++; //to count pulses2.
    out1 = digitalRead(Pulse_In2);
    }

if (digitalRead(Pulse_In3) != out2){ //read pulse from Pulse generator.
 Pulse_count3++; //to count pulses3.
    out2 = digitalRead(Pulse_In3);
    }

lcd.backlight();
   lcd.setCursor(0, 0);
   lcd.print("Tl=");
       lcd.setCursor(5,0 );
   lcd.print(Pulse_count1/2);

lcd.setCursor(0, 1);
   lcd.print("T2=");
       lcd.setCursor(5,1 );
   lcd.print(Pulse_count2/2);

 lcd.setCursor(10, 0);
   lcd.print("T3=");
       lcd.setCursor(10,1 );
   lcd.print(Pulse_count3/2);     
}

Your Pulse_count variables are 32 bit (4 bytes),

Your write the first one to address 0 and it will occupies locations 0..3
next you write your second one to address 1, overwriting locations 1, 2 and 3 of the first Pulse_count.

The addresses where you write should be spaced 4 bytes apart.

int address1 = 0;
int address2 = 4;
int address3 = 8;

Hi Sterretje,
Thanks for your help. Now I manage to solve the problem by using your advise. Let me test it in the actual application. So far so good.
Thanks a lot.
JAMES

Hi Sterretje,

Now I am testing it again after OFF the Power for sometime.
The problem still exist.
Notice that, if the counting are below 50, then there is no problem but if the counting goes beyond 100, the problem occurs.
In my application, I need to have counting up to more that 50,000 pulses.
Hope you can help me.
Thanks

Hi all,
I need some assistance for my coding problem.

I am storing Pulse counting figures into EEprom in ATMega164.
The problem is, the stored figure corrupted, once I Power OFF and ON back.
The figures changed to some other unexpected figures.

Need help to solve this issue.

Why the EEprom storage changed?
How to make it stable?

Below is the codings;

#define Pulse_In1 (20) //Pulse to be counted - generated from pulse generator.
#define Pulse_In2 (11) //Pulse to be counted - generated from pulse generator.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>  //startup internal memory.
unsigned long Pulse_count1;  
unsigned long Pulse_count2;  
unsigned long Pulse_count3;  




LiquidCrystal_I2C lcd(0x26,16,2);

bool out = LOW;
bool out1 = LOW;



void setup() {

lcd.begin(16,2);
lcd.backlight();
lcd.clear();
pinMode(Pulse_In1, INPUT);
pinMode(Pulse_In2, INPUT);


Pulse_count1 =EEPROM[ 0 ]; // to store the total pulse counted from pulse generator.
Pulse_count2 =EEPROM[ 1 ]; // to store the total pulse counted from pulse generator.
Pulse_count3 =EEPROM[ 3 ]; // to store the total pulse counted from pulse generator.

}

void loop() {

EEPROM.update(0, Pulse_count1) ;
EEPROM.update(1, Pulse_count2) ;
EEPROM.update(3, Pulse_count3) ;

if (digitalRead(Pulse_In1) != out){ //read pulse from Pulse generator.
 Pulse_count1++; //to count pulses1.
 Pulse_count2++; //to count pulses2.
    out = digitalRead(Pulse_In1);
}

if (digitalRead(Pulse_In2) != out1){ //read pulse from Pulse generator.
 Pulse_count3++; //to count pulses3.
    out1 = digitalRead(Pulse_In2);
}





lcd.backlight();
   lcd.setCursor(0, 0);
   lcd.print("Tl=");
       lcd.setCursor(5,0 );
   lcd.print(Pulse_count1/2);

lcd.setCursor(0, 1);
   lcd.print("T2=");
       lcd.setCursor(5,1 );
   lcd.print(Pulse_count2/2);

 lcd.setCursor(10, 0);
   lcd.print("Tkn=");
       lcd.setCursor(10,1 );
   lcd.print(Pulse_count3/2);  
   
}
unsigned long Pulse_count1; 
unsigned long Pulse_count2; 
unsigned long Pulse_count3;
int address1 = 0;
int address2 = 1;
int address3 = 2;
Pulse_count1 =EEPROM[ 0 ]; // to store the total pulse counted from pulse generator.
Pulse_count2 =EEPROM[ 1 ]; // to store the total pulse counted from pulse generator.
Pulse_count3 =EEPROM[ 3 ]; // to store the total pulse counted from pulse generator.
EEPROM.update(0, Pulse_count1) ;
EEPROM.update(1, Pulse_count2) ;
EEPROM.update(3, Pulse_count3) ;

I thought you were changing your addresses to be 4 locations apart, and then going to use .put() and .get() to use with the long integers.

Yes, I had tried to change according to suggested coding last night.
And now in the morning, I am trying again. Hope fully it works.
I will feedback the result soon.
Thanks for all the help.
James

Thank you very much for your guide.
I manage to solve the issue by your guide.
I change the address to 0, 4 & 8.
With this changes, I tested it in my application, it works fine up to about 100counts of pulses.
Once I Off and On back, one of the figure chanced to 96 counts.
So, I tried with changing the address to 8, 16 & 24.
So far it works fine without any problem.
But I don't really understand the reason behind it.
Can someone explain to me.
Thanks.
James

But I don't really understand the reason behind it.
Can someone explain to me.

EEPROM cells have a limited number of write cycles (100,000).

If you exceed that (e.g. because you initially used EEPROM.write instead of EEPROM.update), bits can start falling over.

100 decimal is 0110 0100 in binary, 96 decimal is 0110 0000 binary; one bit difference in this case.

In general it's advised to only write to EEPROM when data changes; EEPROM.put and EEPROM.update will take care of that for you. But if you initially used EEPROM.write, you buggered up your EEPROM in no time.