Skip to content

Instantly share code, notes, and snippets.

@GreenMoonArt
Last active June 28, 2018 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GreenMoonArt/33efc2a07e6b72da477eb7414beb8460 to your computer and use it in GitHub Desktop.
Save GreenMoonArt/33efc2a07e6b72da477eb7414beb8460 to your computer and use it in GitHub Desktop.
Concurrent blinkind LEDs using millis()
//answering a question from StackExchange:
//https://arduino.stackexchange.com/questions/53976/too-few-arguments-for-multiple-if-statement-conditions/53979#53979
// Blue LED blinks
// Red LED should go on if Blue LED is ON and switch/button is in correct posistion
// Concurrent LED blinking code from here: https://learn.adafruit.com/multi-tasking-the-arduino-part-1/using-millis-for-timing
int redLEDPin = 7;
int blueLEDPin = 3;
int redLEDtiming = 250;
int blueLEDtiming = 2000;
int leverR=2;
long previousMillisBlue = 0;
long previousMillisRed = 0;
bool ledStateBlue = LOW;
bool ledStateRed = LOW;
void setup() {
pinMode(redLEDPin, OUTPUT);
pinMode(blueLEDPin, OUTPUT);
pinMode(leverR, INPUT_PULLUP);
}
void loop() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillisBlue > blueLEDtiming)
{
previousMillisBlue = currentMillis;
ledStateBlue = !ledStateBlue;
digitalWrite(blueLEDPin, ledStateBlue);
}
//only blink Red LED if Blue LED is also on
if(digitalRead(leverR) == HIGH && ledStateBlue)
{
if(currentMillis - previousMillisRed > redLEDtiming)
{
previousMillisRed = currentMillis;
ledStateRed = !ledStateRed;
digitalWrite(redLEDPin, ledStateRed);
}
}
else
{digitalWrite(redLEDPin, LOW);}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment