FireTimer icon indicating copy to clipboard operation
FireTimer copied to clipboard

BUG when delayed a fire

Open DinoMesina opened this issue 2 years ago • 1 comments
trafficstars

There is a BUG in reset() when the fire() function is called in delay it do not compute correctly the time between the calls. Maybe its a features but when I need a correct time count between two calls it fails continuous

#include "FireTimer.h"
FireTimer timer0, timer1;
unsigned long start, diff;

void setup() {
  Serial.begin(115200);
  timer0.start();
  timer1.start();
  timer0.begin(1000);
  timer1.begin(1000);
  start = millis();
}

void loop() {
  if (timer0.fire()) {
    Serial.print("timer0.timeDiff = "); Serial.println(timer0.timeDiff);
  }
  diff = millis() - start;
  if (diff >= 1500) {
    start = millis();
    if (timer1.fire()) {
      Serial.println();
      Serial.print("millis() diff   = "); Serial.println(diff);
      Serial.print("timer1.timeDiff = "); Serial.println(timer1.timeDiff);
      Serial.println();
    }
  }
  delay(10);
}
13:43:59.142 -> timer0.timeDiff = 1000
13:43:59.640 -> 
13:43:59.640 -> millis() diff   = 1500
13:43:59.640 -> timer1.timeDiff = 1500
13:43:59.673 -> 
13:44:00.138 -> timer0.timeDiff = 1000
13:44:01.167 -> timer0.timeDiff = 1000
13:44:01.167 -> 
13:44:01.167 -> millis() diff   = 1500
13:44:01.167 -> timer1.timeDiff = 2000
13:44:01.167 -> 
13:44:02.163 -> timer0.timeDiff = 1000
13:44:02.661 -> 
13:44:02.661 -> millis() diff   = 1500
13:44:02.661 -> timer1.timeDiff = 2500
13:44:02.661 -> 
13:44:03.159 -> timer0.timeDiff = 1000
13:44:04.155 -> timer0.timeDiff = 1000
13:44:04.155 -> 
13:44:04.155 -> millis() diff   = 1500
13:44:04.155 -> timer1.timeDiff = 3000
13:44:04.155 -> 
13:44:05.151 -> timer0.timeDiff = 1000
13:44:05.649 -> 
13:44:05.649 -> millis() diff   = 1500
13:44:05.649 -> timer1.timeDiff = 3500
13:44:05.649 -> 

DinoMesina avatar Aug 20 '23 11:08 DinoMesina

timeDiff is only updated in fire():

https://github.com/PowerBroker2/FireTimer/blob/13778fed308bf9000d63f3de01f39ac02e9e4e84/src/FireTimer.cpp#L131-L135

I could see making timeDiff a private member and incorporating a getTimeDiff() function in it's place so that you will always get the correct time difference. I'll give it a thought.

PowerBroker2 avatar Aug 21 '23 19:08 PowerBroker2