counting down and stroing into EEPROM

Hello friends.

Another day, another learning question.

What i am using: ESP32, Nextion display

First: Thanks again to all the help from you guys.

What i am trying to do: I want to create a counter that is counting down. When the counter reaches zero an event should be triggered. It should also be possible to "refresh" the counter back to its starting value. When my ESP32 is unbplugged and replugged the counter should still be the same. So i have to store the current data into my "ESP-EEPROM".

I looked into diffrent threads. This is what i have so far:

The button that refrehs my counter back to its starting value:

void resetServiceIntervallPopCallback(void *ptr)
{
  DateTime now = rtc.now();
  DateTime future (now + TimeSpan(0,0,4,0));         //gives me the time 4 minutes into the future
  GenInfo._serviceIntervall = future.unixtime();    //store seconds since1970 into _serviceIntervall
}

My couting down routing:

void ServiceInterval()
{
  DateTime now4Service = rtc.now();
  uint32_t timeNow = now4Service.unixtime();
  uint32_t counter;
  uint32_t counter2;
  uint32_t counter_copy;

  if(timeNow <= GenInfo._serviceIntervall)
  {
    counter = (GenInfo._serviceIntervall - timeNow)/60;
    counter2 = (GenInfo._serviceIntervall - timeNow);
    o_errorFlag.ServiceIntervalFlag=0;
    nserviceInta.setValueGlobal("GeneralInfo",counter);

   if (counter_copy!=counter)
   {
    Serial.println("Write into EEPROM;");
   }
  }
  if(timeNow == GenInfo._serviceIntervall)
  {
  o_errorFlag.ServiceIntervalFlag=1;
  Serial.println("ALARM - Counter is zero");
  }
  counter_copy = counter;
}

The counter (as far as i see) works as i want. The messages, when the counter reaches zero does also what i am expecting, but i have troubles with storing the data into EEPROM

The problem that i have: i cant handle it, that "write into EEPROM" just appears every minute (counter ). The idea behind it is , that in the future i dont want to write the data every second into EEPROM

What do is miss?

Thank you a lot

best regards

“ i dont want to write the data every second into EEPROM”

I have not used the ESP32 yet.

Try looking at the power supply voltage with an analog input.

When the power supply voltage reaches a certain value ex: 2.5V, write the counter to EPROM.

Then when the voltage gets higher than 3V restart the sketch.

i kinda tried it with this

counter = (GenInfo._serviceIntervall - timeNow)/60;

So this is how i can handle the counter an the time, when it will be stored into EPROM

Its not clear to me why this part, isnt executed every minute

   if (counter_copy!=counter)
   {
    Serial.println("Write into EEPROM;");
   }

What you describe will very quickly wear out any built-in EEPROM. EEPROM has a limited life, often as little as 10,000 writes, before it no longer works reliably. There are external, I2C or SPI, EEPROMs that are good for as many as 1,000,000 writes.

Regards,
Ray L.

thank you for your answer. yes i know.

the example is just for testing purposes. My plan is at the end to write only into eeprom once a day (everytime the day changes)

for example: counter = (GenInfo._serviceIntervall - timeNow)/86 400;

so only evertime "counter" changes.

So what i would expecting

Since i store the counter into my copycounter (counter_copy = counter;) i would expect that IF
the minute changes (counter = (GenInfo._serviceIntervall - timeNow)/60;) the if clause should be triggered

   if (counter_copy!=counter)
   {
    Serial.println("Write into EEPROM;");
   }

So what i figured out.

in this part

   if (counter_copy!=counter)
   {
    Serial.println("Write into EEPROM;");
   }

counter_copy is alsways zero. But why?

It appears you initially set the counter to count at 4 minutes intervals from the current time, making the counter reach 0 at 4 minute intervals. Would be easier to save the initial time in eprom, then when restating get the current time, calculate where in the 4 minute cycle you are currently, and restart the timer from there.

yes. good idea. thats kinda what i planed.

But my problem i still.

Lets say i initially set my counter at 7 days.

Here im countin down:
counter = (GenInfo._serviceIntervall - timeNow)/86 400; //counter are days now
counter2 = (GenInfo._serviceIntervall - timeNow);

Note that _serviceIntervall and timeNow are sconds.

So i must now create an event that writes only once a day into my eprom.

void ServiceInterval()
{
  DateTime now4Service = rtc.now();
  uint32_t timeNow = now4Service.unixtime();
  uint32_t counter;
  uint32_t counter2;
  uint32_t counter_copy;

  if(timeNow <= GenInfo._serviceIntervall)
  {
    counter = (GenInfo._serviceIntervall - timeNow)/86 400;
    counter2 = (GenInfo._serviceIntervall - timeNow);
    o_errorFlag.ServiceIntervalFlag=0;
    nserviceInta.setValueGlobal("GeneralInfo",counter);

   if (counter_copy!=counter)
   {
    Serial.println("Write into EEPROM;");
   }
  }
  if(timeNow == GenInfo._serviceIntervall)
  {
  o_errorFlag.ServiceIntervalFlag=1;
  Serial.println("ALARM - Counter is zero");
  }
  counter_copy = counter;
}

thats why i store the counter at the end of the code into counter_copy. So that only when counter (that are days now) changes, the value will be written into eprom. But i dont know why

   if (counter_copy!=counter)
   {
    Serial.println("Write into EEPROM;");
   }

does not work

I don't see why you need to store the counter itself into EPROM, what you need is the original start time and the interval, that will allow you to restart the counter in sync with the original timing. Or am I misunderstanding what you are trying to accomplish.

sorry i dont understand what you mean :confused:

What i try to accomplish: A timer (that can be reseted) that counts down. When i unplugg my ESP, the timer should not start again from the begining. So thats why i need the eprom

Lets say:

With this function i generate the seconds since 1970 until my future event

void resetServiceIntervallPopCallback(void *ptr)
{
  DateTime now = rtc.now();
  DateTime future (now + TimeSpan(0,0,4,0));         //gives me the time 4 minutes into the future
  GenInfo._serviceIntervall = future.unixtime();    //store seconds since1970 into _serviceIntervall
}

My future event is for testing purpose 4 minutes into the future. ( in the endphase this will be days).

what would be your next steps?

thank you