Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // #define FASTLED_ESP8266_D1_PIN_ORDER
- //#define FASTLED_ESP8266_NODEMCU_PIN_ORDER
- // #define FASTLED_ESP8266_RAW_PIN_ORDER
- // #include <FastLED.h>
- #include <NeoPixelBus.h>
- #include <unordered_set>
- #define LED_PIN D7
- #define NUM_LEDS 50
- #define STEP 1
- #define MAX_BRIGHT 256 //32
- RgbColor leds[NUM_LEDS];
- RgbColor target[NUM_LEDS];
- NeoPixelBus<NeoGrbFeature, NeoEsp8266DmaWs2812xMethod> strip(NUM_LEDS);
- using namespace std;
- unordered_set<int> branches = {
- 6, 7, 8, 12, 13, 17, 18, 19, 22, 23, 27, 28, 31, 32, 33, 37, 38, 42, 43, 44
- };
- unordered_set<int> trunk = {
- 3, 4, 5, 45, 46, 47
- };
- int rcolor(int low, int high, int bright) {
- if (low > high)
- low = 0;
- if (high > 255)
- high = 255;
- bright = min(bright, MAX_BRIGHT);
- return random(low, high) * bright / 255;
- }
- void gen_led(int i) {
- int r, g, b, bright;
- if (branches.count(i)) {
- bright = random(0, 256);
- r = rcolor(0, 32, bright);
- g = rcolor(200, 255, bright);
- b = rcolor(0, 32, bright);
- } else if (trunk.count(i)) {
- bright = random(0, 256);
- r = rcolor(60, 80, 255);
- g = rcolor(20, 30, 255);
- b = rcolor(0, 10, 255);
- } else {
- bright = random(0, 256);
- r = rcolor(200, 255, bright);
- g = rcolor(0, 64, bright);
- b = rcolor(0, 32, bright);
- }
- target[i] = RgbColor(r, g, b);
- }
- void adjust(int led) {
- auto adjust1 = [](uint8_t& actual, uint8_t desired) {
- if (actual == desired)
- return;
- uint8_t diff = max(desired, actual) - min(desired, actual);
- if (diff <= STEP)
- actual = desired;
- else if (actual > desired)
- actual -= STEP;
- else
- actual += STEP;
- };
- adjust1(leds[led].R, target[led].R);
- adjust1(leds[led].G, target[led].G);
- adjust1(leds[led].B, target[led].B);
- }
- void setup() {
- strip.Begin();
- randomSeed(analogRead(0));
- for (int i = 0; i < NUM_LEDS; i++) {
- gen_led(i);
- leds[i] = target[i];
- strip.SetPixelColor(i, leds[i]);
- }
- }
- void loop() {
- for (int i = 0; i < 2; i++)
- gen_led(random(NUM_LEDS));
- for (int i = 0; i < NUM_LEDS; i++) {
- adjust(i);
- strip.SetPixelColor(i, leds[i]);
- }
- strip.Show();
- delay(10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement