Advertisement
atuline

dualsaber.ino

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