How to start a count down timer & save to EEPROM

Hi i really do not like posting again and again. but considering my previous example it kinda set me on the right path and i got the result quickly. what i wanna do this time is

-initialize a countdown timer everytime a specific button is pushed
-it will count down to 21 days on the 18th day it will execute a function
-save the updated value to eeprom
-and on every reset continue the countdown from that value.

i have searched a bit all i could find was the minutes timer. now the reason i did not follow those tutorials was that they did not have any other function to achieve or carry out. but i do. i have to measure the angle of the motor the temperature the humidity and the rtc so i thought how am i going to time that?

i read up some more and found the millis function. now that is all good and great but how am i going to save that value to eeprom? even if i do manage to do that. it gets updated so quickly it is going to destroy the eeprom in days.

i could create a case where when that button is pushed it starts executes that function and add the eeprom.read() function in the setup so that whenever i reset it reads it.

I was going to write my idea here but got confused in the middle. i am lost.
kindly suggest what i have to learn in order to achieve this task and keep the days passed in eeprom and not destroy it.

note: i have never used the eeprom.

Thanks!

over 21 days you don't want to use the millis() function - if I remember correctly your last post you have a RTC attached to your system, so use the RTC to check progress and you only need to write the current date in EEPROM at the time you press the button. (just make sure you have something that tells you if the EEPROM has been initialized with a timing - You can see how to deal with first time write using a keyword as I recently described in that post )

pseudo code:

setup:
initialize pins and local variables
initialize RTC
check EEPROM for preferences

buttonPressed function
write in EEPROM keyword and current time (unix time) which will be the count down start time in a known EEPROM location + a flag stating you are at step 0 in another location

loop
if keyword is in EEPROM
if so read the start time of countdown
compare to current RTC time
if action needed, check in EEPROM if that action (which steps you are at) has already taken place
if not take action and add a flag in EEPROM to mark step taken (for example step 1, 2, 3...)
if full countdown has elapsed or no more steps are required then remove keyword from EEPROM to let your code know there is no instruction set

Your RTC is battery backed so you don't need to save current time to the EEPROM

Link to OP's previous thread for context:

odometer:
Link to OP's previous thread for context:
How to Request time from RTC 1307 - Programming Questions - Arduino Forum

Thanks for that :slight_smile:

I think it will be better for everyone if i explain what i am trying to achieve exactly. i mean how many things i am dealing with.
I am making a fully automatic incubator

these are the things i have to do with time.

-rotate the motor after a 6 hour interval 3 times each day
-also controlling the motors position so that it does not accidentally flip the eggs upside down
for that i am trying to come up with a solution with time. i will declare a total time it takes to complete 180 degrees
which in my case is 30s and divide that by two which is 15s (for a single arc) and divide that by two further because for every arc it will take 7.5s to go to exactly 180degrees and back. which completes the 15s and the same for the other direction. so that if the power cuts at 3 setting it is going to calculate that complete the 15 seconds and continue the cycle. i am still having some issues understanding how i will write this data so that it remembers how many seconds it had traveled before the power outage.. it all comes to eeprom.
another idea was to map the value of seconds to degrees and then that will carry out the function and do the same as before.
-have a countdown that will remember the number of days left
-ring a buzzer when the 18th day is up and stop the motor from rotating permanently
-show the number of days left on the screen

I guess that is all i want to achieve and obviously recording temperature humidity controlling a relay l298n motor controller and a couple of leds and a buzzer. i guess that is it.

but this has proven so troublesome so far. maybe because i am new to programming but it is fun that is why i am sticking to it. as close as i get to the completion of project i get more errors and it feels like it is further away than before.

my main objective was to learn coding through this. which i am but i wanted to go deep. but i think i just want to complete the project now on any cost. cause its been about 2 months. I am tired XD

What kind of motor are you using?

It seems that you really don't care about seconds, you care about degrees. Maybe there is some way to measure the position of your motor. That is why I am asking what kind of motor it is.

manhoosbilli1:
-have a countdown that will remember the number of days left
-ring a buzzer when the 18th day is up and stop the motor from rotating permanently
-show the number of days left on the screen

You have an RTC. You know how to read the date from the RTC.

The only date that you need to save (to EEPROM or whatever) is the date that you started. That is because you have the RTC to tell you what today's date is. From that you can calculate the number of days that have passed. Since your countdown always starts from 21 days, the number of days left will always be 21 minus the number of days that have passed.

Example calculation:
The countdown runs for 21 days. It started on April 15. Today is April 20. How many days are left?
From April 15 to April 20 is 5 days. 21 minus 5 is 16. There are 16 days left.

The Arduino can do this kind of calculation. All you need to do is provide it with a calendar so it can do the arithmetic. Why does it need a calendar? I will show you.

Another example calculation:
The countdown runs for 21 days. It started on May 28. Today is June 7. How many days left?
May has 31 days. From May 28 to the end of May is 3 days. Then to June 7 is another 7 days. 3 plus 7 is 10. 10 days have passed. 21 minus 10 is 11. There are 11 days left.

using the DateTime and TimeSpan classes from Adafruit's fork of Jeelab's RTC library will let you do all the math you want with time easily

odometer:
What kind of motor are you using?

It seems that you really don't care about seconds, you care about degrees. Maybe there is some way to measure the position of your motor. That is why I am asking what kind of motor it is.

I have two motors both DC one 12v and the other 24 volt. the 24 volt is geared. which reduces the rpm to as i said 30s per rotation which makes it 2rotations per minute which is good for me. yes i do not care about the seconds but i guess i still need it map the degrees to it. cause i have no other way right now.

J-M-L:
using the DateTime and TimeSpan classes from Adafruit’s fork of Jeelab’s RTC library will let you do all the math you want with time easily

Yes About that, i am trying to avoid libraries as much as i can. if you remember from my last post. i do not want to make something a part of my program if i do not understand how it works. except for i2c LCD cause that is complicated AF XD

I think i got my answer. Thanks for the help. i will work on the code. research a bit and come back with a code i guess. <3

my 2 cts... if you are worried about angles, it's probably better to use motors (or systems attached to the motor) that report current angle rather than rely on "time it takes to do something if there is not power supply issue"...

manhoosbilli1:
Yes About that, i am trying to avoid libraries as much as i can. if you remember from my last post. i do not want to make something a part of my program if i do not understand how it works. except for i2c LCD cause that is complicated AF XD

Whilst I think it's indeed a good practice to understand what one does - you need to set the bar somewhere anyway. One of the beauty of Object oriented programing is abstraction.... you don't need to fully understand to get started as long as you understand how to use the abstraction. Once you have a bit of time you can always go look at the code since it's full available

J-M-L:
Once you have a bit of time you can always go look at the code since it's full available

100% agreed. but i have some reasons for choosing that path. one of biggest reason was that i always somehow got an error with rtc. library. and that is the only library that gave me error. once i wrote my own code which would call on the time. it felt so simple and great that i just can't think of using a library for something as simple as that.

i am sure all of this would be a piece of cake if i had some experience with programming. hell, i just learnt what voltage is a month ago. so i am pretty bad XD

Have a look at the Datecalc example..

you can see how easy it is

J-M-L:
Have a look at the Datecalc example…

you can see how easy it is

not THAT easy for me. i think this link will be of use.
Thanks. <3

would be something like this (conceptually)

#include <Wire.h>
#include "RTClib.h"

RTC_DS3231 rtc;

................


// code when you press the button

   DateTime now = rtc.now(); // now is a DateTime instance initialized with the current time
   // if you save in EEPROM now.unixtime() you have an absolute reference to when the button was pressed
   // this is a 32-bit representation of your time as seconds since 1/1/1970
   uint32_t startMoment = now.unixtime();
   EEPROM.put(xxx, startMoment);   // xxx is the address in EEPROM where you want to save the start Time

................


   // later in the program (probably in the loop()) you extract from EEPROM the data in a 32 bit variable
   uint32_t startMoment; 
   EEPROM.get( xxx, startMoment); // xxx is the address in EEPROM where you saved it

   // here you build a new DateTime using the startMoment unix time
   // LEFT AS AN EXERCISE 
    DateTime startTime  <SOMETHING HERE>;    // hint https://github.com/adafruit/RTClib/blob/82d00a1a15bcf738b001e42bf26a3d1f1a15be32/RTClib.h#L32

    DateTime future (startTime + TimeSpan(18,6,12,32)); // future is a DateTime instance initialized with the start time plus 18 days and 6 hours, 12 minutes, 32 seconds

// you can then compare the current time with your future moment
   DateTime now = rtc.now(); // now is a DateTime instance initialized with the current time

   if (now >= future) {
      // we have reached the time to do something
      // of course you need to check if this has not been already completed
      ......
   }