const int buttonPin = 2; // GPIO for Button
const int redPin = 9; // GPIO for Red LED
const int greenPin = 8; // GPIO for Green LED
const int bluePin = 7; // GPIO for Blue LED
int buttonState = 0; // Current state of the button
int lastButtonState = 0; // Previous state of the button
int mode = 0; // Mode to track which LED is ON (0 = all off, 1 = red, 2 = green, 3 = blue)
void setup() {
// Initialize the button pin as input
pinMode(buttonPin, INPUT);
// Initialize RGB LED pins as outputs
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
// Turn off all LEDs initially
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
}
void loop() {
// Read the current state of the button
buttonState = digitalRead(buttonPin);
// Check if the button was pressed (transition from LOW to HIGH)
if (buttonState == HIGH && lastButtonState == LOW) {
mode++; // Increment mode
if (mode > 3) {
mode = 1; // Cycle through modes 1 to 3 (Red -> Green -> Blue)
}
// Turn off all LEDs before turning on the next one
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
// Based on the mode, turn on the corresponding LED
switch (mode) {
case 1: // First press: Red LED on
digitalWrite(redPin, HIGH);
break;
case 2: // Second press: Green LED on
digitalWrite(greenPin, HIGH);
break;
case 3: // Third press: Blue LED on
digitalWrite(bluePin, HIGH);
break;
}
// Add a small delay to debounce the button
delay(50);
}
// Save the current button state for the next loop
lastButtonState = buttonState;
}