Skip to content

Instantly share code, notes, and snippets.

@vargham
Last active March 17, 2018 11:17
Show Gist options
  • Save vargham/95e9e6e37f926d2fa68ac58c0144257a to your computer and use it in GitHub Desktop.
Save vargham/95e9e6e37f926d2fa68ac58c0144257a to your computer and use it in GitHub Desktop.
Simple, cooperative timed tasks in Arduino
/*
* 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