Advertisement
S_Robertson

NeoPixel Lightning - Arduino

Aug 10th, 2020
1,549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import <Adafruit_NeoPixel.h>;
  2.  
  3. //---------------------------
  4. // Parameters
  5. // NeoPixel parameters
  6. #define LED_PIN           2
  7. #define LED_COUNT         10
  8. #define LED_BRIGHTNESS    64
  9.  
  10. // Define the LED color
  11. #define LED_COLOR_R       64
  12. #define LED_COLOR_G       128
  13. #define LED_COLOR_B       255
  14.  
  15. // LED maximum length of LEDs to flash
  16. #define LED_LENGTH_MIN    1
  17. #define LED_LENGTH_MAX    10
  18.  
  19. // Define timing variables
  20. #define DELAY_OFF_MIN     150
  21. #define DELAY_OFF_MAX     10000
  22.  
  23. #define DELAY_ON_MIN      25
  24. #define DELAY_ON_MAX      100
  25.  
  26. #define DELAY_FADE_MIN    75
  27. #define DELAY_FADE_MAX    750
  28.  
  29. //#define DEBUG
  30. //---------------------------
  31.  
  32. //---------------------------
  33. // Globals
  34. // Create the strip state machine
  35. enum StateMachine { OFF, INIT_FLASH, FLASH, INIT_FADE, FADE, INIT_OFF };
  36. StateMachine strip_state;
  37.  
  38. // Create the timer
  39. unsigned long timer;
  40. int delay_length;
  41.  
  42. // Create LED position and length variables
  43. int led_position, led_length;
  44.  
  45. // Create the strip
  46. Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
  47. //---------------------------
  48.  
  49. void setup() {
  50.   // Initialize and clear the strip
  51.   strip.begin();
  52.   strip.setBrightness(LED_BRIGHTNESS);
  53.   strip.clear();
  54.  
  55.   // Set the state of the LED strip to INIT_OFF
  56.   strip_state = INIT_OFF;
  57.  
  58. #ifdef DEBUG
  59.   //Init serial
  60.   Serial.begin(9600);
  61. #endif
  62. }
  63.  
  64. void loop() {
  65.   // Run the state machine
  66.   switch (strip_state) {
  67.     //    State: INIT_OFF
  68.     case INIT_OFF:
  69. #ifdef DEBUG
  70.       Serial.println("STATE: OFF");
  71. #endif
  72.       //    Turn all LEDs off
  73.       strip.clear();
  74.       strip.show();
  75.       //    Set a timer until the next flash
  76.       delay_length = random(DELAY_OFF_MIN, DELAY_OFF_MAX);
  77.       timer = millis() + delay_length;
  78.       //    Set the state to OFF
  79.       strip_state = OFF;
  80.  
  81.     //    State: OFF
  82.     case OFF:
  83.       //    Check if the timer has expired
  84.       if ( millis() > timer ) {
  85.         //      If so, initialise a new lightning flash
  86.         strip_state = INIT_FLASH;
  87.       }
  88.       break;
  89.  
  90.     //    State: INIT_FLASH
  91.     case INIT_FLASH:
  92. #ifdef DEBUG
  93.       Serial.println("STATE: FLASH");
  94. #endif
  95.       //    Pick a random length of pixels
  96.       led_length = random(LED_LENGTH_MIN, LED_LENGTH_MAX);
  97.       led_position = random(0, LED_COUNT - led_length);
  98.  
  99. #ifdef DEBUG
  100.       Serial.print("POS: ");
  101.       Serial.print(led_position);
  102.       Serial.print("; LEN: ");
  103.       Serial.print(led_length);
  104.       Serial.println();
  105. #endif
  106.       //    Fill the strip with the lightning color
  107.       strip.fill(Adafruit_NeoPixel::Color(LED_COLOR_R, LED_COLOR_G, LED_COLOR_B), led_position, led_length);
  108.       strip.show();
  109.       //    Set a timer until the lightning starts to fade
  110.       delay_length = random(DELAY_ON_MIN, DELAY_ON_MAX);
  111.       timer = millis() + delay_length;
  112.       //    Set the state to FLASH
  113.       strip_state = FLASH;
  114.  
  115.     //    State: FLASH
  116.     case FLASH:
  117.       //    Check if the timer has expired
  118.       if ( millis() > timer ) {
  119.         //      If so, set the state to INIT_FADE
  120.         strip_state = INIT_FADE;
  121.       }
  122.       break;
  123.  
  124.     //    State: INIT_FADE
  125.     case INIT_FADE:
  126. #ifdef DEBUG
  127.       Serial.println("STATE: FADE");
  128. #endif
  129.       //    Set a timer until the flash has faded
  130.       delay_length = random(DELAY_FADE_MIN, DELAY_FADE_MAX);
  131.       timer = millis() + delay_length;
  132.       strip_state = FADE;
  133.  
  134.     //    State: FADE
  135.     case FADE:
  136.       //      Check if timer has expired
  137.       if ( millis() > timer ) {
  138.         //        If it has, set state to INIT_OFF
  139.         strip_state = INIT_OFF;
  140.       } else {
  141.         //        Otherwise, calculate how bright the LEDs should be, based on elapsed time since flash
  142.         float brightness = 1.0 * ( timer - millis() ) / delay_length;
  143.         //        and fill the strip with that color
  144.         strip.fill(Adafruit_NeoPixel::Color(brightness * LED_COLOR_R, brightness * LED_COLOR_G, brightness * LED_COLOR_B), led_position, led_length );
  145.         strip.show();
  146.       }
  147.       break;
  148.  
  149.     //  Default State: Change to INIT_OFF
  150.     //    We should never end up here
  151.     //    .."should"..
  152.     default:
  153. #ifdef DEBUG
  154.       Serial.println("STATE: DEFAULT");
  155. #endif
  156.       strip_state = INIT_OFF;
  157.       break;
  158.   }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement