Skip to content

Instantly share code, notes, and snippets.

@atuline
Created January 25, 2019 20:36
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 atuline/71ad23f5648d7988965f45390ec4dc4e to your computer and use it in GitHub Desktop.
Save atuline/71ad23f5648d7988965f45390ec4dc4e to your computer and use it in GitHub Desktop.
Sinelon with multiple list pixels
/* sinelon_pixels
*
* Moves multiple pixels back and forth using waves of one form or another.
*
*
* By: Mark Kriegsman
*
* Modified by: Andrew Tuline
*
* Date: January 2018
*
* This uses the built in beat in FastLED to move one or more pixels back and forth.
*
* Method 1, usies beatsin8() is a nice smooth sinewave.
* Method 2, uses triwave8() which is a triangle wave.
* Method 3 counts the pixels up and down. Oh wait, I didn't include method 3 because I REALLY don't want to add a bunch of 'if' statements and use elementary school arithmetic to count pixels up and down. Ugh!
*
* Note that triwave8() isn't as easy to use as beatsin8. Sad panda.
*
*/
#include "FastLED.h" // FastLED library. Preferably the latest copy of FastLED 2.1.
#if FASTLED_VERSION < 3001000
#error "Requires FastLED 3.1 or later; check github for latest code."
#endif
// Fixed definitions cannot change on the fly.
#define LED_DT 12 // Data pin to connect to the strip.
#define LED_CK 11 // Clock pin for WS2801 or APA102.
#define COLOR_ORDER BGR // It's GRB for WS2812 and BGR for APA102.
#define LED_TYPE APA102 // Using APA102, WS2812, WS2801. Don't forget to modify LEDS.addLeds to suit.
#define NUM_LEDS 40 // Number of LED's.
// Global variables can be changed on the fly.
uint8_t max_bright = 128; // Overall brightness definition. It can be changed on the fly.
struct CRGB leds[NUM_LEDS]; // Initialize our LED array.
CRGBPalette16 currentPalette;
CRGBPalette16 targetPalette;
TBlendType currentBlending; // NOBLEND or LINEARBLEND
// Define variables used by the sequences.
#define ledlen 3 // Number of LED's to move back and forth.
uint8_t thisbeat = 10; // Beats per minute for first part of dot.
uint8_t thisfade = 240; // How quickly does it fade? Lower = slower fade rate.
uint8_t thissat = 255; // The saturation, where 255 = brilliant colours.
uint8_t thisbri = 255; // Brightness of a sequence.
uint8_t myhue = 0; // Hue, or palette index #.
int thisdelay = 10;
void setup() {
Serial.begin(57600); // Initialize serial port for debugging.
delay(1000); // Soft startup to ease the flow of electrons.
// LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS); // Use this for WS2812B
LEDS.addLeds<LED_TYPE, LED_DT, LED_CK, COLOR_ORDER>(leds, NUM_LEDS); // Use this for WS2801 or APA102
currentBlending = LINEARBLEND;
currentPalette = PartyColors_p;
FastLED.setBrightness(max_bright);
set_max_power_in_volts_and_milliamps(5, 500); // FastLED Power management set at 5V, 500mA.
} // setup()
void loop () {
EVERY_N_MILLISECONDS(thisdelay) { // FastLED based non-blocking delay to update/display the sequence.
sinelon_pixels(); // Call our sequence.
}
FastLED.show();
} // loop()
void sinelon_pixels() { // a colored dot sweeping back and forth, with fading trails
fadeToBlackBy( leds, NUM_LEDS, thisfade);
int pos1 = beatsin8(thisbeat,0,NUM_LEDS-ledlen); // This is METHOD 1.
int pos2 = triwave8(millis()/20); // Which goes from 0 to 255.
pos2 = (pos2*(NUM_LEDS-ledlen+1))/255; // We need to map that go go to NUM_LEDS-ledlen
for(int i=0; i<ledlen; i++)
leds[(i+pos2)%(NUM_LEDS)] = ColorFromPalette(currentPalette, myhue, thisbri, currentBlending); // Try both the pos1 and pos2 variables!! Add the modulo operator for safety.
} // sinelon_pixels()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment