atuline

alightsaber.ino

Jul 5th, 2021 (edited)
1,170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.69 KB | None | 0 0
  1. /* lightsaber.ino
  2.  
  3.    Start up a light saber and watch it shimmer slightly on an Arduino UNO/Nano (or better).
  4.  
  5.   Modified by: Andrew Tuline
  6.  
  7.   Date: July, 2021
  8.  
  9.   Referemce:
  10.  
  11.   https://old.reddit.com/r/FastLED/comments/oe9i45/led_turn_onoff_light_effect_for_a_lightsaber_build/
  12.  
  13.   The video:
  14.  
  15.   https://www.youtube.com/watch?v=00BXnUiAU_M
  16.  
  17. */
  18.  
  19. #include <FastLED.h>
  20. #include <OneButton.h>             // Available from Library manager.
  21.  
  22. #define LED_DT 12
  23. #define NUM_LEDS 32
  24. #define BTN_PIN   6
  25.  
  26. uint16_t num_leds = NUM_LEDS;
  27. bool leddir = 1;
  28.  
  29. uint8_t max_bright = 128;
  30.  
  31. struct CRGB leds[NUM_LEDS];
  32.  
  33.  
  34. uint8_t color = 32;
  35. uint8_t hue = 0;
  36. uint8_t patternCounter = 0;
  37.  
  38. OneButton btn = OneButton(BTN_PIN, true);
  39.  
  40.  
  41. CRGBPalette16 currentPalette;
  42. CRGBPalette16 targetPalette;
  43. TBlendType    currentBlending;
  44.  
  45.  
  46. void setup() {
  47.  
  48.   Serial.begin(115200);
  49.   delay(1000);
  50.   LEDS.addLeds<WS2812, LED_DT, GRB>(leds, NUM_LEDS);
  51.   currentBlending = LINEARBLEND;
  52.  
  53.   uint8_t shimcol = random8();
  54.   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)));
  55.  
  56.   FastLED.setBrightness(max_bright);
  57.  
  58.   btn.attachLongPressStart(nextPattern); //changes the modes
  59.   btn.attachClick(nextColor); //changes the base color
  60.   btn.attachDoubleClick(doubleclick);
  61.  
  62. } // setup()
  63.  
  64.  
  65.  
  66. void loop () {
  67.  
  68.   //  fadeToBlackBy(leds,NUM_LEDS,1);
  69.  
  70.   switch (patternCounter) {
  71.     case 0:
  72.       shimmer();
  73.       break;
  74.     case 1:
  75.       rainbow();
  76.       break;
  77.     case 2:
  78.       baseColor();
  79.       break;
  80.   }
  81.  
  82.   EVERY_N_MILLIS(50) {
  83.     if (leddir) {
  84.       if (num_leds < NUM_LEDS) num_leds++;
  85.     } else {
  86.       if (num_leds > 0) num_leds--;
  87.     }
  88.   }
  89.  
  90.   if (!leddir) {
  91.     fadeToBlackBy(leds + num_leds + random8(NUM_LEDS - num_leds), 1, 64);      // Fade the area between num_leds and NUM_LEDS.
  92.   }
  93.  
  94.   FastLED.show();
  95.   btn.tick();
  96.  
  97. } // loop()
  98.  
  99.  
  100.  
  101.  
  102. void doubleclick() {
  103.   leddir = !leddir;
  104.   Serial.print(leddir);
  105. }
  106.  
  107.  
  108. void nextPattern() {
  109.   patternCounter = (patternCounter + 1) % 3;
  110. }
  111.  
  112.  
  113. void nextColor() {
  114.   color = color + 32;
  115.   uint8_t shimcol = random8();
  116.   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)));
  117. }
  118.  
  119.  
  120. void baseColor() {
  121.   uint8_t oneBeat = beatsin8(30, 200, 255, 0, 0); //sin1
  122.   uint8_t twoBeat = beatsin8(60, 200, 255, 0, 0); //sin2
  123.   uint8_t treeBeat = beatsin8(90, 200, 255, 0, 0); //sin2
  124.   uint8_t sinBeat = (oneBeat + twoBeat + treeBeat) / 3;
  125.   fill_solid(leds, num_leds, CHSV(color, 255, sinBeat));   //base color with alternating/breathing brightnes
  126. }
  127.  
  128.  
  129. void rainbow()  {
  130.   uint8_t oneBeat = beatsin8(30, 200, 255, 0, 0); //sin1
  131.   uint8_t twoBeat = beatsin8(60, 200, 255, 0, 0); //sin2
  132.   uint8_t treeBeat = beatsin8(90, 200, 255, 0, 0); //sin2
  133.   uint8_t sinBeat = (oneBeat + twoBeat + treeBeat) / 3;
  134.  
  135.   for (int i = num_leds / 2; i < num_leds; i++) { //front
  136.     leds[ (num_leds - 1) - i ] = CHSV(hue + (i * 10), 255, sinBeat);
  137.   }
  138.   for (int j = num_leds / 2; j < num_leds; j++) { //back
  139.     leds[j] = CHSV(hue + (j * 10), 255, sinBeat);
  140.   }
  141.  
  142.   EVERY_N_MILLISECONDS(15) {
  143.     hue++;
  144.   }
  145. }
  146.  
  147.  
  148. void shimmer() {                                                           // A time (rather than loop) based demo sequencer. This gives us full control over the length of each sequence.
  149.  
  150.   static int16_t dist;                                          // A random number for our noise generator.
  151.   uint16_t xscale = 30;                                         // Wouldn't recommend changing this on the fly, or the animation will be really blocky.
  152.   uint16_t yscale = 30;                                         // Wouldn't recommend changing this on the fly, or the animation will be really blocky.
  153.  
  154.   for (int i = 0; i < num_leds; i++) {                                     // Just ONE loop to fill up the LED array as all of the pixels change.
  155.     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.
  156.     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.
  157.   }
  158.   //  dist += beatsin8(10,1,4);                                                // Moving along the distance (that random number we started out with). Vary it a bit with a sine wave.
  159.   dist += inoise8(millis(), millis()) / 24;
  160.   // In some sketches, I've used millis() instead of an incremented counter. Works a treat.
  161. } // shimmer()
Add Comment
Please, Sign In to add comment