Advertisement
gluk47

runlight.ino

Jan 5th, 2021
963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. // #define FASTLED_ESP8266_D1_PIN_ORDER
  2. //#define FASTLED_ESP8266_NODEMCU_PIN_ORDER
  3. // #define FASTLED_ESP8266_RAW_PIN_ORDER
  4. // #include <FastLED.h>
  5. #include <NeoPixelBus.h>
  6. #include <unordered_set>
  7.  
  8. #define LED_PIN     D7
  9. #define NUM_LEDS    50
  10. #define STEP        1
  11. #define MAX_BRIGHT  256 //32
  12.  
  13. RgbColor leds[NUM_LEDS];
  14. RgbColor target[NUM_LEDS];
  15.  
  16. NeoPixelBus<NeoGrbFeature, NeoEsp8266DmaWs2812xMethod> strip(NUM_LEDS);
  17. using namespace std;
  18.  
  19. unordered_set<int> branches = {
  20.     6, 7, 8, 12, 13, 17, 18, 19, 22, 23, 27, 28, 31, 32, 33, 37, 38, 42, 43, 44
  21. };
  22. unordered_set<int> trunk = {
  23.     3, 4, 5, 45, 46, 47
  24. };
  25.  
  26. int rcolor(int low, int high, int bright) {
  27.     if (low > high)
  28.         low = 0;
  29.     if (high > 255)
  30.         high = 255;
  31.     bright = min(bright, MAX_BRIGHT);
  32.     return random(low, high) * bright / 255;
  33. }
  34.  
  35. void gen_led(int i) {
  36.     int r, g, b, bright;
  37.     if (branches.count(i)) {
  38.         bright = random(0, 256);
  39.         r = rcolor(0, 32, bright);
  40.         g = rcolor(200, 255, bright);
  41.         b = rcolor(0, 32, bright);
  42.     } else if (trunk.count(i)) {
  43.         bright = random(0, 256);
  44.         r = rcolor(60, 80, 255);
  45.         g = rcolor(20, 30, 255);
  46.         b = rcolor(0, 10, 255);
  47.     } else {
  48.         bright = random(0, 256);
  49.         r = rcolor(200, 255, bright);
  50.         g = rcolor(0, 64, bright);
  51.         b = rcolor(0, 32, bright);
  52.     }
  53.     target[i] = RgbColor(r, g, b);
  54. }
  55.  
  56. void adjust(int led) {
  57.     auto adjust1 = [](uint8_t& actual, uint8_t desired) {
  58.         if (actual == desired)
  59.             return;
  60.         uint8_t diff = max(desired, actual) - min(desired, actual);
  61.         if (diff <= STEP)
  62.             actual = desired;
  63.         else if (actual > desired)
  64.             actual -= STEP;
  65.         else
  66.             actual += STEP;
  67.     };
  68.     adjust1(leds[led].R, target[led].R);
  69.     adjust1(leds[led].G, target[led].G);
  70.     adjust1(leds[led].B, target[led].B);
  71. }
  72.  
  73. void setup() {
  74.     strip.Begin();
  75.     randomSeed(analogRead(0));
  76.     for (int i = 0; i < NUM_LEDS; i++) {
  77.         gen_led(i);
  78.         leds[i] = target[i];
  79.         strip.SetPixelColor(i, leds[i]);
  80.     }
  81. }
  82.  
  83. void loop() {
  84.     for (int i = 0; i < 2; i++)
  85.        gen_led(random(NUM_LEDS));
  86.  
  87.     for (int i = 0; i < NUM_LEDS; i++) {
  88.         adjust(i);
  89.         strip.SetPixelColor(i, leds[i]);
  90.     }
  91.     strip.Show();
  92.  
  93.     delay(10);
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement