Last active
March 17, 2018 11:17
-
-
Save vargham/95e9e6e37f926d2fa68ac58c0144257a to your computer and use it in GitHub Desktop.
Simple, cooperative timed tasks in Arduino
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
/* | |
* Simple, cooperative timed tasks example in Arduino | |
* by Mark Peter Vargha | |
* varghamarkpeter.hu | |
*/ | |
#define INTERVAL_LED 1000 | |
#define INTERVAL_PRINT 5000 | |
unsigned long ledTimestamp = 0; | |
unsigned long printTimestamp = 0; | |
void toggleLed() | |
{ | |
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); | |
} | |
void printElapsed() | |
{ | |
Serial.print("Elapsed time: "); | |
Serial.print((millis() / 1000), DEC); | |
Serial.println(); | |
} | |
void setup() | |
{ | |
Serial.begin(9600); | |
pinMode(LED_BUILTIN, OUTPUT); | |
} | |
void loop() | |
{ | |
if ( millis() - ledTimestamp >= INTERVAL_LED) | |
{ | |
toggleLed(); | |
ledTimestamp = millis(); | |
} | |
if (millis() - printTimestamp >= INTERVAL_PRINT) | |
{ | |
printElapsed(); | |
printTimestamp = millis(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment