From b2e9d90686720adb3aeae85c5f1365769b2f6998 Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <49808844+DmitryBorisenko33@users.noreply.github.com> Date: Wed, 18 Nov 2020 13:18:08 +0300 Subject: [PATCH] timer bug fully fixed --- data/items/count-down.txt | 2 +- include/items/vCountDown.h | 9 ++++++++- src/items/vCountDown.cpp | 18 ++++++++++-------- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/data/items/count-down.txt b/data/items/count-down.txt index fa38b94b..3300573c 100644 --- a/data/items/count-down.txt +++ b/data/items/count-down.txt @@ -1 +1 @@ -0;count-down;id;na;na;na;order \ No newline at end of file +0;count-down;id;anydata;Таймер;Обратный#отчет;order \ No newline at end of file diff --git a/include/items/vCountDown.h b/include/items/vCountDown.h index 1c5e9f14..a8d9d46a 100644 --- a/include/items/vCountDown.h +++ b/include/items/vCountDown.h @@ -16,8 +16,15 @@ public: private: unsigned long _countDownPeriod = 0; - String _key; bool _start = false; + String _key; + + unsigned long currentMillis; + unsigned long prevMillis1; + unsigned long prevMillis2; + unsigned long difference1; + unsigned long difference2; + int sec; }; diff --git a/src/items/vCountDown.cpp b/src/items/vCountDown.cpp index 2215ca9f..118f7b3a 100644 --- a/src/items/vCountDown.cpp +++ b/src/items/vCountDown.cpp @@ -17,24 +17,23 @@ void CountDownClass::execute(unsigned int countDownPeriod) { } void CountDownClass::loop() { - static unsigned long prevMillis1; - static unsigned long prevMillis2; - static int sec; if (_countDownPeriod > 0 && _start) { prevMillis1 = millis(); _start = false; - sec = (_countDownPeriod / 1000) + 1; + sec = (_countDownPeriod / 1000); } - unsigned long currentMillis = millis(); - unsigned long difference1 = currentMillis - prevMillis1; - unsigned long difference2 = currentMillis - prevMillis2; + currentMillis = millis(); + difference1 = currentMillis - prevMillis1; + difference2 = currentMillis - prevMillis2; if (difference1 > _countDownPeriod && _countDownPeriod > 0) { _countDownPeriod = 0; eventGen2(_key, "0"); + Serial.println(_key + " completed"); } if (difference2 > 1000 && _countDownPeriod > 0) { prevMillis2 = millis(); - Serial.println(sec--); + sec--; + Serial.println(_key + " " + String(sec)); publishStatus(_key, String(sec)); } } @@ -49,6 +48,9 @@ void countDown() { countDown_EnterCounter++; addKey(key, countDown_KeyList, countDown_EnterCounter); + Serial.println(countDown_EnterCounter); + Serial.println(countDown_KeyList); + static bool firstTime = true; if (firstTime) myCountDown = new MyCountDownVector(); firstTime = false;