int buttonPin = 5; // the pin that the button is attached to
int led1Pin = 13; // the pin that LED1 is attached to
int led2Pin = 12; // the pin that LED2 is attached to
int led3Pin = 11; // the pin that LED3 is attached to
int ledState = 0; // variable for keeping track of which LED is on
int buttonState = 0; // variable for reading the button status
int lastButtonState = 0; // variable for storing the previous button state
int buttonPresses = 0; // variable for counting the number of button presses
void setup() {
pinMode(buttonPin, INPUT); // set the button pin as an input
pinMode(led1Pin, OUTPUT); // set the LED1 pin as an output
pinMode(led2Pin, OUTPUT); // set the LED2 pin as an output
pinMode(led3Pin, OUTPUT); // set the LED3 pin as an output
}
void loop() {
buttonState = digitalRead(buttonPin); // read the button state
if (buttonState != lastButtonState) {
if (buttonState == HIGH) { // button is pressed
buttonPresses++;
if (buttonPresses > 3) { // if button pressed more than 3 times, turn off all LEDs
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
ledState = 0; // reset the LED state
buttonPresses = 0; // reset the button presses counter
} else {
if (ledState == 0) { // turn on LED1
digitalWrite(led1Pin, HIGH);
ledState = 1;
} else if (ledState == 1) { // turn off LED1 and turn on LED2
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, HIGH);
ledState = 2;
} else if (ledState == 2) { // turn off LED2 and turn on LED3
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, HIGH);
ledState = 3;
} else if (ledState == 3) { // turn off LED3 and turn on LED1
digitalWrite(led3Pin, LOW);
digitalWrite(led1Pin, HIGH);
ledState = 1;
}
}
}
delay(50); // debounce delay to avoid multiple button presses
}
lastButtonState = buttonState; // store the current button state for comparison in the next iteration
}