const int redPin = 15;
const int greenPin = 2;
const int bluePin = 4;
const int potPin = 14;
const int buttonPin = 13;
int redBrightness = 0;
int greenBrightness = 1;
int blueBrightness = 2;
int currentColor = 0; // 0: Red, 1: Green, 2: Blue
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
int potValue = analogRead(potPin); // Read the potentiometer value
int buttonState = digitalRead(buttonPin); // Read the button state
// Map the potentiometer value to LED brightness (0-255)
int brightness = map(potValue, 0, 1023, 0, 255);
// Check if the button is pressed and change the LED color
if (buttonState == LOW) {
currentColor = (currentColor + 1) % 3; // Cycle through Red, Green, Blue
delay(250); // Debounce delay
}
// Set LED brightness based on the current color
if (currentColor == 0) {
redBrightness = brightness;
} else if (currentColor == 1) {
greenBrightness = brightness;
} else if (currentColor == 2) {
blueBrightness = brightness;
}
// Update the LED brightness using the LEDC library (PWM)
ledcSetup(0, 5000, 8); // PWM frequency = 5 kHz, 8-bit resolution
ledcAttachPin(redPin, 0);
ledcWrite(0, redBrightness);
ledcSetup(1, 5000, 8);
ledcAttachPin(greenPin, 1);
ledcWrite(1, greenBrightness);
ledcSetup(2, 5000, 8);
ledcAttachPin(bluePin, 2);
ledcWrite(2, blueBrightness);
}