Last active
June 6, 2021 06:33
-
-
Save kakopappa/2eef95c7737ef0b7d997f5d181a486bc to your computer and use it in GitHub Desktop.
wait example without delay for esp8266/esp32 from Boris. Blame him if there are bugs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Wait { | |
public: | |
Wait() : next_millis(0) {} | |
bool operator()(unsigned long wait_millis, bool instant=false) { | |
unsigned long current_millis = millis(); | |
if (next_millis == 0 && instant==false) next_millis = current_millis + wait_millis; | |
if (current_millis >= next_millis) { | |
next_millis = current_millis + wait_millis; | |
return true; | |
} | |
return false; | |
} | |
protected: | |
unsigned long next_millis; | |
}; | |
void loop() { | |
static Wait wait_1; // can be static or in protected or private section... | |
static Wait wait_2; // can be static or in protected or private section... | |
if (wait_1(1000)) Serial.printf("Device \"%s\" is running...(%lu)\r\n", deviceId.toString().c_str(), millis() / 1000); | |
if (wait_2(2000)) Serial.printf("Device \"%s\": this should run every 2nd second...\r\n", deviceId.toString().c_str()); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment