#include <Arduino.h>
#include <driver/ledc.h>
#define BUTTON_PIN 0
#define POTENTIOMETER_PIN 4
#define LED_RED_PIN 21
#define LED_GREEN_PIN 19
#define LED_BLUE_PIN 18
const int PWM_FREQUENCY = 5000;
const int PWM_RESOLUTION = 8;
const int RED_CHANNEL = 0;
const int GREEN_CHANNEL = 1;
const int BLUE_CHANNEL = 2;
void updateMode1();
void updateMode2();
void updateMode3();
void updateMode4();
bool lastButtonState = HIGH;
int currentMode = 0;
unsigned long lastModeChangeTime = 0;
unsigned long lastColorChangeTime = 0;
void setup()
{
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(POTENTIOMETER_PIN, INPUT);
ledcSetup(RED_CHANNEL, PWM_FREQUENCY, PWM_RESOLUTION);
ledcSetup(GREEN_CHANNEL, PWM_FREQUENCY, PWM_RESOLUTION);
ledcSetup(BLUE_CHANNEL, PWM_FREQUENCY, PWM_RESOLUTION);
ledcAttachPin(LED_RED_PIN, RED_CHANNEL);
ledcAttachPin(LED_GREEN_PIN, GREEN_CHANNEL);
ledcAttachPin(LED_BLUE_PIN, BLUE_CHANNEL);
Serial.begin(115200);
}
void loop()
{
bool currentButtonState = digitalRead(BUTTON_PIN);
if (lastButtonState == HIGH && currentButtonState == LOW && millis() - lastModeChangeTime > 200)
{
currentMode = (currentMode + 1) % 4;
lastModeChangeTime = millis();
}
lastButtonState = currentButtonState;
switch (currentMode)
{
case 0:
updateMode1();
break;
case 1:
updateMode2();
break;
case 2:
updateMode3();
break;
case 3:
updateMode4();
break;
}
delay(50);
}
void updateMode1()
{
int sensorValue = analogRead(POTENTIOMETER_PIN);
int brightness = map(sensorValue, 0, 4095, 0, 255);
ledcWrite(RED_CHANNEL, 255 - brightness);
ledcWrite(GREEN_CHANNEL, 255 - brightness);
ledcWrite(BLUE_CHANNEL, 255 - brightness);
}
void updateMode2()
{
int sensorValue = analogRead(POTENTIOMETER_PIN);
int brightness = map(sensorValue, 0, 4095, 0, 255);
ledcWrite(RED_CHANNEL, 255 - map(brightness, 0, 255, 0, 255));
ledcWrite(GREEN_CHANNEL, 255 - map(brightness, 0, 255, 0, 153));
ledcWrite(BLUE_CHANNEL, 255 - map(brightness, 0, 255, 0, 51));
}
void updateMode3() {
int speedControl = map(analogRead(POTENTIOMETER_PIN), 0, 4095, 1, 10);
static int colorPhase = 0;
static int colorValue = 0;
colorValue += speedControl;
switch (colorPhase) {
case 0:
ledcWrite(RED_CHANNEL, 255 - colorValue);
ledcWrite(GREEN_CHANNEL, 255 - (255 - colorValue));
ledcWrite(BLUE_CHANNEL, 255);
if (colorValue >= 255) {
colorPhase = 1;
colorValue = 0;
}
break;
case 1:
ledcWrite(RED_CHANNEL, 255);
ledcWrite(GREEN_CHANNEL, 255 - colorValue);
ledcWrite(BLUE_CHANNEL, 255 - (255 - colorValue));
if (colorValue >= 255) {
colorPhase = 2;
colorValue = 0;
}
break;
case 2:
ledcWrite(RED_CHANNEL, 255 - (255 - colorValue));
ledcWrite(GREEN_CHANNEL, 255);
ledcWrite(BLUE_CHANNEL, 255 - colorValue);
if (colorValue >= 255) {
colorPhase = 0;
colorValue = 0;
}
break;
}
}
void updateMode4()
{
int sensorValue = analogRead(POTENTIOMETER_PIN);
int brightness = map(sensorValue, 0, 4095, 0, 30);
ledcWrite(RED_CHANNEL, 255 - map(brightness, 0, 30, 255, 255));
ledcWrite(GREEN_CHANNEL, 255 - map(brightness, 0, 30,229, 178));
ledcWrite(BLUE_CHANNEL, 255 - map(brightness, 0, 30, 204, 102));
}