Skip to content

Instantly share code, notes, and snippets.

@vivithemage
Created May 10, 2014 20:03
Show Gist options
  • Save vivithemage/f70b39fd966984d9cf66 to your computer and use it in GitHub Desktop.
Save vivithemage/f70b39fd966984d9cf66 to your computer and use it in GitHub Desktop.
arduino working delay - do things while delay is called rather than wait.
// Used instead of delay so various things can still be checked (e.g. button presses, saving data).
int workingDelay(int interval) {
unsigned long previousMillis = millis();
while(true) {
unsigned long currentMillis = millis();
Serial.println(currentMillis);
// If the current miliseconds run is more than the interval, the delay has been done, so exit.
if(currentMillis - previousMillis > interval) {
return 0;
} else {
// running during delay
int randNum1 = random(300);
int randNum2 = random(300);
int randNumSum = randNum1 + randNum2;
Serial.println("Sum result:");
Serial.println(randNumSum);
// end running during delay
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment