Skip to content

Instantly share code, notes, and snippets.

@focalintent
Last active January 13, 2016 23:58
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 focalintent/91c7a5b485f8723f7277 to your computer and use it in GitHub Desktop.
Save focalintent/91c7a5b485f8723f7277 to your computer and use it in GitHub Desktop.
#include "FastLED.h"
#include "Bounce2.h"
// how fast to move the dots, in frames per second, adjust this to
// make the dots move faster or slower
#define SPEED 60
// minimum button press time in ms to debounce
#define MIN_BUTTON_PRESS 5
// Change this to adjust how long of a tail a dot will have
#define FADE_RATE 64
#define NUM_LEDS 48
#define NUM_ARMS 4
CRGB leds[NUM_ARMS][NUM_LEDS];
int button_pins[NUM_ARMS] = {3,4,5,6};
Bounce buttons[NUM_ARMS];
// These are the moving dots - we'll store a hue value in each of these
uint8_t dots[NUM_ARMS][NUM_LEDS];
void setup() {
// replace with your actual pin layouts - see https://github.com/FastLED/FastLED/wiki/SPI-Hardware-or-Bit-banging for advice
FastLED.addLeds<APA102,7,8>(leds[0], NUM_LEDS);
FastLED.addLeds<APA102,9,10>(leds[1], NUM_LEDS);
FastLED.addLeds<APA102,11,12>(leds[2], NUM_LEDS);
FastLED.addLeds<APA102,13,14>(leds[3], NUM_LEDS);
// 0 out all the dots
memset(dots, 0, NUM_ARMS * NUM_LEDS);
Serial.begin(57600);
// Setup the buttons - assume the buttons are on pins 3,4,5 & 6
for(int i = 0; i < NUM_ARMS; i++){
pinMode(button_pins[i], INPUT_PULLUP);
buttons[i].attach(button_pins[i]);
buttons[i].interval(MIN_BUTTON_PRESS);
}
}
void loop() {
// update button states in Bounce2 objects
for(int i = 0; i < 4; i++) { buttons[i].update(); }
// Now, check if any buttons are pressed, if so - put a new dot on the line!
for(int r = 0; r < NUM_ARMS; r++) {
// Has the button been pressed? (note: this only changes when the button is actually pushed, it won't
// return true again until the button is released and pressed again)
if(buttons[r].fell()) {
// yay, button pressed! New dot for this row! Let's pick a random hue from
// 1-255 for the value
dots[r][0] = 1 + random8(254);
}
}
// 60 times a second, this will move dots down the line with fading tails
EVERY_N_MILLISECONDS(1000/SPEED) {
Serial.println("frame...");
// Fade anything currently on the leds
fadeToBlackBy((CRGB*)leds, NUM_ARMS * NUM_LEDS, 64);
// Draw all the currently defined dots
for(int r = 0 ; r < NUM_ARMS; r++ ) {
for(int i = 0; i < NUM_LEDS; i++) {
if(dots[r][i] != 0) {
leds[r][i] += CHSV(dots[r][i], 192, 255);
}
}
}
// Now, "move" all the dots down the line. We're going to cheat and use memmove
// to do all the work from us. Basically, we're going to move every dot one forward.
// of course, this would mean that dots[0][99] would be moved into dots[1][0], so we'll
// clean that up next so they don't wrap over the arms
memmove(&dots[0][1], dots, (NUM_ARMS * NUM_LEDS) - 1);
for(int r = 0; r < NUM_ARMS; r++) { dots[r][0] = 0; }
FastLED.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment