Problem using EEPROM get() and put() function in my Working hours counting program

Hi
I am not an expert in Arduino programing ,After a lot of Google search i have made a program for my Engine working hours meter with Arduino Mega and Nextion display. Nextion display will shows Engine working hours since its commissioned.

I have used engine pressure signal for pinMode (EngineCommand, INPUT_PULLUP) enable the hours counting and value shows on diplay. At the time of engine off Present total woking hours value stored in EEPROM EEPROM.put(addr_HMR, Hmread)

I have facing the problem is that Arduino start the counting from Zero on every Switch on the unit. But i want to start the counting from its last reading.
Brief details of problem:-

My expectation

if Engine work for 2 hrs at Morning display shows live working hours while engine ON and whenever the dash board switch display will shows last working hours.

Engine again work 2 hrs at evening , display will shows total working hours that is 4 hrs.

Actual happened

Hrs counting start from the zero on every switch on. Not cumulative hours not recorded properly.

This is my code





#include <Nextion.h>/////nextion
#include <EEPROM.h>/////eeprom
#include "RTClib.h"///////////////////// rtc
RTC_DS3231 rtc;////////////////////////// rtc
#define deviceAddress 0b1101000////rtc
#include <Wire.h>

NexButton yes = NexButton(0, 2, "yes");  // Button added
NexButton no = NexButton(0, 3, "no");
NexButton Mtsviw = NexButton(0, 4, "Mtsviw");
NexButton sbmt = NexButton(1, 1, "sbmt");  // Button added
NexButton MtsRst = NexButton(2, 13, "MtsRst");  // Button added
NexButton sbmt1 = NexButton(3, 9, "sbmt1");  // Button added
NexButton IntrSet = NexButton(3, 14, "IntrSet");  // Button added

NexText hmr = NexText(0, 7, "hmr");
NexText Dt = NexText(0, 9, "Dt");
NexText tim = NexText(0, 10, "tim");
NexText MtsDis = NexText(0, 1, "MtsDis");
NexText Eng = NexText(1, 3, "Eng");
NexText Pto = NexText(1, 4, "Pto");
NexText Tm = NexText(1, 5, "Tm");
NexText Hyd = NexText(1, 6, "Hyd");
NexText Fd = NexText(1, 7, "Fd");
NexText EH1 = NexText(1, 15, "EH1");
NexText OH1 = NexText(1, 16, "OH1");
NexText TH1 = NexText(1, 17, "TH1");
NexText HH1 = NexText(1, 18, "HH1");
NexText FH1 = NexText(1, 19, "FH1");
NexText New = NexText(1, 20, "New");
NexText Mtsdetails = NexText(1, 9, "Mtsdetails");
NexText NewIntrvl = NexText(1, 2, "NewIntrvl");
NexText EH = NexText(2, 8, "EH");
NexText PH = NexText(2, 9, "PH");
NexText TH = NexText(2, 10, "TH");
NexText HH = NexText(2, 11, "HH");
NexText FH = NexText(2, 12, "FH");
NexText Eng1 = NexText(3, 1, "Eng");
NexText Pto1 = NexText(3, 2, "Pto");
NexText Tm1 = NexText(3, 3, "Tm");
NexText Hyd1 = NexText(3, 4, "Hyd");
NexText Fd1 = NexText(3, 5, "Fd");
NexText MtsConf = NexText(3, 13, "MtsConf");
NexText All = NexText(3, 12, "All");
NexText Dis = NexText(3, 6, "Dis");

NexPage page3 = NexPage(0, 0, "page3");  // Page added as a touch event
NexPage page0 = NexPage(1, 0, "page0");
NexPage page2 = NexPage(2, 0, "page2");
NexPage page1 = NexPage(3, 0, "page1");

NexText va1 = NexText(0, 6, "va1");

NexNumber va0 = NexNumber(0, 5, "va0");
NexNumber v0 = NexNumber(1, 8, "va0");
NexNumber v1 = NexNumber(1, 11, "va1");
NexNumber Smts1 = NexNumber(1, 14, "Smts1");
NexNumber Sub1 = NexNumber(1, 13, "Sub1");
NexNumber length = NexNumber(1, 12, "length");
NexNumber Smts = NexNumber(3, 7, "Smts");
NexNumber Sub = NexNumber(3, 10, "Sub");
NexNumber Mset = NexNumber(3, 7, "Mset");

int addr_HMR = 300;


NexTouch *nex_listen_list[] =
{
  &yes,
  &no,
  &sbmt,
  &MtsRst,
  &sbmt1,
  &Dt,
  &tim,
  &IntrSet,
  &hmr,
  &MtsDis,
  &Eng,
  &Pto,
  &Tm,
  &Hyd,
  &Fd,
  &Mtsdetails,
  &NewIntrvl,
  &EH,
  &PH,
  &HH,
  &TH,
  &FH,
  &EH1,
  &OH1,
  &HH1,
  &TH1,
  &FH1,
  &New,
  &Eng1,
  &Pto1,
  &Tm1,
  &Hyd1,
  &Fd1,
  &Mtsviw,
  &MtsConf,
  &All,
  &Dis,
  &page3,
  &page0,
  &page2,
  &page1,
  &va1,
  &va0,
  &v0,
  &v1,
  &length,
  &Smts,
  &Sub,
  &Smts1,
  &Sub1,
  &Mset,
  NULL  // String terminated
};  // End of touch event list

long lastTime = 1;

int Hrs1;
volatile byte seconds;
volatile byte minutes;
volatile byte hours;
float Hmread;
float Hrsread;
float hourm;
const byte interruptPin = 2;
volatile byte state1 = LOW;
const uint8_t EngineCommand = 8;
int EventState = 0;
int EventState1 = 0;

void setup() {
  Serial.begin(9600);
  rtc.begin();
  pinMode(EngineCommand, INPUT_PULLUP);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, FALLING);
  setSQW(0x00);
  DateTime now = rtc.now();
  Serial.print("page 0");
  Serial.write(0xFF);
  Serial.write(0xFF);
  Serial.write(0xFF);

}

String time1()
{
  String dataString1 = "";
  DateTime now = rtc.now();

  dataString1 += String(now.year(), DEC);
  dataString1 += String('.');
  dataString1 += String(now.month(), DEC);
  dataString1 += String('.');
  dataString1 += String(now.day(), DEC);
  return dataString1;
}
String time()
{
  String dataString = "";
  DateTime now = rtc.now();
  dataString += String(now.hour(), DEC);
  dataString += String(':');
  dataString += String(now.minute(), DEC);
  dataString += String(':');
  dataString += String(now.second(), DEC);

  return dataString;
}

void loop() 
{
  Serial.print("Dt.txt=");
  Serial.print("\"");
  Serial.print(time1());
  Serial.print("\"");
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);

  Serial.print("tim.txt=");
  Serial.print("\"");
  Serial.print(time());
  Serial.print("\"");
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  





  if (digitalRead(EngineCommand) == HIGH)
  {

   if (EventState < 1)
    {

           EEPROM.put(addr_HMR, Hmread);///Present hour recorded
           
           EventState++;
    }
    hourm = EEPROM.get(addr_HMR, Hrsread);
    Serial.print("hmr.txt=");
    Serial.print("\"");
    Serial.print(hourm);//Display Stored reading Engine OFF Condition
    Serial.print("\"");
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
if (digitalRead(EngineCommand) == LOW)
  {
    Serial.print("hmr.txt=");
    Serial.print("\"");
    Serial.print(Hmread);//Display Live reading Engine ON Condition
    Serial.print("\"");
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
  }
  }
 nexLoop(nex_listen_list);  
}
void blink() 
{
  if (digitalRead(EngineCommand) == LOW)
  {
    EventState = 0; 
    if (lastTime > 0)
    {
      seconds++;
    }
    if (seconds =1)
    {
      minutes++;
      seconds = 0;
    }
    if (minutes > 2)
    {
      hours++;
      minutes = 0;
    }
     state1 = !state1;
    }
String Hours=String(hours) + (".") + String(minutes);
Hmread=Hours.toFloat();
}

void setSQW(uint8_t value) {
    Wire.beginTransmission(deviceAddress); //device address and STSRT command are queued
    Wire.write(0x0E); //Control Register Address is queued
    Wire.write(0x00); //Data for Control Register is queued
    Wire.endTransmission(); //queued information are transferred under ACK; STOP
  }

  

Please help me to this solve the issue.

I think your issue is that after the PUT command you need to issue a EEPROM.commit(); command to actually store it.

I don't believe there is such a function on the Mega.

2 Likes

EEPROM.commit();

Please more details about this function

It is used on devices that emulate EEPROM.

For you, using a Mega, it is irrelevant.

Yes, sorry - I had not realised this was the case, please ignore me :wink:

i don't know how this function work? can anybody explain about this?

It is used on devices like the ESP32 which don't have EEPROM.
It is irrelevant how it works in your context.

Ok,is this any solution to my problem?

At startup, you need to read the hours from EEPROM. As you have it now, with the engine off, you overwrite it with zero.

in this line : hourm = EEPROM.get(addr_HMR, Hrsread);

try this way:

EEPROM.get(addr_HMR, hourm );

RV mineirin

I have solved the issue after some modification i dont know ,is this good method.But my problem is solved and its working.





#include <Nextion.h>/////nextion
#include <EEPROM.h>/////eeprom
#include "RTClib.h"///////////////////// rtc
RTC_DS3231 rtc;////////////////////////// rtc
#define deviceAddress 0b1101000////rtc
#include <Wire.h>

NexButton yes = NexButton(0, 2, "yes");  // Button added
NexButton no = NexButton(0, 3, "no");
NexButton Mtsviw = NexButton(0, 4, "Mtsviw");
NexButton sbmt = NexButton(1, 1, "sbmt");  // Button added
NexButton MtsRst = NexButton(2, 13, "MtsRst");  // Button added
NexButton sbmt1 = NexButton(3, 9, "sbmt1");  // Button added
NexButton IntrSet = NexButton(3, 14, "IntrSet");  // Button added

NexText hmr = NexText(0, 7, "hmr");
NexText Dt = NexText(0, 9, "Dt");
NexText tim = NexText(0, 10, "tim");
NexText MtsDis = NexText(0, 1, "MtsDis");
NexText Eng = NexText(1, 3, "Eng");
NexText Pto = NexText(1, 4, "Pto");
NexText Tm = NexText(1, 5, "Tm");
NexText Hyd = NexText(1, 6, "Hyd");
NexText Fd = NexText(1, 7, "Fd");
NexText EH1 = NexText(1, 15, "EH1");
NexText OH1 = NexText(1, 16, "OH1");
NexText TH1 = NexText(1, 17, "TH1");
NexText HH1 = NexText(1, 18, "HH1");
NexText FH1 = NexText(1, 19, "FH1");
NexText New = NexText(1, 20, "New");
NexText Mtsdetails = NexText(1, 9, "Mtsdetails");
NexText NewIntrvl = NexText(1, 2, "NewIntrvl");
NexText EH = NexText(2, 8, "EH");
NexText PH = NexText(2, 9, "PH");
NexText TH = NexText(2, 10, "TH");
NexText HH = NexText(2, 11, "HH");
NexText FH = NexText(2, 12, "FH");
NexText Eng1 = NexText(3, 1, "Eng");
NexText Pto1 = NexText(3, 2, "Pto");
NexText Tm1 = NexText(3, 3, "Tm");
NexText Hyd1 = NexText(3, 4, "Hyd");
NexText Fd1 = NexText(3, 5, "Fd");
NexText MtsConf = NexText(3, 13, "MtsConf");
NexText All = NexText(3, 12, "All");
NexText Dis = NexText(3, 6, "Dis");

NexPage page3 = NexPage(0, 0, "page3");  // Page added as a touch event
NexPage page0 = NexPage(1, 0, "page0");
NexPage page2 = NexPage(2, 0, "page2");
NexPage page1 = NexPage(3, 0, "page1");

NexText va1 = NexText(0, 6, "va1");

NexNumber va0 = NexNumber(0, 5, "va0");
NexNumber v0 = NexNumber(1, 8, "va0");
NexNumber v1 = NexNumber(1, 11, "va1");
NexNumber Smts1 = NexNumber(1, 14, "Smts1");
NexNumber Sub1 = NexNumber(1, 13, "Sub1");
NexNumber length = NexNumber(1, 12, "length");
NexNumber Smts = NexNumber(3, 7, "Smts");
NexNumber Sub = NexNumber(3, 10, "Sub");
NexNumber Mset = NexNumber(3, 7, "Mset");

int addr_HMR = 300;


NexTouch *nex_listen_list[] =
{
  &yes,
  &no,
  &sbmt,
  &MtsRst,
  &sbmt1,
  &Dt,
  &tim,
  &IntrSet,
  &hmr,
  &MtsDis,
  &Eng,
  &Pto,
  &Tm,
  &Hyd,
  &Fd,
  &Mtsdetails,
  &NewIntrvl,
  &EH,
  &PH,
  &HH,
  &TH,
  &FH,
  &EH1,
  &OH1,
  &HH1,
  &TH1,
  &FH1,
  &New,
  &Eng1,
  &Pto1,
  &Tm1,
  &Hyd1,
  &Fd1,
  &Mtsviw,
  &MtsConf,
  &All,
  &Dis,
  &page3,
  &page0,
  &page2,
  &page1,
  &va1,
  &va0,
  &v0,
  &v1,
  &length,
  &Smts,
  &Sub,
  &Smts1,
  &Sub1,
  &Mset,
  NULL  // String terminated
};  // End of touch event list

long lastTime = 1;

int Hrs1;
 int NoDecHrs;
float Hmread;
float Hrsread;
float hourm;
int addr_secnd = 0;
int addr_Minute = 2;
int addr_hrs2 = 4;
const byte interruptPin = 2;
volatile byte state1 = LOW;
const uint8_t EngineCommand = 8;
int EventState = 0;
int EventState1 = 0;
 volatile byte seconds= EEPROM.read(addr_secnd);
volatile byte minutes= EEPROM.read(addr_Minute);
volatile int hours= EEPROM.get(addr_hrs2,Hrs1);
void setup() {
  Serial.begin(9600);
  rtc.begin();
  pinMode(EngineCommand, INPUT_PULLUP);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, FALLING);
  setSQW(0x00);
  DateTime now = rtc.now();
  Serial.print("page 0");
  Serial.write(0xFF);
  Serial.write(0xFF);
  Serial.write(0xFF);

}

String time1()
{
  String dataString1 = "";
  DateTime now = rtc.now();

  dataString1 += String(now.year(), DEC);
  dataString1 += String('.');
  dataString1 += String(now.month(), DEC);
  dataString1 += String('.');
  dataString1 += String(now.day(), DEC);
  return dataString1;
}
String time()
{
  String dataString = "";
  DateTime now = rtc.now();
  dataString += String(now.hour(), DEC);
  dataString += String(':');
  dataString += String(now.minute(), DEC);
  dataString += String(':');
  dataString += String(now.second(), DEC);

  return dataString;
}

void loop() 
{
  Serial.print("Dt.txt=");
  Serial.print("\"");
  Serial.print(time1());
  Serial.print("\"");
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);

  Serial.print("tim.txt=");
  Serial.print("\"");
  Serial.print(time());
  Serial.print("\"");
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  





  if (digitalRead(EngineCommand) == HIGH)
  {
           EEPROM.get(addr_HMR, Hrsread);
           if(Hrsread=Hmread)
           {
          //Serial.print("high:");
          //Serial.println(Hrsread);
           NoDecHrs= Hrsread;
          Serial.print("hmr.txt=");
          Serial.print("\"");
          Serial.print(Hrsread);//Display Stored reading Engine OFF Condition
          Serial.print("\"");
          Serial.write(0xff);
          Serial.write(0xff);
          Serial.write(0xff);
         }
  
 
  }
   //Serial.println(NoDecHrs);
  if (digitalRead(EngineCommand) == LOW)
  {
    
    //Serial.print("low");
    //Serial.println(Hmread);
    Serial.print("hmr.txt=");
    Serial.print("\"");
    Serial.print(Hmread);//Display Live reading Engine ON Condition
    Serial.print("\"");
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
  }
 nexLoop(nex_listen_list);  
}
void blink() 
{
  if (digitalRead(EngineCommand) == LOW)
  {
    EventState = 0; 
    if (lastTime > 0)
    {
      seconds++;
    }
    if (seconds =1)
    {
      minutes++;
      seconds = 0;
    }
    if (minutes > 2)
    {
      hours++;
      minutes = 0;
    }
     state1 = !state1;
    }
     String Hours=String(hours) + (".") + String(minutes);
     Hmread=Hours.toFloat();
   //Serial.println(Hmread);
    if (digitalRead(EngineCommand) == HIGH)
  {

   if (EventState < 1)
    {

           EEPROM.put(addr_HMR, Hmread);///Present hour recorded
           EEPROM.update(addr_secnd, seconds);
           EEPROM.update(addr_Minute, minutes);
           EEPROM.put(addr_hrs2, hours);
           
          EventState++;
    }
  }

}

void setSQW(uint8_t value) {
    Wire.beginTransmission(deviceAddress); //device address and STSRT command are queued
    Wire.write(0x0E); //Control Register Address is queued
    Wire.write(0x00); //Data for Control Register is queued
    Wire.endTransmission(); //queued information are transferred under ACK; STOP
  }

  /*
   
  
         
   
   */

if its wrong method, please advice me

You shouldn't be doing these time-consuming operations in interrupt context.

When i doing in the void loop stored "Hmread" is zero.
But in the interrupt function it self "Hmread" is stored in EEPROM is actual reading

Oops

1 Like

:relieved: sorry

Is any other option?

On an AVR, it's a pretty binary option - you're either in interrupt context, or you're not (excepting sleep modes)

:roll_eyes:
Please suggest some modifications in program

Use the IDE's auto-format tool before posting code.

Don't do time-consuming operations in interrupt context.