Advertisement
RebounD11

Decoratiune

Dec 6th, 2020 (edited)
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2.  
  3. #define PIN 9
  4. #define NUM_OF_PIX 64
  5. #define STEP 11
  6.  
  7. // Parameter 1 = number of pixels in strip
  8. // Parameter 2 = Arduino pin number (most are valid)
  9. // Parameter 3 = pixel type flags, add together as needed:
  10. //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  11. //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  12. //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
  13. //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  14. Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_OF_PIX, PIN, NEO_GRB + NEO_KHZ800);
  15.  
  16. // IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
  17. // pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
  18. // and minimize distance between Arduino and first pixel.  Avoid connecting
  19. // on a live circuit...if you must, connect GND first.
  20.  
  21. uint32_t startPix = 0;
  22. uint8_t bright[NUM_OF_PIX];
  23. uint8_t redMax;
  24. uint8_t greenMax;
  25. uint8_t blueMax;
  26.  
  27. void setup() {
  28.   strip.begin();
  29.   strip.show(); // Initialize all pixels to 'off'
  30.   for (int i=0; i<NUM_OF_PIX; i++) {
  31.     bright[i] = 0;
  32.   }
  33.   redMax = 63;
  34.   greenMax = 63;
  35.   blueMax = 63;
  36. }
  37.  
  38. void loop() {
  39.   startPix = starlight(redMax, greenMax, blueMax, startPix, 50);
  40. }
  41.  
  42. uint32_t starlight (uint8_t redMax, uint8_t greenMax, uint8_t blueMax, uint32_t startPixel, int wait) {
  43.   uint32_t ret = startPixel;
  44.   bool newStar = false;
  45.   int i = startPixel;
  46.   uint8_t minBright = 31;
  47.   while (!newStar) {
  48.     minBright = 31;
  49.     for (int j=0; j<NUM_OF_PIX; j++) {
  50.       if ((bright[j] != 0) && (bright[j] < minBright)) {
  51.         minBright = bright[j];
  52.       }
  53.     }
  54.     if ((bright[i] == 0) && (minBright > 8)) {
  55.       bright[i] = 1;
  56.       newStar = true;
  57.     }
  58.     else {
  59.       if (bright[i] == 31) {
  60.         bright[i] = 0;
  61.         ret = (i+STEP)%NUM_OF_PIX;
  62.       }
  63.       else if (bright[i] != 0) {
  64.         bright[i]++;
  65.       }
  66.       else {
  67.         newStar = true;
  68.       }
  69.       i += STEP;
  70.     }
  71.     i = i % NUM_OF_PIX;
  72.   }
  73.   for (int i=0; i<strip.numPixels(); i++) {
  74.     uint32_t color;
  75.     uint8_t red;
  76.     uint8_t green;
  77.     uint8_t blue;
  78.     if (bright[i] <= 16) {
  79.       red = (redMax >> 4) * bright[i];
  80.       green = (greenMax >> 4) * bright[i];
  81.       blue = (blueMax >> 4) * bright[i];
  82.     }
  83.     else {
  84.       red = (redMax >> 4) * (32 - bright[i]);
  85.       green = (greenMax >> 4) * (32 - bright[i]);
  86.       blue = (blueMax >> 4) * (32 - bright[i]);
  87.     }
  88.     color = strip.Color(red, green, blue);
  89.     strip.setPixelColor(i, color);
  90.   }
  91.   strip.show();
  92.   delay(wait);
  93.   return ret;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement