Last active
May 29, 2021 21:47
-
-
Save EleotleCram/0893191b09043aa91585a1850c52c6d6 to your computer and use it in GitHub Desktop.
Simple Arduino non-blocking interval macros
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
#pragma once | |
// Utility macros for periodically doing stuff in a non-blocking loop. | |
// Usage: | |
// | |
// void loop() { | |
// EVERY(2.5 SECONDS) { | |
// static bool ledOn = false; | |
// ledOn = !ledOn; | |
// digitalWrite(LED_BUILTIN, ledOn); | |
// } | |
// } | |
// Note: | |
// | |
// Every ~50 days the Arduino internal timer overflows, these macros | |
// are susceptible to this overflow. | |
#include <stdint.h> | |
#define MILLISECONDS * 1 | |
#define SECONDS * 1000 MILLISECONDS | |
#define SECOND (1 SECONDS) | |
#define MINUTES * 60 SECONDS | |
#define MINUTE (1 MINUTES) | |
#define HOURS * 60 MINUTES | |
#define HOUR (1 HOURS) | |
#define DAYS * 24 HOURS | |
#define DAY (1 DAYS) | |
#define WEEKS * 7 DAYS | |
#define WEEK (1 WEEKS) | |
#define EVERY(N) \ | |
for (static uint32_t _lasttime; \ | |
(uint32_t)((uint32_t)millis() - _lasttime) >= (N); _lasttime += (N)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It completely eludes me, why beginners are taught about the delay() function, even when doing it properly from the start can be written so cleanly...