# Measuring the period

• Hardware model: ESP8266-01 + communication type: Wifi.
• Smartphone OS (iOS or Android): Android + version: lolipop
• Blynk server or local server: Local sure
• Blynk Library version:0.5.4

Hi everybody;

I need an idea to mesure my relay stay open period daily.

Which one is viser?
Using “timelib” to calculate minute, hour, second from start to stop? for example;

if (x>y)
{

....relay on here...

StartHour = hour();
StartMin = minute();
StartSec = second();
}
else
{
....relay off here....
StopHour = hour();
StopMin = minute();
StopSec = second();
}
....then calculation here...
PeriodH = StopHour - StartHour ;
PeriodM = StopMinute - StartMinute ;
PeriodS = StopSec - StartSec ;

or using millis ?

My code must feedback to me like this: "Today relay …hour … min … .sec stands open"
and it will be reset every day automatically

Vich ever one you are more comfortable coding with… time is time… translating to your preferred feedback is all in how you program the output. But, using a time library usually has much of the day, hour, minute coding already done for you, while millis() is like making CPU’s from sand… lots of fiddly steps.

1 Like

I think timelib more suitable for “control the day”
Thanks…
But it will want extra calculation for negative min or sec values

Negative?? Does your relay control a time traveling device?

Relay start time: 23:40 tue
Relay stop time: 01:20 wed

StartHour= 23
StopHour= 01
DurationH = StopHour - StartHour

StartMin=  40
StopMin= 20
DurationM = StopMin - StartMin

Then if you want a result that looks like this… calculating negative numbers is not the answer, a normal time library (and possibly some simple math) that accounts for the day of week (if it changed during the start/stop time) will do the job.

I mean, If your relay starts at 20:00 tue and stops at 22:00 tue (22-20 = 2) its ok, duration formula gives you “2”

but if it starts 23:00 tue and 01:00 wed, simple math always gives you negative number (01 - 23 = -22)

No need to time travel

I want a calculation that MS Excel do

Forget it… my question was “timelib” or “millis”. Timelib is my answer. Thank you

https://playground.arduino.cc/code/time

weekday(); // Day of the week (1-7), Sunday is day 1

But then you use an if() function so that if weekday at start i.e. Tuesday (3) == weekday at end, do nothing. Else if ! i.e. Wednesday (4) take answer (-22) and add 24 = 2… duration formula gives you “2”

Why not just use:

now();             // returns the current time as seconds since Jan 1 1970

That simplifying everything?

2 Likes

Probably a mute point since the OP question is resolved… but out of my own curiosity…

While processing now() at the start and subtracting it from now() at the finish of an action should result in the proper elapsed time… but only in a UNIX time format (time as seconds since Jan 1 1970), so one still has to convert to a standard readable time format… I personally haven’t found a simple Arduino code that does that… and why try when the time libraries do all that for us.

So using the built in weekday(), hour(), minute(), second(), and a little simple math does the same thing.

Unless you have a code snippet using now() that will give a readable time duration between events? That might be good to add in the coding toolbox.

Opps I was able to make “CPU from sand” after all.

Dear @Gunner,

Psudo code:
startTime=now(); /// time_t
...
endTime=now(); /// time_t
durationTime=endTime-startTime; /// durationTime should be global time_t
int2Time(durationTime);
terminal.printf("  end @ %02d:%02d:%02d \n",hour(endTime), minute(endTime), second(endTime)); /// if you want endTime in human like format...
terminal.flush();
...

/// START of int2Time convert integer to time format ///
void int2Time(time_t duration)
{
if(duration >= 86400){ /// 86400
terminal.printf("%dD:", (duration / 86400)); /// how many days ? ///
duration = duration % 86400;
}
if(duration >= 3600){ /// 3600
terminal.printf("%02dH:",(duration / 3600)); /// how many hours ? ///
duration = duration % 3600;
}
if(duration >= 60){ /// 60
terminal.printf("%02dM:",(duration / 60)); /// how many minutes ? ///
duration = duration % 60;
}
terminal.printf("%02dS",(duration)); /// how many seconds ? ///
terminal.flush();
}
/// END of int2Time convert integer to time format ///

Does it make sense to you?

Thanks and Best Regards,
Mike Kranidis

1 Like

Yes, thanks… it is a very similar process to what I discovered earlier with the millis() translation I posted about above.

I was trying to figure out how to get days… and now I think what you did will work the same way for the Timelibrary/RTC free millis() option as well.

Makes sense now… seconds from Jan 1, 1970 or seconds from MCU boot is still just a lot of seconds that can be broken down into readable portions… doh

I am still learning the power of sprintf (and now how to use it with terminal ) Arithmetic Operators like % (remainder, not percent ) and waaayy too much about the necessity of my nemesis… math

2 Likes

Just for your convenience or for your future projects search at GitHub eztime. It has all we need. ( Sorry not in my computer to sent you the link… )

2 Likes