1
\$\begingroup\$

I've tried to google this and failed, but I am trying to figure out if I can use a momentary push button and some controller like an Arduino UNO (which I have) to activate two LED lights, one after the other. My example, say I push the button, the blue LED lights up first for 5 seconds, and right after it shuts off, a red LED lights up for 3 seconds then stops, and all done with a single push.

If I want to do it again, I simply press the button again. Any links or advice would be most appreciated. I am very new to the world of controllers, so I likely am using the wrong wording to do proper searching.

\$\endgroup\$
6
  • 4
    \$\begingroup\$ Just write a program to do whatever you want after the button is pressed. \$\endgroup\$
    – Hearth
    Jan 13, 2022 at 17:01
  • 7
    \$\begingroup\$ Of course! But this isn't really an electronics question - it's a programming question more suited to arduino.stackexchange.com. \$\endgroup\$
    – Transistor
    Jan 13, 2022 at 17:01
  • \$\begingroup\$ :| I see wrong forum, thanks for the redirect! \$\endgroup\$ Jan 13, 2022 at 18:15
  • 1
    \$\begingroup\$ The question has a false assumption and is not about doing multiple things from one button. It's really about one button doing one thing, and the one thing it does is to just run a sequence of commands to have two LEDs light up in a sequence. \$\endgroup\$
    – Justme
    Jan 13, 2022 at 19:25
  • 1
    \$\begingroup\$ @MicroservicesOnDDD yup and thanks for the reminder, solution below worked perfectly. \$\endgroup\$ Jan 18, 2022 at 22:13

1 Answer 1

2
\$\begingroup\$

Yes, it can be done easily.

Supposing you connect a normally open button between ground and pin 4 of Arduino, your blue LED with a 330 ohm resistor in series between pin 5 and ground, and your red LED with a 330 ohm resistor in series between pin 6 and ground.

Then the following program should work and match your example (not tested, but it compiles):

#define PIN_BUTTON 4
#define PIN_LED_BLUE 5
#define PIN_LED_RED 6

void setup() {
    // put your setup code here, to run once:
    pinMode(PIN_BUTTON, INPUT_PULLUP);
    pinMode(PIN_LED_BLUE, OUTPUT);
    pinMode(PIN_LED_RED, OUTPUT);
    digitalWrite(PIN_LED_BLUE, LOW);
    digitalWrite(PIN_LED_RED, LOW);
}

void loop() {
    // put your main code here, to run repeatedly:

    if(digitalRead(PIN_BUTTON)==0)  //button pressed
    {
        digitalWrite(PIN_LED_BLUE, HIGH);    //turn on blue led
        delay(5000);    //wait for 5000ms = 5s
        digitalWrite(PIN_LED_BLUE, LOW);    //turn off blue led

        digitalWrite(PIN_LED_RED, HIGH);    //turn on red led
        delay(3000);    //wait for 3000ms = 3s
        digitalWrite(PIN_LED_RED, LOW);    //turn off red led
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ wow, Ill give it a shot and report back, thank you for that! \$\endgroup\$ Jan 13, 2022 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.