Skip to content

Instantly share code, notes, and snippets.

@wyojustin
Created October 11, 2020 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wyojustin/a137109f22874756fb55fbb86e03b3eb to your computer and use it in GitHub Desktop.
Save wyojustin/a137109f22874756fb55fbb86e03b3eb to your computer and use it in GitHub Desktop.
#include "FastLED.h"
FASTLED_USING_NAMESPACE
// FastLED "100-lines-of-code" demo reel, showing just a few
// of the kinds of animation patterns you can quickly and easily
// compose using FastLED.
//
// This example also shows one easy way to define multiple
// animations patterns and have them automatically rotate.
//
// -Mark Kriegsman, December 2014
#if defined(FASTLED_VERSION) && (FASTLED_VERSION < 3001000)
#warning "Requires FastLED 3.1 or later; check github for latest code."
#endif
#define DATA_PIN A5
#define LED_TYPE WS2812
//#define CLK_PIN 19
//#define LED_TYPE APA102
#define COLOR_ORDER GRB
#define NUM_LEDS 31
CRGB leds[NUM_LEDS];
#define BRIGHTNESS 255
#define FRAMES_PER_SECOND 120
void setup() {
Serial.begin(115200);
Serial.println("WyoLum!");
delay(300);
// tell FastLED about the LED strip configuration
FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).
setCorrection(TypicalLEDStrip);
// set master brightness control
FastLED.setBrightness(BRIGHTNESS);
FastLED.setMaxPowerInMilliWatts(2000);
pinMode(A0, INPUT_PULLUP);
pinMode(A1, OUTPUT);
digitalWrite(A1, LOW);
}
// List of patterns to cycle through. Each is defined as a separate function below.
typedef void (*SimplePatternList[])();
void blue();
void green();
void red();
void white();
void rainbow();
void rainbowWithGlitter();
void sinelon();
void juggle();
void bpm();
void pattern_cycle();
void nextPattern();
void off();
void setOff();
void addGlitter(fract8 chanceOfGlitter);
SimplePatternList gPatterns = {white, blue, green, red,
rainbow, rainbowWithGlitter, sinelon, juggle, bpm,
pattern_cycle};
int8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
int8_t gLastPatternNumber = 0; // Index number of which pattern is current
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
uint32_t last_press = 0;
#define DEBOUNCE 200
void loop()
{
// Call the current pattern function once, updating the 'leds' array
if(gCurrentPatternNumber >= 0){
gPatterns[gCurrentPatternNumber]();
}
// 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);
// do some periodic updates
EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
//EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically
if(millis() - last_press > DEBOUNCE && digitalRead(A0) == LOW){
nextPattern();
gPatterns[gCurrentPatternNumber]();
FastLED.show();
uint32_t press_start = millis();
uint32_t press_dur = millis() - press_start;
while(digitalRead(A0) == LOW && press_dur < 3000){
press_dur = millis() - press_start;
}
if(press_dur >= 3000){
setOff();
off();
FastLED.show();
Serial.println("Long Press");
}
else{
Serial.println("normal press");
}
Serial.println("Wait for relase");
while(digitalRead(A0) == LOW){
// insure release
}
last_press = millis();
}
}
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
void setPattern(int num){
gCurrentPatternNumber = num % (ARRAY_SIZE(gPatterns));
}
void setOff(){
gLastPatternNumber = (gCurrentPatternNumber - 1) % (ARRAY_SIZE(gPatterns));
gCurrentPatternNumber = -1;
}
void nextPattern(){
// add one to the current pattern number, and wrap around at the end
if(gCurrentPatternNumber < 0){
gCurrentPatternNumber = gLastPatternNumber;
}
else{
setPattern(gCurrentPatternNumber + 1);
}
Serial.print("CurrentPatternNumber: ");
Serial.println(gCurrentPatternNumber);
}
int hue = 0;
int divisor = 30;
#define MIN_BRIGHTNESS 8
#define MAX_BRIGHTNESS 255
void breath () {
float breath = (exp(sin(millis()/5000.0*PI)) - 0.36787944)*108.0;
breath = map(breath, 0, 255, MIN_BRIGHTNESS, MAX_BRIGHTNESS);
FastLED.setBrightness(breath);
fill_rainbow(leds, NUM_LEDS, (hue++/divisor));
if(hue == (255 * divisor)) {
hue = 0;
}
delay(5);
}
int pattern_cycle_num = 0;
void pattern_cycle(){
gPatterns[pattern_cycle_num]();
EVERY_N_SECONDS( 10 ) {
pattern_cycle_num += 1; // last two are cycle and off
pattern_cycle_num %= ARRAY_SIZE(gPatterns) - 2;
} // change patterns periodically
}
void off(){
for(int i = 0; i < NUM_LEDS; i++){
leds[i] = CRGB::Black;
FastLED.show();
}
}
void mona(){
for(int i = 0; i < NUM_LEDS; i++){
leds[i] = CRGB::Black;
}
for(int i = 8; i < 16; i++){
leds[i] = CRGB::Red;
}
for(int i = 16; i < 24; i++){
leds[i] = CRGB::Green/8;
}
FastLED.show();
}
void blue(){
for(int i = 0; i < NUM_LEDS; i++){
leds[i] = CRGB::Blue;
FastLED.show();
}
}
void red(){
for(int i = 0; i < NUM_LEDS; i++){
leds[i] = CRGB::Red;
FastLED.show();
}
}
void white(){
for(int i = 0; i < NUM_LEDS; i++){
leds[i] = CRGB::White;
FastLED.show();
}
}
void green(){
for(int i = 0; i < NUM_LEDS; i++){
leds[i] = CRGB::Green;
FastLED.show();
}
}
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(80);
}
void addGlitter( fract8 chanceOfGlitter)
{
if( random8() < chanceOfGlitter) {
leds[ random16(NUM_LEDS) ] += CRGB::White;
}
}
void sinelon()
{
// a colored dot sweeping back and forth, with fading trails
fadeToBlackBy( leds, NUM_LEDS, 20);
int pos = beatsin16( 13, 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment