#include <Arduino.h>
#include <FastLED.h>
// Pin Definitions
#define RED_PIN 2
#define GREEN_PIN 0
#define BLUE_PIN 4
#define BUTTON_PIN 17
#define POT_PIN 18
// LED Configuration
#define NUM_LEDS 1
CRGB leds[NUM_LEDS];
// Global Variables
int mode = 1; // Current mode
bool buttonPressed = false; // Button state
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50;
// Function Declarations
void handleButtonPress();
void updateMode();
void whiteLightMode();
void warmLightMode();
void rgbMoodMode();
void nightLightMode();
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(POT_PIN, INPUT);
// Initialize FastLED
FastLED.addLeds<WS2812, RED_PIN, GRB>(leds, NUM_LEDS);
// Start in mode 1
whiteLightMode();
}
void loop() {
handleButtonPress();
switch (mode) {
case 1:
whiteLightMode();
break;
case 2:
warmLightMode();
break;
case 3:
rgbMoodMode();
break;
case 4:
nightLightMode();
break;
}
FastLED.show();
}
void handleButtonPress() {
int buttonState = digitalRead(BUTTON_PIN);
if (buttonState == LOW && !buttonPressed) {
unsigned long currentTime = millis();
if (currentTime - lastDebounceTime > debounceDelay) {
buttonPressed = true;
updateMode();
lastDebounceTime = currentTime;
}
} else if (buttonState == HIGH) {
buttonPressed = false;
}
}
void updateMode() {
mode++;
if (mode > 4) {
mode = 1;
}
}
void whiteLightMode() {
int potValue = analogRead(POT_PIN);
int brightness = map(potValue, 0, 4095, 0, 255);
leds[0] = CRGB(brightness, brightness, brightness);
}
void warmLightMode() {
int potValue = analogRead(POT_PIN);
int brightness = map(potValue, 0, 4095, 0, 255);
leds[0] = CRGB(brightness, brightness * 0.7, brightness * 0.4);
}
void rgbMoodMode() {
static uint8_t hue = 0;
int potValue = analogRead(POT_PIN);
int speed = map(potValue, 0, 4095, 1, 50);
leds[0] = CHSV(hue, 255, 255);
hue += speed;
delay(20);
}
void nightLightMode() {
int potValue = analogRead(POT_PIN);
int brightness = map(potValue, 0, 4095, 0, 77); // 30% of 255 = 77
leds[0] = CRGB(brightness, brightness * 0.4, 0);
}