/*
Arduino Button Control for Multiple LEDs
Create an Arduino code that uses 1 button attached to pin 5,
and 3 LED attached to pin 13, 12, and 11. Whenever the button
is pressed, LED1 will light up, and if the button is pressed
again, LED1 will turn off and LED2 will turn on. If the button
is pressed again, LED2 will turn off and LED3 will turn on.
If the button is pressed more than 3 times, all LEDs will turn off.
The LEDs will remain on until another press is received.
*/
int count = 0;
int led[3] = {13, 12, 11};
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(5, INPUT_PULLUP);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
}
void loop() {
ifelsearray();
}
void ifelsearray() {
if (digitalRead(5) == LOW && count <= 3) {
count++;
digitalWrite(led[count-2], LOW);
digitalWrite(led[count-1], HIGH);
//optional
if (count != 4) {
Serial.print("LED ");
Serial.print(count-1);
Serial.println(" is turned on.");
}
delay(200);
}
else if (count >= 4) {
for (count; count != 0; count--) {
digitalWrite(led[count], LOW);
//Optional
if (count < 4) {
Serial.print("LED ");
Serial.print(count-1);
Serial.println(" is turned off.");
}
}
}
}