Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* lightsaber.ino
- Start up a light saber and watch it shimmer slightly on an Arduino UNO/Nano (or better).
- Modified by: Andrew Tuline
- Date: July, 2021
- Referemce:
- https://old.reddit.com/r/FastLED/comments/oe9i45/led_turn_onoff_light_effect_for_a_lightsaber_build/
- The video:
- https://www.youtube.com/watch?v=00BXnUiAU_M
- */
- #include <FastLED.h>
- #include <OneButton.h> // Available from Library manager.
- #define LED_DT 12
- #define NUM_LEDS 32
- #define BTN_PIN 6
- uint16_t num_leds = NUM_LEDS;
- bool leddir = 1;
- uint8_t max_bright = 128;
- struct CRGB leds[NUM_LEDS];
- uint8_t color = 32;
- uint8_t hue = 0;
- uint8_t patternCounter = 0;
- OneButton btn = OneButton(BTN_PIN, true);
- CRGBPalette16 currentPalette;
- CRGBPalette16 targetPalette;
- TBlendType currentBlending;
- void setup() {
- Serial.begin(115200);
- delay(1000);
- LEDS.addLeds<WS2812, LED_DT, GRB>(leds, NUM_LEDS);
- currentBlending = LINEARBLEND;
- uint8_t shimcol = random8();
- currentPalette = CRGBPalette16(CHSV(shimcol, 224, random8(64, 255)), CHSV(shimcol + 3, 240, random8(64, 255)), CHSV(shimcol, 192, random8(224, 255)), CHSV(shimcol - 3, 240, random8(224, 255)));
- FastLED.setBrightness(max_bright);
- btn.attachLongPressStart(nextPattern); //changes the modes
- btn.attachClick(nextColor); //changes the base color
- btn.attachDoubleClick(doubleclick);
- } // setup()
- void loop () {
- // fadeToBlackBy(leds,NUM_LEDS,1);
- switch (patternCounter) {
- case 0:
- shimmer();
- break;
- case 1:
- rainbow();
- break;
- case 2:
- baseColor();
- break;
- }
- EVERY_N_MILLIS(50) {
- if (leddir) {
- if (num_leds < NUM_LEDS) num_leds++;
- } else {
- if (num_leds > 0) num_leds--;
- }
- }
- if (!leddir) {
- fadeToBlackBy(leds + num_leds + random8(NUM_LEDS - num_leds), 1, 64); // Fade the area between num_leds and NUM_LEDS.
- }
- FastLED.show();
- btn.tick();
- } // loop()
- void doubleclick() {
- leddir = !leddir;
- Serial.print(leddir);
- }
- void nextPattern() {
- patternCounter = (patternCounter + 1) % 3;
- }
- void nextColor() {
- color = color + 32;
- uint8_t shimcol = random8();
- currentPalette = CRGBPalette16(CHSV(shimcol, 224, random8(64, 255)), CHSV(shimcol + 3, 240, random8(64, 255)), CHSV(shimcol, 192, random8(224, 255)), CHSV(shimcol - 3, 240, random8(224, 255)));
- }
- void baseColor() {
- uint8_t oneBeat = beatsin8(30, 200, 255, 0, 0); //sin1
- uint8_t twoBeat = beatsin8(60, 200, 255, 0, 0); //sin2
- uint8_t treeBeat = beatsin8(90, 200, 255, 0, 0); //sin2
- uint8_t sinBeat = (oneBeat + twoBeat + treeBeat) / 3;
- fill_solid(leds, num_leds, CHSV(color, 255, sinBeat)); //base color with alternating/breathing brightnes
- }
- void rainbow() {
- uint8_t oneBeat = beatsin8(30, 200, 255, 0, 0); //sin1
- uint8_t twoBeat = beatsin8(60, 200, 255, 0, 0); //sin2
- uint8_t treeBeat = beatsin8(90, 200, 255, 0, 0); //sin2
- uint8_t sinBeat = (oneBeat + twoBeat + treeBeat) / 3;
- for (int i = num_leds / 2; i < num_leds; i++) { //front
- leds[ (num_leds - 1) - i ] = CHSV(hue + (i * 10), 255, sinBeat);
- }
- for (int j = num_leds / 2; j < num_leds; j++) { //back
- leds[j] = CHSV(hue + (j * 10), 255, sinBeat);
- }
- EVERY_N_MILLISECONDS(15) {
- hue++;
- }
- }
- void shimmer() { // A time (rather than loop) based demo sequencer. This gives us full control over the length of each sequence.
- 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.
- 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.
- dist += inoise8(millis(), millis()) / 24;
- // In some sketches, I've used millis() instead of an incremented counter. Works a treat.
- } // shimmer()
Add Comment
Please, Sign In to add comment