Skip to content

Instantly share code, notes, and snippets.

Last active Jun 22, 2019
What would you like to do?
/* sinelon_pixels
* Moves a single sine wave back and forth (or continuously) using waves.
* By: Andrew Tuline
* Date: January 2019
* This uses the built in FastLED beatsin8 combined with a sinewave to move one or more pixels back and forth.
* The challenge is scaling low brightness pixels as they still look pretty bright. That's a whole other ballgame.
#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."
// 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;
void setup() {
Serial.begin(57600); // Initialize serial port for debugging.
// 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
currentPalette = PartyColors_p;
set_max_power_in_volts_and_milliamps(5, 500); // FastLED Power management set at 5V, 500mA.
} // setup()
void loop () {
EVERY_N_MILLISECONDS(20) { // FastLED based non-blocking delay to update/display the sequence.
sinelon_pixels(); // You don't really need the EVERY_N_MILLISECONDS in this routine.
} // loop()
void sinelon_pixels() {
fadeToBlackBy( leds, NUM_LEDS, 220);
// int pos1 = millis()/64; // Continuous loop.
int pos1 = beatsin8(10,0,NUM_LEDS-7); // Back and forth a la sinelon.
for (uint8_t i = 0; i<7; i++) {
int thisbright = cubicwave8((i*40)); // Could've used another sine function and adjusted the phase to suit.
thisbright = thisbright*thisbright/255; // Increase the brightness drop (see gamma correction).
leds[(i+pos1)%NUM_LEDS] = ColorFromPalette( currentPalette, 5, thisbright, LINEARBLEND);
} // sinelon_pixels()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment