atuline

dancing_shadows.ino

Feb 14th, 2022 (edited)
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.33 KB | None | 0 0
  1. /* Dancing Shadows for FastLED
  2.  
  3.    By: Steve Pomeroy @xxv
  4.  
  5.    Link: https://github.com/Aircoookie/WLED/blob/447b811fa07629673ab009b48e8157d3e110070a/wled00/FX.cpp#L3759
  6.  
  7.    Converted from WLED to FastLED by: Andrew Tuline
  8.  
  9.    Link: https://pastebin.com/TeqMpJ5L
  10.    
  11.    Date: Valentines Day 2022
  12.  
  13.    Dancing shadows has two controls, intensity and speed. Feel free to play with them.
  14.  
  15.    Originally (as far as I know) written for WLED on an ESP8266 and ESP32, this was ported on
  16.    a Nano, so it should work on lots of devices.
  17.  
  18. */
  19.  
  20.  
  21. #include <FastLED.h>
  22.  
  23. #define LED_DT 12
  24. #define NUM_LEDS 60
  25.  
  26. uint8_t max_bright = 128;
  27.  
  28. struct CRGB leds[NUM_LEDS];
  29.  
  30. CRGBPalette16 currentPalette = PartyColors_p;
  31. CRGBPalette16 targetPalette;
  32.  
  33.  
  34. typedef struct Spotlight {
  35.   float speed;
  36.   uint8_t colorIdx;
  37.   int16_t position;
  38.   unsigned long lastUpdateTime;
  39.   uint8_t width;
  40.   uint8_t type;
  41. } spotlight;
  42.  
  43.  
  44. #define SPOT_TYPE_SOLID       0
  45. #define SPOT_TYPE_GRADIENT    1
  46. #define SPOT_TYPE_2X_GRADIENT 2
  47. #define SPOT_TYPE_2X_DOT      3
  48. #define SPOT_TYPE_3X_DOT      4
  49. #define SPOT_TYPE_4X_DOT      5
  50. #define SPOT_TYPES_COUNT      6
  51.  
  52. #ifdef AVR
  53. #define SPOT_MAX_COUNT 16         // Number of simultaneous waves.
  54. #else
  55. #define SPOT_MAX_COUNT 32
  56. #endif
  57.  
  58. uint8_t intensity = 128;          // An adjustable control.
  59. uint8_t speedv = 128;             // Another adjustable control.
  60.  
  61. bool initialize = true;
  62.  
  63. spotlight spotlights[SPOT_MAX_COUNT];
  64.  
  65.  
  66.  
  67. void setup() {
  68.   Serial.begin(115200);
  69.   LEDS.addLeds<WS2812, LED_DT, GRB>(leds, NUM_LEDS);
  70.   FastLED.setBrightness(max_bright);
  71.  
  72.   Serial.println(SPOT_MAX_COUNT);
  73. } // setup()
  74.  
  75.  
  76.  
  77. void loop () {
  78.   dancing_shadows();
  79.   FastLED.show();
  80. } // loop()
  81.  
  82.  
  83.  
  84. void dancing_shadows(void) {
  85.  
  86.   uint8_t numSpotlights = map(intensity, 0, 255, 2, SPOT_MAX_COUNT);
  87.  
  88.   fadeToBlackBy(leds, NUM_LEDS, 64);
  89.  
  90.   unsigned long time = millis();
  91.   bool respawn = false;
  92.  
  93.   for (uint8_t i = 0; i < numSpotlights; i++) {
  94.     if (!initialize) {                                                // advance the position of the spotlight
  95.       int16_t delta = (float)(time - spotlights[i].lastUpdateTime) *
  96.                       (spotlights[i].speed * ((1.0 + speedv) / 100.0));
  97.  
  98.       if (abs(delta) >= 1) {
  99.         spotlights[i].position += delta;
  100.         spotlights[i].lastUpdateTime = time;
  101.       }
  102.  
  103.       respawn = (spotlights[i].speed > 0.0 && spotlights[i].position > (NUM_LEDS + 2))
  104.                 || (spotlights[i].speed < 0.0 && spotlights[i].position < -(spotlights[i].width + 2));
  105.     }
  106.  
  107.     if (initialize || respawn) {
  108.       spotlights[i].colorIdx = random8();
  109.       spotlights[i].width = random8(1, 10);
  110.       spotlights[i].speed = 1.0 / random8(4, 50);
  111.  
  112.       if (initialize) {
  113.         spotlights[i].position = random16(NUM_LEDS);
  114.         spotlights[i].speed *= random8(2) ? 1.0 : -1.0;
  115.       } else {
  116.         if (random8(2)) {
  117.           spotlights[i].position = NUM_LEDS + spotlights[i].width;
  118.           spotlights[i].speed *= -1.0;
  119.         } else {
  120.           spotlights[i].position = -spotlights[i].width;
  121.         }
  122.       }
  123.  
  124.       spotlights[i].lastUpdateTime = time;
  125.       spotlights[i].type = random8(SPOT_TYPES_COUNT);
  126.     }
  127.  
  128.     CRGB color = ColorFromPalette(currentPalette, spotlights[i].colorIdx, 255, LINEARBLEND);
  129.  
  130.     int start = spotlights[i].position;
  131.  
  132.     if (spotlights[i].width <= 1) {
  133.       if (start >= 0 && start < NUM_LEDS) {
  134.         leds[start] = blend(leds[start], color, 128);
  135.       }
  136.     } else {
  137.       switch (spotlights[i].type) {
  138.         case SPOT_TYPE_SOLID:
  139.           for (uint8_t j = 0; j < spotlights[i].width; j++) {
  140.             if ((start + j) >= 0 && (start + j) < NUM_LEDS) {
  141.               leds[start + j] = blend(leds[start + j], color, 128);
  142.             }
  143.           }
  144.           break;
  145.  
  146.         case SPOT_TYPE_GRADIENT:
  147.           for (uint8_t j = 0; j < spotlights[i].width; j++) {
  148.             if ((start + j) >= 0 && (start + j) < NUM_LEDS) {
  149.               leds[start + j] = blend(leds[start + j], color, cubicwave8(map(j, 0, spotlights[i].width - 1, 0, 255)));
  150.             }
  151.           }
  152.           break;
  153.  
  154.         case SPOT_TYPE_2X_GRADIENT:
  155.           for (uint8_t j = 0; j < spotlights[i].width; j++) {
  156.             if ((start + j) >= 0 && (start + j) < NUM_LEDS) {
  157.               leds[start + j] = blend(leds[start + j] , color, cubicwave8(2 * map(j, 0, spotlights[i].width - 1, 0, 255)));
  158.             }
  159.           }
  160.           break;
  161.  
  162.         case SPOT_TYPE_2X_DOT:
  163.           for (uint8_t j = 0; j < spotlights[i].width; j += 2) {
  164.             if ((start + j) >= 0 && (start + j) < NUM_LEDS) {
  165.               leds[start + j] = blend(leds[start + j], color , 128);
  166.             }
  167.           }
  168.           break;
  169.  
  170.         case SPOT_TYPE_3X_DOT:
  171.           for (uint8_t j = 0; j < spotlights[i].width; j += 3) {
  172.             if ((start + j) >= 0 && (start + j) < NUM_LEDS) {
  173.               leds[start + j] = blend(leds[start + j], color, 128);
  174.             }
  175.           }
  176.           break;
  177.  
  178.         case SPOT_TYPE_4X_DOT:
  179.           for (uint8_t j = 0; j < spotlights[i].width; j += 4) {
  180.             if ((start + j) >= 0 && (start + j) < NUM_LEDS) {
  181.               leds[start + j] = blend(leds[start + j], color, 128);
  182.             }
  183.           }
  184.           break;
  185.       }
  186.     }
  187.   }
  188.   if (initialize) initialize = false;
  189.  
  190. } // dancing_shadows()
Add Comment
Please, Sign In to add comment