#include <FastLED.h>
#define LED_PIN 2 // Pin connected to the LED strip
#define NUM_LEDS 16 // Number of LEDs in the strip
#define PATTERN_COUNT 8 // Number of light patterns
#define BRIGHTNESS_LEVELS 5 // Number of brightness levels
CRGB leds[NUM_LEDS];
// Pattern variables
int currentPattern = 0;
int currentBrightness = 254; // Maximum brightness initially
// Button variables
int patternButtonPin = 3; // Pin connected to the pattern change button
int brightnessButtonPin = 4; // Pin connected to the brightness change button
bool patternButtonState = false;
bool brightnessButtonState = false;
bool lastPatternButtonState = false;
bool lastBrightnessButtonState = false;
void setup() {
pinMode(patternButtonPin, INPUT_PULLUP);
pinMode(brightnessButtonPin, INPUT_PULLUP);
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(currentBrightness);
}
void loop() {
// Read the button states
patternButtonState = digitalRead(patternButtonPin);
brightnessButtonState = digitalRead(brightnessButtonPin);
// Change pattern if the pattern button is pressed
if (patternButtonState && !lastPatternButtonState) {
currentPattern = (currentPattern + 1) % PATTERN_COUNT;
clearLEDs();
}
// Change brightness if the brightness button is pressed
if (brightnessButtonState && !lastBrightnessButtonState) {
currentBrightness = map(currentBrightness, 0, 255, 0, 255 / BRIGHTNESS_LEVELS);
currentBrightness = (currentBrightness + 1) % (255 / BRIGHTNESS_LEVELS + 1);
currentBrightness = map(currentBrightness, 0, 255 / BRIGHTNESS_LEVELS, 0, 255);
FastLED.setBrightness(currentBrightness);
}
// Execute the current pattern
switch (currentPattern) {
case 0:
patternChasingPurple();
break;
case 1:
patternChasingRainbow();
break;
case 2:
patternStationaryRed();
break;
case 3:
patternStationaryWhite();
break;
case 4:
patternStationaryPurple();
break;
case 5:
patternChasingRedWhiteBlue();
break;
case 6:
patternVUMeter();
break;
case 7:
patternCycleSpectrum();
break;
case 8:
patternShortCircuit();
break;
// Add more patterns as needed
}
// Store the button states for the next loop
lastPatternButtonState = patternButtonState;
lastBrightnessButtonState = brightnessButtonState;
// Show the LED pattern
FastLED.show();
}
void clearLEDs() {
memset(leds, 0, NUM_LEDS * sizeof(CRGB));
}
// Example patterns (you can modify these or add your own)
void patternChasingPurple() {
static int pos = 0;
clearLEDs();
leds[pos] = CRGB::Purple;
pos = (pos + 1) % NUM_LEDS;
delay(50);
}
void patternChasingRainbow() {
static int pos = 0;
clearLEDs();
for (int i = 0; i < 3; i++) {
leds[(pos + i) % NUM_LEDS] = CHSV(i * 85, 255, 255);
}
pos = (pos + 1) % NUM_LEDS;
delay(50);
}
void patternStationaryRed() {
clearLEDs();
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Red;
}
delay(100);
}
void patternStationaryWhite() {
clearLEDs();
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::White;
}
delay(100);
}
void patternStationaryPurple() {
clearLEDs();
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Purple;
}
delay(100);
}
void patternChasingRedWhiteBlue() {
static int pos = 0;
clearLEDs();
for (int i = 0; i < 3; i++) {
if ((pos + i) % 3 == 0) {
leds[(pos + i) % NUM_LEDS] = CRGB::Red;
} else if ((pos + i) % 3 == 1) {
leds[(pos + i) % NUM_LEDS] = CRGB::White;
} else {
leds[(pos + i) % NUM_LEDS] = CRGB::Blue;
}
}
pos = (pos + 1) % NUM_LEDS;
delay(50);
}
void patternVUMeter() {
static int pos = 0;
clearLEDs();
int level = map(analogRead(A0), 0, 1023, 0, NUM_LEDS);
for (int i = 0; i < level; i++) {
leds[i] = CRGB::Green;
}
pos = (pos + 1) % NUM_LEDS;
delay(50);
}
void patternCycleSpectrum() {
static int hue = 0;
clearLEDs();
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(hue, 255, 255);
hue += 5;
}
delay(50);
}
void patternShortCircuit() {
clearLEDs();
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Red;
delay(10);
leds[i] = CRGB::Black;
}
delay(200);
}
// Add more patterns as needed