Trying to customise <TimeLib.h>

Status
Not open for further replies.

laptophead

Well-known member
I am putting together a downtime tracking device for an industrial application.

THe time is arbitrary and it starts with the reset button or powering up. TimeLib.h does a nice job to avoid rolling millis and it displays correctly the time elapsed since the start of the processor.

However, my application requires that I show hours from 0 to 99. The trouble is that TimeLib.h will roll hours in days after 24hrs as normal.

I wonder if I could customize it to show just minutes and hours up to 99 hrs. There is very little reference on it.

If you have another algorithm that will convert millis in min and hours, I am all ears.

Thanks

Mitch

I use now
tft.setTextSize(4);

tft.print(hour());
tft.print(" ");
tft.print(minute());
tft.print(" ");
tft.print(second());
 
You don't need a library for that.

In setup(), you set up an IntervalTimer object which launches the dispElapsed function once a second, nothing from that must go into the loop():
Code:
IntervalTimer myTick;

void setup() {
  myTick.begin(dispElapsed, 1000000); //update the display by calling dispElapsed() every second aka 1000000 microseconds
}

void loop() {
  //other stuff might go here
}

void dispElapsed() {
uint32_t cont_seconds = millis() / 1000; 
uint32_t cont_minutes = cont_seconds / 60;
uint32_t cont_hours = cont_minutes / 60;
tft.print(cont_hours); //will not roll
tft.print(" ");
tft.print(cont_minutes % 60); //will roll from 59 to 0
tft.print(" ");
tft.print(cont_seconds % 60); //will roll from 59 to 0
}
 
Monseur Ingineur,

Merci beaucoup,

However I am running all this on a Mega . The TFT plugs easily on a mega.

IntervalTimer myTick; does not compile. Any ideas?

to tell you more of what I want to do:

Once I see the elapsed time, I also need to count time during a condition "UPTIME", such as

while digitalRead pin14 == High , count time. etc,

Then I want to deduct the second time from the elapsed time and obtain a UPTIME- Downtime percentage.

Now, the Condition might be ON and OFF many times, and I need to add all those Times, till I hit the reboot time.


A Reboot limit, that the operator will set. Say 8 hours, end of a shift. At that time, the arduino will Zero all values (Elapsed and UPTIME) and start the process again.

Qua faire?

Thanks a lot

Mitch
 
You are running this on a Mega??? This is a forum where Teensy users help Teensy users. Thus, my code, using Teensyduino objects, will work on a Teensy!
 
You can get something pretty similar to IntervalTimer on Arduino Mega using the TimerOne or TimerThree library. Arduino also also a blink without delay example in the File > Examples menu which might help.

Arduino's forum would be a much better place to ask for help specific to programming on their board.
 
<TimeLib.h> is working on any arduino, I was hoping to get some help here.
I am a fan of Teensy and I use it any time I can, but this TFT requires Mega.
This forum generally provides much better help than the arduino forum, and the <TimeLib.h> was written here.

So i thought I would ask for help. I got to figure out something else I guess,

Thanks

I will try the IntervalTimer on my next teensy project
 
Paul, Please I still need your help

While using the TimeLib, I built a equation to transform the current time in elapsed seconds.

ElapsedSeconds = (hour() * 3600) + (minute() * 60) + (second());

I never get past 24 hrs. The math works fine as long as the time does not exceed 9hrs and 7 min.

Past that , the Elapsed seconds jumps into millions totally non nonsensical, but the time above is still correct.
Why would that happen?

I attached pics. first line is the time, and second is elapsed seconds.
Thanks
 

Attachments

  • Cycle_Box - 1.jpg
    Cycle_Box - 1.jpg
    142 KB · Views: 124
  • Cycle_Box - 2.jpg
    Cycle_Box - 2.jpg
    117.7 KB · Views: 117
If there are problems at 9h and 7min, which corresponds to roughly 32800 or 15 bit, that means that there is somewhere a 16bit integer variable rolling over. Make sur that everything is of the uint32_t type.
 
You often need to make sure you understand how many bits of data each match expression is doing.
So if you have this: ElapsedSeconds = (hour() * 3600) + (minute() * 60) + (second());

So as Themingenieur mentioned when you said that you had problems with the 9 hours... again maybe some of the math is being done with signed 16 calculations.

Sometimes I do overkill and do some castings to see if they help

Things like:
Code:
 ElapsedSeconds = ((uint32_t)hour() * 3600) + ((uint32_t)minute() * 60) + ((uint32_t)second());
 
Status
Not open for further replies.
Back
Top