RTC control in seconds

Hi all,

I need help.... (yes, I know people have been saying that for years.... :rofl: )

I have nearly finished a datalogger and controller and am facing my final two problems...

The first problem is with using an RTC.
the controller needs to perform two functions, one is to turn a machine (and associated ancillaries on and off, and the second is to record data.

In the case of turning the machine on and off, I need to input a time in seconds, ranging from 1 to 1800 seconds entered by a matrix keypad. All of that works perfectly.

once a start button is pressed, I want the RTC to act as a timer in seconds, and then stop the machine after that amount of seconds. Can I read the clock and then 'add' the number of seconds to create a finish time?

during the time the machine is running, I want to monitor several sensors every 10 seconds and export that data (either to an SD card, serial line or via Bluetooth).

I have every function working, except the timer running in seconds. I have a tiny RTC running on I2C. could anyone help guide me on that bit of code please?

I have included the loop(); area of code, and also the cycle mode (where the timing needs to take place..... Unfortunately, as I have used the tab function to create the code, and there are currently 11 tabs, I cannot include the full code, however, the two relevant parts are present.

any help would be much appreciated

ECM_Acid_Datalogger_v1.1.ino (8.0 KB)
cycleMode.ino (665 Bytes)

The cycleMode bit is more of a placeholder at the minute, just so I could check the electrical functions of the system.... so the code just performs a sample routine at present....

Do you need to calculate a finish time, which you can do, or could you just count the number of seconds as they occur ?

I need to calculate a finish time, (based on when the start button is pressed) and then stop the cycle.

during the cycle I need to be able to count seconds and then export the data every 15 seconds......

if this is possible. I do have another RTC (they came in a pack of 5) just in case I can only use one RTC for each timing process.

this will be the first time I have played with RTCs, by the way.

Only if you are going to display it

Easily done by counting seconds

You certainly do not need to use 2 RTCs

So it would be nice to have a countdown timer displayed on one of the LCD's. As even 10 minutes seems to take a lot longer than expected when you are waiting for the result...

Am glad I do not need 2 RTCs, but as I have a pack of them, I just thought I would mention it....

  • List item

Having said that, if I am counting seconds (without displaying the actual finish time) I can use a simple sum, to minus seconds from the entered value..... co, I do not need to calculate the time, I just need to count seconds......

I agree that it would be preferable to display the end time. You can calculate it in a number of ways, the best of which may be to use the epoch time, defined as the number of seconds that have elapsed since 00:00:00 UTC, Thursday, 1 January 1970, not counting leap seconds.

If you keep all times (start, current, end) as their epoch time then it is easy to add or subtract times or calculate the difference between times. Most RTC libraries will have functions to convert to/from epoch time

so, I find the current epoch time, convert that to seconds, add the cycle time I want to run... something like this, but in proper code.

find epoch time
convert to seconds
cycle = cycleTime (seconds) + epochTime (seconds)
if (cycle - (cycleTime + currentEpochTime) <= 0){
end cycle}

Just to show the system I am controlling (forgive the mess, but it is a serious work in progress) I have attached a picture of the R&D equipment.....

I would express what you do like this

get cycle length from user in seconds
end time = current epoch time + cycle length (epoch time is in seconds)
convert end time to human readable form and display it
start cycle
when current epoch time == end time stop the cycle

You can add niceties such as displaying the number of seconds left in the cycle by using the epoch times

Why are you using even one RTC (apart from having a suitcase full)- unless I missed something the real time is irrelevant. Why not just use millis()?

1 Like

Perhaps to a man with a hammer everything looks like a nail

You did. The display of the end time of the cycle is required, or at least desirable

However, to be fair, using an RTC makes working with dates and times very easy and we don't know which Arduino is being used so don't know what other source of time the project has

Do you want to know what time (clock) the process will end or how many minutes and seconds there are to go?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.