Turning on multiple LEDs on with one switch individually.

Hi everyone,

I am pretty much brand new to arduino and coding and i am having some trouble with LEDs and i am hoping you guys can help me:)

I have connected 3 LEDs in a series and got as far with the programming so that now when i press the button all three LEDs come on and stay on until i let go of the switch.

What i want to do is:
press button once, first led comes on
press button again, second led comes on
press button third time, third led comes on
all LEDs stay on for 6 seconds before turning off from the last time the button was pressed.

I really hope u guys can help me, i will be waiting,
Thanks! :slight_smile:

Here is the coding i have so far:

const int buttonPin = 2; // pushbutton pin
const int ledPin1 = 13; // LED pin
const int ledPin2 = 12;
const int ledPin3 = 8;

int buttonState = 0; //reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);

pinMode(buttonPin, INPUT);
}

void loop()
{
buttonState = digitalRead(buttonPin); // read the state of the pushbutton value

if (buttonState == HIGH) { // check if the pushbutton is pressed.
digitalWrite(ledPin1, LOW);

}
if (buttonState == HIGH) {
digitalWrite(ledPin2, LOW);
}

if (buttonState == HIGH) {
digitalWrite(ledPin3, LOW);
}

else {

digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
}

}

You need to check that the button becomes high, not just is high. You also need to count the presses, and do things differently on each count.

So, have a look here

PS.... you say:

I have connected 3 LEDs in a series

..... but I assume you don't actually mean in series, but have one per pin?

Good reason to add a pic of the circuit to this thread....

Hi JimboZA and thanks for replying,

Yes, sorry i meant they are connected to three separate pins.

I will attempt to work with the link you provided and let you know how i get on, hopefully it will work lol

Thanks again!