const int buttonR = 5; // Button for Red
const int buttonG = 4; // Button for Green
const int buttonB = 3; // Button for Blue
const int ledR = 11; // Red pin of RGB LED
const int ledG = 10; // Green pin of RGB LED
const int ledB = 9; // Blue pin of RGB LED
int stateR = HIGH; // Current state of the Red button
int stateG = HIGH; // Current state of the Green button
int stateB = HIGH; // Current state of the Blue button
void setup() {
pinMode(buttonR, INPUT_PULLUP);
pinMode(buttonG, INPUT_PULLUP);
pinMode(buttonB, INPUT_PULLUP);
pinMode(ledR, OUTPUT);
pinMode(ledG, OUTPUT);
pinMode(ledB, OUTPUT);
}
void loop() {
// Inside the loop function
Serial.print("Button states: ");
Serial.print("R: ");
Serial.print(stateR);
Serial.print(" G: ");
Serial.print(stateG);
Serial.print(" B: ");
Serial.println(stateB);
// Check the state of the Red button
stateR = digitalRead(buttonR);
if (stateR == LOW) {
// Toggle Red LED
digitalWrite(ledR, !digitalRead(ledR));
delay(50); // Debounce delay
}
// Check the state of the Green button
stateG = digitalRead(buttonG);
if (stateG == LOW) {
// Toggle Green LED
digitalWrite(ledG, !digitalRead(ledG));
delay(50); // Debounce delay
}
// Check the state of the Blue button
stateB = digitalRead(buttonB);
if (stateB == LOW) {
// Toggle Blue LED
digitalWrite(ledB, !digitalRead(ledB));
delay(50); // Debounce delay
}
// If no button is pressed, turn off all LEDs
if (stateR == HIGH && stateG == HIGH && stateB == HIGH) {
digitalWrite(ledR, LOW);
digitalWrite(ledG, LOW);
digitalWrite(ledB, LOW);
}
}