Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //----------------------------------------------------------------------------
- // ChristmasTree.cpp
- //
- // A simple program to animate a set of NeoPixels for use as Christmas
- // Tree lights.
- //
- // This program sets the NeoPixels to a variety of colors then causes
- // them to "twinkle" by randomly setting some NeoPixels to max
- // brightness then fading them quickly back to minimum brightness. All
- // the parameters that control colors and timing are customizable
- // below.
- //
- // Copyright (C) 2023 Blinkenjim
- //
- // This program is free software: you can redistribute it and/or
- // modify it under the terms of the GNU General Public License as
- // published by the Free Software Foundation, either version 3 of the
- // License, or (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful, but
- // WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- // General Public License for more details.
- //
- // See <https://www.gnu.org/licenses/>.
- //----------------------------------------------------------------------------
- #include <FastLED.h>
- //----------------------------------------------------------------------------
- // BEGIN CUSTOMIZATIONS
- // The number of NeoPixels in the string
- const int NUM_LEDS = 100;
- // How much to delay at each loop iteration. Typical values are
- // between 10 and 20, depending on the length of your string.
- const int LOOP_DELAY = 20;
- // The fade factor influences how quickly to fade from maximum
- // brightness. Reasonable values are 255 (slowest) to 220 (fastest); I
- // prefer 240.
- const int FADE_FACTOR = 240;
- // The minimum brightness to fade to. Good values are 2 - which I use
- // at home where the typical ambient brightness is not high, to 16,
- // which I use at work where the ambient brightness is higher.
- const int MIN_BRIGHTNESS = 16;
- // These define our color palette. Change to suit your taste.
- CRGB colors[] = {
- CRGB::Red,
- CRGB::Green,
- CRGB::Blue,
- CRGB::Cyan,
- CRGB::Magenta,
- CRGB::Yellow,
- };
- // Which GPIO pin is connected to the NeoPixel data line.
- #define DATA_PIN 4
- //----------------------------------------------------------------------------
- // END CUSTOMIZATIONS.
- // These are the LED structures representing our string of LEDs:
- CRGB leds[NUM_LEDS];
- // Number of colors in the color table.
- const int NCOLORS = sizeof(colors) / sizeof(CRGB);
- void setup() {
- // Set up the serial port so we can do some debugging if needed.
- Serial.begin(57600);
- Serial.println("resetting");
- // Initialize the NeoPixel data structures.
- LEDS.addLeds<WS2812, DATA_PIN,RGB>(leds, NUM_LEDS);
- // We want the maximum brightness
- LEDS.setBrightness(255);
- // Initialize each of the Neopixels. For simplicity's sake we just
- // assign colors in sequence.
- for (int i = 0; i < NUM_LEDS; i += 1) {
- leds[i] = colors[i % NCOLORS];
- }
- // Turn on all the NeoPixels. This will turn them all on to their
- // full brightness, but will all fade.
- FastLED.show();
- }
- // A little debug routine.
- void printled(CRGB pixel) {
- Serial.print("R: ");
- Serial.print(pixel.red);
- Serial.print(" G: ");
- Serial.print(pixel.green);
- Serial.print(" B: ");
- Serial.print(pixel.blue);
- Serial.println();
- }
- // Fade every NeoPixel in the string down to a limit. The limit
- // prevents the string from going completely dark.
- void fadeall(uint8_t amount, uint8_t limit = 0) {
- for (int i = 0; i < NUM_LEDS; i++) {
- if (leds[i].getAverageLight() >= limit) {
- leds[i].nscale8(amount);
- }
- }
- }
- // Pick one NeoPixel at random and set it to a new random color and to
- // full brightness. The chosen NeoPixel will then fade to its lower
- // limit as a result of the fadeall() call in the loop function.
- void plinkOne() {
- // Choose the NeoPixel at random from all the NeoPixels in the
- // string.
- long led_index = random(0, NUM_LEDS);
- // Choose a new random color.
- long color_index = random(0, NCOLORS);
- // Set the NexPixel to its max brightness by assigning it a new
- // value from the colors array.
- leds[led_index] = colors[color_index];
- }
- // This loop is called very fast and forever.
- void loop() {
- // Set one NeoPixel to maximum brightness.
- plinkOne();
- // Fade every NeoPixel one increment, but stay above a minimum limit
- // to keep the NexPixels from all going dark at once (unless that's
- // what you want, in which case you can set MIN_BRIGHTNESS to 0 at
- // the top of the file).
- fadeall(FADE_FACTOR, MIN_BRIGHTNESS);
- FastLED.show();
- // Delay some small time until the next iteration. This keeps things
- // from going too fast.
- delay(LOOP_DELAY);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement