Skip to content

Instantly share code, notes, and snippets.

@kakopappa
Last active June 6, 2021 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kakopappa/2eef95c7737ef0b7d997f5d181a486bc to your computer and use it in GitHub Desktop.
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
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