Closed
Description
Describe the bug
There is a lot of code, that tries to compute how much time has expired using code like:
uint32_t start_time = micros();
<do something>
uint32_t delta_time = micros() - start_time;
This is not working correctly when the micros rolls over. i.e. the call to micros() returns a smaller number than the previous call.
Example sketch
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial && millis() < 5000) {}
Serial.println("\nTest millis and Micros");
Serial.println(micros());
}
void loop() {
unsigned long start_us = micros();
unsigned long start_ms = millis();
delay(1000);
unsigned long end_us = micros();
unsigned long end_ms = millis();
unsigned long delta_us = end_us - start_us;
unsigned long delta_ms = end_ms - start_ms;
Serial.print("US: ");
Serial.print(start_us);
Serial.print(" ");
Serial.print(end_us);
Serial.print(" ");
Serial.print(delta_us);
Serial.print("\tMS: ");
Serial.print(start_ms);
Serial.print(" ");
Serial.print(end_ms);
Serial.print(" ");
Serial.println(delta_ms);
if (Serial.available()) {
Serial.println("Paused");
while (Serial.read() != -1) {}
while (Serial.read() == -1) {}
while (Serial.read() != -1) {}
}
}
Output from the sketch:
Test millis and Micros
1619447
US: 1619450 2619503 1000053 MS: 1619 2619 1000
US: 2619555 3619603 1000048 MS: 2619 3619 1000
US: 3619632 4619702 1000070 MS: 3619 4619 1000
US: 4619732 5619802 1000070 MS: 4619 5619 1000
US: 5619832 6619902 1000070 MS: 5619 6619 1000
US: 6619933 7620002 1000069 MS: 6619 7620 1001
US: 7620032 8620102 1000070 MS: 7620 8620 1000
US: 8620154 672354 4287019496 MS: 8620 9620 1000
US: 672384 1672454 1000070 MS: 9620 10620 1000
US: 1672484 2672554 1000070 MS: 10620 11620 1000
US: 2672584 3672654 1000070 MS: 11620 12620 1000
US: 3672705 4672754 1000049 MS: 12620 13620 1000
US: 4672784 5672854 1000070 MS: 13620 14620 1000
US: 5672883 6672954 1000071 MS: 14620 15620 1000
US: 6672983 7673054 1000071 MS: 15620 16620 1000
US: 7673084 8673154 1000070 MS: 16620 17621 1001
US: 8673184 725405 4287019517 MS: 17621 18621 1000
US: 725457 1725505 1000048 MS: 18621 19621 1000
US: 1725535 2725605 1000070 MS: 19621 20621 1000
US: 2725657 3725705 1000048 MS: 20621 21621 1000
US: 3725735 4725805 1000070 MS: 21621 22621 1000
US: 4725835 5725905 1000070 MS: 22621 23621 1000
US: 5725935 6726005 1000070 MS: 23621 24621 1000
US: 6726057 7726105 1000048 MS: 24621 25621 1000
US: 7726135 8726205 1000070 MS: 25621 26621 1000
US: 8726235 778457 4287019518 MS: 26621 27622 1001
US: 778487 1778557 1000070 MS: 27622 28622 1000
Paused
Notice the lines like: US: 8620154 672354 4287019496
Additional context
I have not waited long enough on millis() to know if it has similar issue or not.
Metadata
Metadata
Assignees
Labels
No labels