Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/* lightsaber
*
* By: Andrew Tuline
*
* Start up a light saber and watch it shimmer slightly.
*
* Date: January, 2017
*
* To do:
*
* - Change colours
* - Turn it off
*
*
*/
#include "FastLED.h" // FastLED library.
#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 60 // Number of LED's.
// Global variables can be changed on the fly.
uint8_t max_bright = 128; // Overall brightness.
struct CRGB leds[NUM_LEDS]; // Initialize our LED array.
// Palette definitions
CRGBPalette16 currentPalette;
CRGBPalette16 targetPalette; // Not in use with this sketch.
TBlendType currentBlending; // NOBLEND or LINEARBLEND
uint8_t shimmerdelay = 20;
static int16_t dist; // A random number for our noise generator.
uint16_t xscale = 30; // Wouldn't recommend changing this on the fly, or the animation will be really blocky.
uint16_t yscale = 30; // Wouldn't recommend changing this on the fly, or the animation will be really blocky.
uint8_t maxChanges = 24; // Value for blending between palettes.
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, LED_CK, COLOR_ORDER>(leds, NUM_LEDS); // Use this for WS2801 or APA102
// LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS); // Use this for WS2812
currentBlending = LINEARBLEND;
uint8_t shimcol = random8();
currentPalette = CRGBPalette16(CHSV(shimcol, 224, random8(224,255)), CHSV(shimcol+3, 240, random8(224,255)), CHSV(shimcol, 192, random8(224,255)), CHSV(shimcol-3, 240, random8(224,255)));
FastLED.setBrightness(max_bright);
set_max_power_in_volts_and_milliamps(5, 500); // FastLED Power management set at 5V, 500mA.
startup();
} // setup()
void loop () {
EVERY_N_MILLISECONDS(shimmerdelay) { // FastLED based non-blocking delay to update/display the sequence.
shimmer();
}
show_at_max_brightness_for_power();
} // loop()
void startup() {
for(int i = 0; i < NUM_LEDS; i++) { // Just ONE loop to fill up the LED array as all of the pixels change.
uint8_t index = inoise8(i*xscale, dist+i*yscale) % 255; // Get a value from the noise function. I'm using both x and y axis.
leds[i] = ColorFromPalette(currentPalette, index, 255, LINEARBLEND); // With that value, look up the 8 bit colour palette value and assign it to the current LED.
show_at_max_brightness_for_power();
delay(10);
}
} // startup()
void shimmer() { // A time (rather than loop) based demo sequencer. This gives us full control over the length of each sequence.
for(int i = 0; i < NUM_LEDS; i++) { // Just ONE loop to fill up the LED array as all of the pixels change.
uint8_t index = inoise8(i*xscale, dist+i*yscale) % 255; // Get a value from the noise function. I'm using both x and y axis.
leds[i] = ColorFromPalette(currentPalette, index, 255, LINEARBLEND); // With that value, look up the 8 bit colour palette value and assign it to the current LED.
}
dist += beatsin8(10,1,4); // Moving along the distance (that random number we started out with). Vary it a bit with a sine wave.
// In some sketches, I've used millis() instead of an incremented counter. Works a treat.
} // shimmer()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment