|
millis_t and ELAPSED() doesn't delay March 08, 2019 04:54AM |
Registered: 7 years ago Posts: 11 |
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 ...
|
Re: millis_t and ELAPSED() doesn't delay March 08, 2019 06:15AM |
Admin Registered: 15 years ago Posts: 7,319 |
|
Re: millis_t and ELAPSED() doesn't delay March 08, 2019 05:40PM |
Registered: 7 years ago Posts: 11 |
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
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 ...