// Define the pins for the RGB LED
int redPin = 5;
int greenPin = 9;
int bluePin = 11;
// Define the pin for the active buzzer
int buzzerPin = 6;
// Define the pin for the push button
int buttonPin = 2;
// Variable to track the current color state
int colorState = 0; // 0 = Red, 1 = Green, 2 = Blue
// Variable to store the last state of the button
int lastButtonState = HIGH;
void setup() {
// Set all component pins as outputs or inputs
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
// Read the current state of the button
int currentButtonState = digitalRead(buttonPin);
// Check if the button has just been pressed
// This logic is a simple way to "debounce" the button
if (currentButtonState == HIGH && lastButtonState == LOW) {
// A button press has occurred, so change the color
changeColor();
// Make the buzzer beep to indicate the color change
beepBuzzer();
// Small delay for debounce
delay(50);
}
// Update the last button state for the next loop iteration
lastButtonState = currentButtonState;
}
// Function to change the RGB LED color
void changeColor() {
// First, turn off all LED colors
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
// Increment the color state and loop back to 0 if it exceeds 2
colorState++;
if (colorState > 2) {
colorState = 0;
}
// Set the new color based on the current state
if (colorState == 0) {
digitalWrite(redPin, HIGH); // Turn on Red
} else if (colorState == 1) {
digitalWrite(greenPin, HIGH); // Turn on Green
} else if (colorState == 2) {
digitalWrite(bluePin, HIGH); // Turn on Blue
}
}
// Function to make the buzzer beep
void beepBuzzer() {
tone(buzzerPin, 255); // Turn the buzzer on
delay(100); // Wait for 100 milliseconds
noTone(buzzerPin); // Turn the buzzer off
}