Welcome! Log In Create A New Profile

Advanced

millis_t and ELAPSED() doesn't delay

Posted by davidramiro 
millis_t and ELAPSED() doesn't delay
March 08, 2019 04:54AM
Hi there,

I'm kind of struggling with millis_t and the ELAPSED macro.
I'm working on a filament runout routine that triggers only if the sensor has been high for 3 seconds.
So far my code looks like this, my issue is that the routine gets executed immediately:

  FilamentPinStatus=READ(FIL_RUNOUT_PIN)&0xff;
  const millis_t fil_ms = millis();
  millis_t fil_next_ms = 0;
  // use real time to track filament sensor status (3sec)
  if((FilamentPinStatus>FilamentPinLastStatus) && (ELAPSED(fil_ms, fil_next_ms)))
  {
    fil_next_ms = fil_ms + 3000UL;
    ... filament runout routine here ...

I'm referring to the documentation from here.
Anything I am missing?

Thank you in advance!
Re: millis_t and ELAPSED() doesn't delay
March 08, 2019 06:15AM
your loop is weird...

if((FilamentPinStatus>FilamentPinLastStatus) && (ELAPSED(fil_ms, fil_next_ms))) will always be true the instant FilamentPinStatus>FilamentPinLastStatus as fil_next_ms == 0

what you probably want is

FilamentPinStatus=READ(FIL_RUNOUT_PIN)&0xff;
const millis_t fil_ms = millis();
millis_t fil_next_ms = 3000;
// use real time to track filament sensor status (3sec)
if(FilamentPinStatus>FilamentPinLastStatus) //if pin is high enter the wait loop
{
if((FilamentPinStatus>FilamentPinLastStatus) && (ELAPSED(fil_ms, fil_next_ms)))
... filament runout routine here ...
}

Edited 1 time(s). Last edit at 03/08/2019 06:15AM by Dust.
Re: millis_t and ELAPSED() doesn't delay
March 08, 2019 05:40PM
Quote
Dust
your loop is weird...

if((FilamentPinStatus>FilamentPinLastStatus) && (ELAPSED(fil_ms, fil_next_ms))) will always be true the instant FilamentPinStatus>FilamentPinLastStatus as fil_next_ms == 0

Right on. I didn't get it to compile at first because fil_next_ms wasn't declared, duh, so I just declared it with zero without thinking about it, thanks for pointing that out.

Another issue was that the whole filament sensor polling was a continuous loop itself so I had to make sure the delayed ms value would only get set once. Simply setting the delay to 3000 wouldn't work, since it had to be the current millis plus 3000 to trigger correctly.

FilamentPinStatus=READ(FIL_RUNOUT_PIN)&0xff;
    if(FilamentPinStatus>FilamentPinLastStatus)
    {
      // pin triggered, save current timestamp.
      const millis_t fil_ms = millis();
      static millis_t fil_delay;

      // since all of this is inside a loop, only set delay time once
      if (FilamentSetMillis){
        // set the delayed timestamp to 3000ms later
        fil_delay = fil_ms + 3000UL;
        // this doesn't need to run until the filament is recovered again
        FilamentSetMillis=false;
      }

      // have three seconds passed?
      if ((FilamentTestStatus>FilamentTestLastStatus) && (ELAPSED(fil_ms, fil_delay))) {
        ... filament runout routine ...

        ... set FilamentSetMillis=true as soon as sensor is low again, so the value can get set once on the next pin trigger ...

Works fine now. Thank you for pointing me in the right direction!
Sorry, only registered users may post in this forum.

Click here to login