Controlling 2 LEDs with 1 pushbutton(Beginner)

Hi,

I want to use 2 LEDs and a pushbutton and program them in such that they have a sequence that the LEDs will go through. I am using an Arduino UNO. I have already model up the circuit in TinkerCAD.

Here is the sequence I want to get:
1st press:Only RED LED turn on
2nd press: Only GREEN LED turn on
3rd press: Both LED turn on
4th press: Both LED turn off

I'm not sure how to code it as I am quite new to Arduino and programming in general. Thank you in advance!

You keep track of which state your are in by using a variable - maybe call it 'state' and then every time you detect a button press. you increment 'state' and set the LEDs on/off. When it gets to the last state, it rolls over to 0.

kraviee:
Hi,

I want to use 2 LEDs and a pushbutton and program them in such that they have a sequence that the LEDs will go through. I am using an Arduino UNO. I have already model up the circuit in TinkerCAD.

Here is the sequence I want to get:
1st press:Only RED LED turn on
2nd press: Only GREEN LED turn on
3rd press: Both LED turn on
4th press: Both LED turn off

I'm not sure how to code it as I am quite new to Arduino and programming in general. Thank you in advance!

Also since you are counting button presses make sure you check for a change in the button state and not the button state itself. Otherwise you will be very frustrated. The following tutorial will be very close to what you want to do:

StateChangeDetection

Here is an update of the State Change Detection tutorial that uses an active LOW (wired to ground with pullup) switch configuration.

Thank you all for the advice, will try them out. Wish me luck!

you don't need luck. You need some patience to learn programming.
best regards Stefan

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.