|
/* |
|
* A simple control panel for kids of all ages to play with. |
|
* Built from WS2811 pixels and excess switches, buttons, and knobs from the ewaste bins! |
|
* Written by Dana Sniezko, Spring 2023. |
|
* |
|
*/ |
|
|
|
#include <FastLED.h> |
|
|
|
FASTLED_USING_NAMESPACE |
|
|
|
// define all buttons |
|
#define R_BTN 13 |
|
#define G_BTN 12 |
|
#define B_BTN 14 |
|
#define Y_BTN 27 |
|
#define U_BTN 26 |
|
#define D_BTN 25 |
|
#define KNOB 34 |
|
|
|
// LED SETUP |
|
#define LED_PIN 15 |
|
#define LED_TYPE WS2811 |
|
#define COLOR_ORDER RGB |
|
#define NUM_LEDS 5 |
|
CRGB leds[NUM_LEDS]; |
|
|
|
#define BRIGHTNESS 96 |
|
#define FRAMES_PER_SECOND 100 |
|
#define GHUE_LONG 1000 |
|
int currentPattern = 0; |
|
int gHueDelay = 30; |
|
|
|
int pins[] = {R_BTN,G_BTN,B_BTN,Y_BTN,U_BTN,D_BTN}; |
|
int pinNum = -1; |
|
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled |
|
unsigned long debounceDelay = 75; // the debounce time; increase if the output flickers |
|
int potValue = 30; |
|
|
|
unsigned long lastStep = 0; |
|
|
|
void setup() { |
|
|
|
delay(20); // 200 ms delay |
|
|
|
// tell FastLED about the LED strip configuration |
|
FastLED.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); |
|
|
|
// set master brightness control |
|
FastLED.setBrightness(BRIGHTNESS); |
|
|
|
Serial.begin(115200); |
|
Serial.println("Here we go!"); |
|
|
|
// setup buttons with internal pull up resistor |
|
pinMode(R_BTN, INPUT_PULLUP); // R |
|
pinMode(G_BTN, INPUT_PULLUP); // G |
|
pinMode(B_BTN, INPUT_PULLUP); // B |
|
pinMode(Y_BTN, INPUT_PULLUP); // Y |
|
pinMode(U_BTN, INPUT_PULLUP); // + |
|
pinMode(D_BTN, INPUT_PULLUP); // - |
|
} |
|
|
|
typedef void (*SimplePatternList[])(); |
|
SimplePatternList gPatterns = { rainbow, rainbowWithGlitter, confetti, sinelon, juggle, bpm }; |
|
|
|
uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current |
|
uint8_t gHue = 0; // rotating "base color" used by many of the patterns |
|
|
|
|
|
void loop() { |
|
// current time |
|
unsigned long currentMillis = millis(); // Update current time |
|
|
|
// do button check behavior |
|
if ((currentMillis - lastDebounceTime) > debounceDelay) { |
|
pinNum = checkPins(pins); |
|
if(pinNum != -1) { |
|
Serial.print("You pressed button on pin: "); |
|
Serial.println(pinNum); |
|
lastDebounceTime = currentMillis; |
|
} |
|
|
|
} |
|
|
|
// set the pattern |
|
choosePattern(pinNum); |
|
gPatterns[currentPattern](); |
|
//Serial.print("Current Pattern:"); |
|
//Serial.println(currentPattern); |
|
|
|
// send the 'leds' array out to the actual LED strip |
|
FastLED.show(); |
|
// insert a delay to keep the framerate modest |
|
FastLED.delay(1000/FRAMES_PER_SECOND); |
|
|
|
potValue= filter(potValue,map(analogRead(KNOB), 0, 4095, 10, 60)); |
|
//Serial.print("Knob says:"); |
|
//Serial.println(potValue); |
|
|
|
if(currentPattern == 3) { |
|
EVERY_N_MILLISECONDS( 1000 ) { gHue++; } |
|
} else { |
|
EVERY_N_MILLISECONDS( potValue ) { gHue++; } // slowly cycle the "base color" through the rainbow |
|
} |
|
|
|
|
|
} |
|
|
|
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0])) |
|
|
|
void nextPattern() |
|
{ |
|
// add one to the current pattern number, and wrap around at the end |
|
currentPattern = (currentPattern + 1) % ARRAY_SIZE( gPatterns); |
|
} |
|
|
|
void choosePattern(int pin) { |
|
switch (pin) { |
|
case D_BTN: |
|
currentPattern = 0; // Rainbow-enhanced theaterChase variant |
|
break; |
|
case U_BTN: |
|
nextPattern(); // Flowing rainbow cycle along the whole strip |
|
break; |
|
case R_BTN: |
|
gHue = 0; |
|
currentPattern = 3; |
|
break; |
|
case Y_BTN: |
|
gHue = 60; |
|
currentPattern = 3; |
|
break; |
|
case B_BTN: |
|
gHue = 155; |
|
currentPattern = 3; |
|
break; |
|
case G_BTN: |
|
gHue = 95; |
|
currentPattern = 3; |
|
break; |
|
default: |
|
break; |
|
} |
|
} |
|
|
|
int checkPins(int pins[]) { |
|
for (int i = 0; i < 6; i++) { |
|
if (digitalRead(pins[i]) == LOW) { |
|
return pins[i]; |
|
} |
|
} |
|
return -1; |
|
} |
|
|
|
int filter(int oldVal,int newVal) { |
|
if (abs(oldVal-newVal) > 4) { |
|
return newVal; |
|
} else { |
|
return oldVal; |
|
} |
|
} |
|
|
|
|
|
void rainbow() |
|
{ |
|
// FastLED's built-in rainbow generator |
|
fill_rainbow( leds, NUM_LEDS, gHue, 7); |
|
} |
|
|
|
void rainbowWithGlitter() |
|
{ |
|
// built-in FastLED rainbow, plus some random sparkly glitter |
|
rainbow(); |
|
addGlitter(20); |
|
} |
|
|
|
void addGlitter( fract8 chanceOfGlitter) |
|
{ |
|
if( random8() < chanceOfGlitter) { |
|
leds[ random16(NUM_LEDS) ] += CRGB::White; |
|
} |
|
} |
|
|
|
void confetti() |
|
{ |
|
// random colored speckles that blink in and fade smoothly |
|
fadeToBlackBy( leds, NUM_LEDS, 10); |
|
int pos = random16(NUM_LEDS); |
|
leds[pos] += CHSV( gHue + random8(64), 200, 255); |
|
} |
|
|
|
void sinelon() |
|
{ |
|
// a colored dot sweeping back and forth, with fading trails |
|
fadeToBlackBy( leds, NUM_LEDS, 20); |
|
int pos = beatsin16(potValue, 0, NUM_LEDS-1 ); |
|
leds[pos] += CHSV( gHue, 255, 192); |
|
} |
|
|
|
void bpm() |
|
{ |
|
// colored stripes pulsing at a defined Beats-Per-Minute (BPM) |
|
uint8_t BeatsPerMinute = 62; |
|
CRGBPalette16 palette = PartyColors_p; |
|
uint8_t beat = beatsin8( BeatsPerMinute, 64, 255); |
|
for( int i = 0; i < NUM_LEDS; i++) { //9948 |
|
leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10)); |
|
} |
|
} |
|
|
|
void juggle() { |
|
// eight colored dots, weaving in and out of sync with each other |
|
fadeToBlackBy( leds, NUM_LEDS, 20); |
|
byte dothue = 0; |
|
for( int i = 0; i < 8; i++) { |
|
leds[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255); |
|
dothue += 32; |
|
} |
|
} |
|
|
|
|
|
int checkButtons() { |
|
for(int i=0; i < 5; i++) { |
|
boolean buttonState = digitalRead(i); |
|
if(buttonState) { |
|
return i; |
|
} |
|
} |
|
} |