Advertisement
Guest User

Untitled

a guest
Dec 26th, 2023
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.72 KB | Source Code | 0 0
  1. //----------------------------------------------------------------------------
  2. // ChristmasTree.cpp
  3. //
  4. // A simple program to animate a set of NeoPixels for use as Christmas
  5. // Tree lights.
  6. //
  7. // This program sets the NeoPixels to a variety of colors then causes
  8. // them to "twinkle" by randomly setting some NeoPixels to max
  9. // brightness then fading them quickly back to minimum brightness. All
  10. // the parameters that control colors and timing are customizable
  11. // below.
  12. //
  13. // Copyright (C) 2023 Blinkenjim
  14. //
  15. // This program is free software: you can redistribute it and/or
  16. // modify it under the terms of the GNU General Public License as
  17. // published by the Free Software Foundation, either version 3 of the
  18. // License, or (at your option) any later version.
  19. //
  20. // This program is distributed in the hope that it will be useful, but
  21. // WITHOUT ANY WARRANTY; without even the implied warranty of
  22. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. // General Public License for more details.
  24. //
  25. // See <https://www.gnu.org/licenses/>.
  26. //----------------------------------------------------------------------------
  27.  
  28. #include <FastLED.h>
  29.  
  30. //----------------------------------------------------------------------------
  31. // BEGIN CUSTOMIZATIONS
  32.  
  33. // The number of NeoPixels in the string
  34. const int NUM_LEDS = 100;
  35.  
  36. // How much to delay at each loop iteration. Typical values are
  37. // between 10 and 20, depending on the length of your string.
  38. const int LOOP_DELAY = 20;
  39.  
  40. // The fade factor influences how quickly to fade from maximum
  41. // brightness. Reasonable values are 255 (slowest) to 220 (fastest); I
  42. // prefer 240.
  43. const int FADE_FACTOR = 240;
  44.  
  45. // The minimum brightness to fade to. Good values are 2 - which I use
  46. // at home where the typical ambient brightness is not high, to 16,
  47. // which I use at work where the ambient brightness is higher.
  48. const int MIN_BRIGHTNESS = 16;
  49.  
  50. // These define our color palette. Change to suit your taste.
  51. CRGB colors[] = {
  52.   CRGB::Red,
  53.   CRGB::Green,
  54.   CRGB::Blue,
  55.   CRGB::Cyan,
  56.   CRGB::Magenta,
  57.   CRGB::Yellow,
  58. };
  59.  
  60. // Which GPIO pin is connected to the NeoPixel data line.
  61. #define DATA_PIN 4
  62.  
  63. //----------------------------------------------------------------------------
  64. // END CUSTOMIZATIONS.
  65.  
  66. // These are the LED structures representing our string of LEDs:
  67. CRGB leds[NUM_LEDS];
  68.  
  69. // Number of colors in the color table.
  70. const int NCOLORS = sizeof(colors) / sizeof(CRGB);
  71.  
  72. void setup() {
  73.   // Set up the serial port so we can do some debugging if needed.
  74.   Serial.begin(57600);
  75.   Serial.println("resetting");
  76.  
  77.   // Initialize the NeoPixel data structures.
  78.   LEDS.addLeds<WS2812, DATA_PIN,RGB>(leds, NUM_LEDS);
  79.  
  80.   // We want the maximum brightness
  81.   LEDS.setBrightness(255);
  82.  
  83.   // Initialize each of the Neopixels. For simplicity's sake we just
  84.   // assign colors in sequence.
  85.   for (int i = 0; i < NUM_LEDS; i += 1) {
  86.     leds[i] = colors[i % NCOLORS];
  87.   }
  88.  
  89.   // Turn on all the NeoPixels. This will turn them all on to their
  90.   // full brightness, but will all fade.
  91.   FastLED.show();
  92. }
  93.  
  94. // A little debug routine.
  95. void printled(CRGB pixel) {
  96.   Serial.print("R: ");
  97.   Serial.print(pixel.red);
  98.  
  99.   Serial.print(" G: ");
  100.   Serial.print(pixel.green);
  101.  
  102.   Serial.print(" B: ");
  103.   Serial.print(pixel.blue);
  104.    
  105.   Serial.println();
  106. }
  107.  
  108. // Fade every NeoPixel in the string down to a limit. The limit
  109. // prevents the string from going completely dark.
  110.  
  111. void fadeall(uint8_t amount, uint8_t limit = 0) {
  112.   for (int i = 0; i < NUM_LEDS; i++) {
  113.     if (leds[i].getAverageLight() >= limit) {
  114.       leds[i].nscale8(amount);
  115.     }
  116.   }
  117. }
  118.  
  119. // Pick one NeoPixel at random and set it to a new random color and to
  120. // full brightness. The chosen NeoPixel will then fade to its lower
  121. // limit as a result of the fadeall() call in the loop function.
  122.  
  123. void plinkOne() {
  124.   // Choose the NeoPixel at random from all the NeoPixels in the
  125.   // string.
  126.   long led_index = random(0, NUM_LEDS);
  127.  
  128.   // Choose a new random color.
  129.   long color_index = random(0, NCOLORS);
  130.  
  131.   // Set the NexPixel to its max brightness by assigning it a new
  132.   // value from the colors array.
  133.   leds[led_index] = colors[color_index];
  134. }
  135.  
  136. // This loop is called very fast and forever.
  137. void loop() {
  138.   // Set one NeoPixel to maximum brightness.
  139.   plinkOne();
  140.  
  141.   // Fade every NeoPixel one increment, but stay above a minimum limit
  142.   // to keep the NexPixels from all going dark at once (unless that's
  143.   // what you want, in which case you can set MIN_BRIGHTNESS to 0 at
  144.   // the top of the file).
  145.   fadeall(FADE_FACTOR, MIN_BRIGHTNESS);
  146.   FastLED.show();
  147.  
  148.   // Delay some small time until the next iteration. This keeps things
  149.   // from going too fast.
  150.   delay(LOOP_DELAY);
  151. }
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement