From 6dd175f63bddeb361942df0c2725f7974f1c6e7a Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Mon, 19 Jun 2023 00:33:53 +0200 Subject: [PATCH] fix power change timer micros overflow --- GITHUB_SHA.h | 2 +- MotorDriver.h | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/GITHUB_SHA.h b/GITHUB_SHA.h index 8bd3812..41f732d 100644 --- a/GITHUB_SHA.h +++ b/GITHUB_SHA.h @@ -1 +1 @@ -#define GITHUB_SHA "devel-202306181725Z" +#define GITHUB_SHA "devel-202306182233Z" diff --git a/MotorDriver.h b/MotorDriver.h index fe577bd..1eb4e95 100644 --- a/MotorDriver.h +++ b/MotorDriver.h @@ -190,9 +190,10 @@ class MotorDriver { // was really long ago (approx > 52min) advance counter approx 35 min so that // we are at 18 minutes again. Times for 32 bit unsigned long. inline unsigned long microsSinceLastPowerChange() { - unsigned long diff = micros() - lastPowerChange; - if (diff > (1UL << (6 *sizeof(unsigned long)))) - lastPowerChange += 1UL << (4 * sizeof(unsigned long)); + unsigned long now = micros(); + unsigned long diff = now - lastPowerChange; + if (diff > (1UL << (7 *sizeof(unsigned long)))) // 2^(4*7)us = 268.4 seconds + lastPowerChange = now - 30000000UL; // 30 seconds ago return diff; }; #ifdef ANALOG_READ_INTERRUPT