Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG when delayed a fire #2

Open
DinoMesina opened this issue Aug 20, 2023 · 1 comment
Open

BUG when delayed a fire #2

DinoMesina opened this issue Aug 20, 2023 · 1 comment
Assignees

Comments

@DinoMesina
Copy link

DinoMesina commented Aug 20, 2023

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 -> 
@PowerBroker2 PowerBroker2 self-assigned this Aug 21, 2023
@PowerBroker2
Copy link
Owner

timeDiff is only updated in fire():

FireTimer/src/FireTimer.cpp

Lines 131 to 135 in 13778fe

// Handle overflow
if (currentTime < timeBench)
timeDiff = (UL_MAX - timeBench) + currentTime;
else
timeDiff = currentTime - timeBench;

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants