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

Thread: How accurate is elapsedMillis?

  1. #1

    How accurate is elapsedMillis?

    I have Teensy 3.6's that I'm working with.

    I have a project that needs to keep track of how long solenoids have been energized and if a pre-set time is exceeded shut them down. This is to avoid them burning up. The time on this is in the 10 - 60 millisecond range (some solenoids go longer than others).

    The documentation on elapsedMillis says to avoid == in comparisons because the value can change by more than 1. How much more can it change?

    It's looking like perhaps elaspedMicros would be better. Is there a significant impact to having dozens of these timers running at the same time (around 88 in total)? I'm assuming these variables are updated in an interrupt, and the more of them there are the longer that interrupt will take, and a microsecond counter is updated 1000 more often than a millisecond one, but perhaps it's so fast it wouldn't matter. I'm use to much slower processors

    Thanks!

  2. #2
    Senior Member
    Join Date
    Feb 2016
    Location
    Australia
    Posts
    271
    1.However much time the rest of your code has taken.That is, you need to check for > rather than == because your code might be doing other stuff for 2ms between the time you check it.

  3. #3
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,120
    You can also use the TeensyTimerTool for such things. I'd define a oneShotTimer for each solenoid. Whenever you switch a solenoid on it triggers the timer with the required solenoid 'on time'. In the callback of the timer you switch it off. Per default you have 20 software timers, but this can be increased to any required number in the config file. See the documentation for details: https://github.com/luni64/TeensyTimerTool/wiki.

    Here a quick proof of principle. It sets up an array of timers and attaches the switchOff callback to each of the timers. The callback gets the solenoid number as parameter so that you can do whatever needs to be done to switch the solenoid off. In loop the example switches on all solenoids and uses a random on time between 10 and 60ms. After a pause of 2s it repeats the procedure. You can also pack this nicely in a Solenoid class if you like.

    Code:
    #include "Arduino.h"
    #include "TeensyTimerTool.h"
    using namespace TeensyTimerTool;
    
    constexpr unsigned nrOfSolenoids = 20;    // 20 available by default, can be increased in the config file
    OneShotTimer *timers[nrOfSolenoids];      // Array of timers
    
    void switchOff(unsigned sNr)
    {
      //do whatever needs to be done to switch off solenoid number sNr
      Serial.printf("Solenoid nr %d off\n", sNr);
    }
    
    void setup()
    {
      while(!Serial){}  
    
      for (unsigned i = 0; i < nrOfSolenoids; i++) // setup all timers
      {
        timers[i] = new OneShotTimer(TCK);         // construct a one shot software timer...
        timers[i]->begin([i] { switchOff(i); });   // and attach a callback to it. Same callback for all timers but pass the timer number to distiguish
      }
    }
    
    void loop()
    {
      // switch all solenoids on:
      for (unsigned i = 0; i < nrOfSolenoids; i++)
      {
        unsigned duration = 1'000 * random(10, 60);               // some random number between 60 and 90 ms
        Serial.printf("Solenoid %d on for %d ms\n", i, duration); // do whatever needs to be done to switch it on
        timers[i]->trigger(duration);                             // start the oneshot timer which switches it off later
      }
      Serial.println("-------------------------");
    
      delay(2000); // repeat every 2 seconds
    }
    Printout:
    Code:
    Solenoid 0 on for 40000 ms
    Solenoid 1 on for 51000 ms
    Solenoid 2 on for 25000 ms
    Solenoid 3 on for 32000 ms
    Solenoid 4 on for 21000 ms
    Solenoid 5 on for 51000 ms
    Solenoid 6 on for 20000 ms
    Solenoid 7 on for 47000 ms
    Solenoid 8 on for 58000 ms
    Solenoid 9 on for 51000 ms
    Solenoid 10 on for 54000 ms
    Solenoid 11 on for 40000 ms
    Solenoid 12 on for 36000 ms
    Solenoid 13 on for 56000 ms
    Solenoid 14 on for 20000 ms
    Solenoid 15 on for 48000 ms
    Solenoid 16 on for 19000 ms
    Solenoid 17 on for 15000 ms
    Solenoid 18 on for 44000 ms
    Solenoid 19 on for 24000 ms
    -------------------------
    Solenoid nr 17 off
    Solenoid nr 16 off
    Solenoid nr 6 off
    Solenoid nr 14 off
    Solenoid nr 4 off
    Solenoid nr 19 off
    Solenoid nr 2 off
    Solenoid nr 3 off
    Solenoid nr 12 off
    Solenoid nr 0 off
    Solenoid nr 11 off
    Solenoid nr 18 off
    Solenoid nr 7 off
    Solenoid nr 15 off
    Solenoid nr 1 off
    Solenoid nr 5 off
    Solenoid nr 9 off
    Solenoid nr 10 off
    Solenoid nr 13 off
    Solenoid nr 8 off

  4. #4
    Thanks for the information, I'll be sure to take a look at it.

    Appreciate your help!

Posting Permissions

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