#include <FastLED.h>
#include <Keypad.h>
#define NUM_LEDS 5
#define DATA_PIN 8
#define POTENTIOMETER_PIN A0
CRGB leds[NUM_LEDS];
int currentEffect = 0;
int animationSpeed = 100;
int brightness = 255; // Default brightness
const byte ROWS = 3;
const byte COLS = 3;
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'}
};
byte rowPins[ROWS] = {2, 3, 4};
byte colPins[COLS] = {5, 6, 7};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setAll(CRGB color) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = color;
}
FastLED.show();
}
void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
pinMode(POTENTIOMETER_PIN, INPUT);
FastLED.setBrightness(brightness);
}
void loop() {
int potValue = analogRead(POTENTIOMETER_PIN);
animationSpeed = map(potValue, 0, 1023, 10, 1000);
char key = keypad.getKey();
if (key) {
currentEffect = key - '1';
}
switch (currentEffect) {
case 0:
// Effect 1: Color Wipe
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Red;
FastLED.show();
delay(animationSpeed);
leds[i] = CRGB::Black;
}
break;
case 1:
// Effect 2: Rainbow
static uint8_t hue = 0;
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(hue + (i * 256 / NUM_LEDS), 255, 255);
}
FastLED.show();
hue += 5;
delay(20);
break;
case 2:
// Effect 3: Sparkling
for (int i = 0; i < NUM_LEDS; i++) {
if (random(10) < 5) {
leds[i] = CRGB::White;
} else {
leds[i] = CRGB::Black;
}
}
FastLED.show();
delay(animationSpeed);
break;
case 3:
// Effect 4: Color Cycle
static uint8_t colorCycleHue = 0;
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(colorCycleHue, 255, 255);
}
FastLED.show();
colorCycleHue++;
delay(animationSpeed);
break;
case 4:
// Effect 5: Police Lights (Red and Blue)
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Red;
}
FastLED.show();
delay(animationSpeed);
setAll(CRGB::Black);
delay(animationSpeed);
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Blue;
}
FastLED.show();
delay(animationSpeed);
setAll(CRGB::Black);
delay(animationSpeed);
break;
case 5:
// Effect 6: Bouncing Ball
int ballPos = 0;
int ballDirection = 1;
int ballColor = random(256);
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Black;
}
while (ballPos >= 0 && ballPos < NUM_LEDS) {
leds[ballPos] = CHSV(ballColor, 255, 255);
FastLED.show();
delay(animationSpeed);
leds[ballPos] = CRGB::Black;
ballPos += ballDirection;
if (ballPos == NUM_LEDS || ballPos == -1) {
ballDirection *= -1;
ballColor = random(256);
}
}
break;
case 6:
// Effect 7: Comet
int cometPos = 0;
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Black;
}
while (cometPos < NUM_LEDS) {
leds[cometPos] = CRGB::Red;
FastLED.show();
delay(animationSpeed);
leds[cometPos] = CRGB::Black;
cometPos++;
}
break;
case 7:
// Effect 8: Theater Chase
for (int j = 0; j < 3; j++) {
for (int q = 0; q < 3; q++) {
for (int i = 0; i < NUM_LEDS; i = i + 3) {
leds[i + q] = CRGB::Red;
}
FastLED.show();
delay(animationSpeed);
for (int i = 0; i < NUM_LEDS; i = i + 3) {
leds[i + q] = CRGB::White;
}
}
}
break;
case 8:
// Effect 9: Knight Rider (Cylon Scanner)
int eyeSize = 2; // Size of the "eye" in pixels
int direction = 1; // 1 = forward, -1 = backward
int pos = 0;
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Black;
}
while (pos >= 0 && pos < NUM_LEDS - eyeSize) {
for (int i = pos; i < pos + eyeSize; i++) {
leds[i] = CRGB::Red;
}
FastLED.show();
delay(animationSpeed);
for (int i = pos; i < pos + eyeSize; i++) {
leds[i] = CRGB::Black;
}
pos += direction;
}
direction *= -1; // Reverse direction
pos += direction; // Move back
break;
}
}