// Define RGB LED pins
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
// Define button pin
const int buttonPin = 2;
int buttonState = 0; // Store the current state of the button
int previousButtonState = 0; // Store the previous state of the button
void setup() {
// Set the RGB LED pins as OUTPUT
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
// Set the button pin as INPUT
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
// Read the state of the button
buttonState = digitalRead(buttonPin);
// If the button is pressed, change the LED color
if (buttonState == LOW && previousButtonState == HIGH) {
// Change the LED color
changeColor();
}
// Update the previous button state
previousButtonState = buttonState;
}
// Function to change the RGB LED color
void changeColor() {
// Generate a random color
int redValue = random(256);
int greenValue = random(256);
int blueValue = random(256);
// Set the RGB LED colors
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
delay(500); // Delay to prevent rapid color changes
}