timer bug fully fixed

This commit is contained in:
Dmitry Borisenko
2020-11-18 13:18:08 +03:00
parent ceb516ddbe
commit b2e9d90686
3 changed files with 19 additions and 10 deletions

View File

@@ -1 +1 @@
0;count-down;id;na;na;na;order 0;count-down;id;anydata;Таймер;Обратный#отчет;order

View File

@@ -16,8 +16,15 @@ public:
private: private:
unsigned long _countDownPeriod = 0; unsigned long _countDownPeriod = 0;
String _key;
bool _start = false; bool _start = false;
String _key;
unsigned long currentMillis;
unsigned long prevMillis1;
unsigned long prevMillis2;
unsigned long difference1;
unsigned long difference2;
int sec;
}; };

View File

@@ -17,24 +17,23 @@ void CountDownClass::execute(unsigned int countDownPeriod) {
} }
void CountDownClass::loop() { void CountDownClass::loop() {
static unsigned long prevMillis1;
static unsigned long prevMillis2;
static int sec;
if (_countDownPeriod > 0 && _start) { if (_countDownPeriod > 0 && _start) {
prevMillis1 = millis(); prevMillis1 = millis();
_start = false; _start = false;
sec = (_countDownPeriod / 1000) + 1; sec = (_countDownPeriod / 1000);
} }
unsigned long currentMillis = millis(); currentMillis = millis();
unsigned long difference1 = currentMillis - prevMillis1; difference1 = currentMillis - prevMillis1;
unsigned long difference2 = currentMillis - prevMillis2; difference2 = currentMillis - prevMillis2;
if (difference1 > _countDownPeriod && _countDownPeriod > 0) { if (difference1 > _countDownPeriod && _countDownPeriod > 0) {
_countDownPeriod = 0; _countDownPeriod = 0;
eventGen2(_key, "0"); eventGen2(_key, "0");
Serial.println(_key + " completed");
} }
if (difference2 > 1000 && _countDownPeriod > 0) { if (difference2 > 1000 && _countDownPeriod > 0) {
prevMillis2 = millis(); prevMillis2 = millis();
Serial.println(sec--); sec--;
Serial.println(_key + " " + String(sec));
publishStatus(_key, String(sec)); publishStatus(_key, String(sec));
} }
} }
@@ -49,6 +48,9 @@ void countDown() {
countDown_EnterCounter++; countDown_EnterCounter++;
addKey(key, countDown_KeyList, countDown_EnterCounter); addKey(key, countDown_KeyList, countDown_EnterCounter);
Serial.println(countDown_EnterCounter);
Serial.println(countDown_KeyList);
static bool firstTime = true; static bool firstTime = true;
if (firstTime) myCountDown = new MyCountDownVector(); if (firstTime) myCountDown = new MyCountDownVector();
firstTime = false; firstTime = false;