RossW1701

NeoPixel Blinky

Oct 6th, 2024
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 2.97 KB | Software | 0 0
  1. #include "FastLED.h"        // https://github.com/FastLED/FastLED
  2.  
  3. #define NEO_PIN PIN_PB1         // or 1 (NeoPixel pin on ATtiny85)
  4. #define ADC_IN PIN_PB4          // or 4 (ADC2 input pin on ATtiny85)
  5.  
  6. #define NEO_COUNT 10            // Number of NePixels connected in a string (could be 10 or 20)
  7. uint8_t NEO_BRIGHTNESS  = 5;    // NeoPixel brightness
  8. uint32_t MIN_RANDOM_NUM = 150;  // lower random blink time
  9. uint32_t MAX_RANDOM_NUM = 1000; // upper random blink time
  10.  
  11. // State variables to determine when to start showing NecPixel blinkies
  12. bool waitForAmberLEDStartup = true;
  13. bool showNeoPixelBlinkies   = false;
  14.  
  15. long delayFudgeFactorMS = 1000;
  16.  
  17. uint32_t colors[] = {
  18.   0x00FF0000,  // Red
  19.   0x00FF6666,  // Lt. Red
  20.   0x0000FF00,  // Green
  21.   0x0066FF66,  // Lt. Green
  22.   0x000000FF,  // Blue
  23.   0x0099CCFF,  // Lt. Blue
  24.   0x00FFFFFF,  // White
  25.   0x00FFFF00,  // Yellow
  26.   0x00FFFF99,  // Lt. Yellow
  27.   0x00FF66FF,  // Pink
  28.   0x00FFCCFF   // Lt. Pink
  29. };
  30.  
  31. CRGB leds[NEO_COUNT];
  32.  
  33. struct Timer{
  34.   bool state;
  35.   uint32_t nextUpdateMillis;
  36. };
  37. Timer* timer;
  38.  
  39. // ==============================================================================================
  40.  
  41. void setup()
  42. {
  43.  
  44.   // Set up FastLED
  45.   FastLED.addLeds<WS2812, NEO_PIN, RGB>(leds, NEO_COUNT);  // RGB ordering
  46.  
  47.   FastLED.setBrightness(NEO_BRIGHTNESS);
  48.  
  49.   timer = new Timer[NEO_COUNT];
  50.  
  51.   for (size_t i = 0; i < NEO_COUNT; i++)
  52.   {
  53.     timer[i].state = 0;   // start with all Neos off, and initial timings
  54.     leds[i] = 0x00000000; // Black
  55.     timer[i].nextUpdateMillis = millis() + random(MIN_RANDOM_NUM, MAX_RANDOM_NUM);
  56.   }
  57.  
  58.   // if analog input pin 1 is unconnected, random analog
  59.   // noise will cause the call to randomSeed() to generate
  60.   // different seed numbers each time the sketch runs.
  61.   // randomSeed() will then shuffle the random function.
  62.   randomSeed(analogRead(A1));
  63.  
  64. }
  65.  
  66. // ==============================================================================================
  67.  
  68. void loop()
  69. {
  70.  
  71.   unsigned long currentTimeMS = millis();
  72.  
  73.   if ( (currentTimeMS >= (2000)) && (waitForAmberLEDStartup == true) ) {
  74.     waitForAmberLEDStartup  = false;
  75.     showNeoPixelBlinkies    = true;                
  76.   }
  77.  
  78.   if ( showNeoPixelBlinkies == true ) {
  79.     updateNEOs();
  80.   }
  81.   FastLED.show();
  82.  
  83. }
  84.  
  85.  
  86. void updateNEOs() {
  87.   const uint32_t interval = 2;
  88.   static uint32_t last = 0;
  89.   uint32_t now = millis();
  90.   bool dirty = false;
  91.  
  92.   if (now - last >= interval) {
  93.     last = now;
  94.     for (size_t i = 0; i < NEO_COUNT; i++)
  95.     {
  96.       if (millis() >= timer[i].nextUpdateMillis)
  97.       {
  98.         dirty = true;
  99.         if (timer[i].state)
  100.         {
  101.           leds[i] = 0x00000000; // Black (off)
  102.         }
  103.         else
  104.         {
  105.           leds[i] = colors[random(sizeof(colors) / sizeof(uint32_t))]; // random colour
  106.         }
  107.         timer[i].state = !timer[i].state;
  108.         timer[i].nextUpdateMillis = millis() + random(MIN_RANDOM_NUM, MAX_RANDOM_NUM);
  109.       }
  110.     }
  111.   }
  112.  
  113. }
  114.  
Advertisement
Add Comment
Please, Sign In to add comment