Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 10 of 10

Thread: Trying to customise <TimeLib.h>

  1. #1
    Member
    Join Date
    Dec 2016
    Location
    Buena Park ca
    Posts
    51

    Trying to customise <TimeLib.h>

    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());

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    18,369
    elapsedMillis msec;

    uint32_t totalsec = msec / 1000;
    uint32_t hours = totalsec / 3600;

  3. #3
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    1,908
    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
    }

  4. #4
    Member
    Join Date
    Dec 2016
    Location
    Buena Park ca
    Posts
    51
    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

  5. #5
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    1,908
    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!

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    18,369
    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.

  7. #7
    Member
    Join Date
    Dec 2016
    Location
    Buena Park ca
    Posts
    51
    <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

  8. #8
    Member
    Join Date
    Dec 2016
    Location
    Buena Park ca
    Posts
    51

    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
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	Cycle_Box - 1.jpg 
Views:	24 
Size:	142.0 KB 
ID:	12097   Click image for larger version. 

Name:	Cycle_Box - 2.jpg 
Views:	30 
Size:	117.7 KB 
ID:	12098  


  9. #9
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    1,908
    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.

  10. #10
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    3,653
    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());

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •