const int ledPin9 = 9; // the number of the LED pin
const int ledPin10 = 10; // the number of the LED pin
const unsigned long interval9 = 1000; // interval at which to blink (milliseconds)
const unsigned long interval10 = 500; // interval at which to blink (milliseconds)
unsigned long LED09_Timer; // just a SINGLE variable for non-blocking timing
unsigned long LED10_Timer;
void setup() {
pinMode(ledPin9, OUTPUT); // set the digital pin as output
pinMode(ledPin10, OUTPUT); // set the digital pin as output
LED09_Timer = millis();
LED10_Timer = millis();
}
void loop() {
if ( TimePeriodIsOver(LED09_Timer,interval9) ) {
digitalWrite(ledPin9,!digitalRead(ledPin9) );
}
if ( TimePeriodIsOver(LED10_Timer,interval10) ) {
digitalWrite(ledPin10,!digitalRead(ledPin10) );
}
}
// the attention mark "!" is the NOT-operator which inverts LOW to HIGH and HIGH to LOW
// digitalRead() can be done for outputs too
// and it delivers the actual logic state
// example digitalRead(ledPin9) = LOW
// inverting with the NOT-operator !LOW results in HIGH
// which means digitalWrite(ledPin10,!digitalRead(ledPin10) )
// digitalWrite(ledPin10,!LOW )
// digitalWrite(ledPin10,HIGH )
// which toggles the state of the IO-pin
// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - startOfPeriod >= TimePeriod ) {
// more time than TimePeriod has elapsed since last time if-condition was true
startOfPeriod = currentMillis; // a new period starts right here so set new starttime
return true;
}
else return false; // actual TimePeriod is NOT yet over
}