Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import <Adafruit_NeoPixel.h>;
- //---------------------------
- // Parameters
- // NeoPixel parameters
- #define LED_PIN 2
- #define LED_COUNT 10
- #define LED_BRIGHTNESS 64
- // Define the LED color
- #define LED_COLOR_R 64
- #define LED_COLOR_G 128
- #define LED_COLOR_B 255
- // LED maximum length of LEDs to flash
- #define LED_LENGTH_MIN 1
- #define LED_LENGTH_MAX 10
- // Define timing variables
- #define DELAY_OFF_MIN 150
- #define DELAY_OFF_MAX 10000
- #define DELAY_ON_MIN 25
- #define DELAY_ON_MAX 100
- #define DELAY_FADE_MIN 75
- #define DELAY_FADE_MAX 750
- //#define DEBUG
- //---------------------------
- //---------------------------
- // Globals
- // Create the strip state machine
- enum StateMachine { OFF, INIT_FLASH, FLASH, INIT_FADE, FADE, INIT_OFF };
- StateMachine strip_state;
- // Create the timer
- unsigned long timer;
- int delay_length;
- // Create LED position and length variables
- int led_position, led_length;
- // Create the strip
- Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
- //---------------------------
- void setup() {
- // Initialize and clear the strip
- strip.begin();
- strip.setBrightness(LED_BRIGHTNESS);
- strip.clear();
- // Set the state of the LED strip to INIT_OFF
- strip_state = INIT_OFF;
- #ifdef DEBUG
- //Init serial
- Serial.begin(9600);
- #endif
- }
- void loop() {
- // Run the state machine
- switch (strip_state) {
- // State: INIT_OFF
- case INIT_OFF:
- #ifdef DEBUG
- Serial.println("STATE: OFF");
- #endif
- // Turn all LEDs off
- strip.clear();
- strip.show();
- // Set a timer until the next flash
- delay_length = random(DELAY_OFF_MIN, DELAY_OFF_MAX);
- timer = millis() + delay_length;
- // Set the state to OFF
- strip_state = OFF;
- // State: OFF
- case OFF:
- // Check if the timer has expired
- if ( millis() > timer ) {
- // If so, initialise a new lightning flash
- strip_state = INIT_FLASH;
- }
- break;
- // State: INIT_FLASH
- case INIT_FLASH:
- #ifdef DEBUG
- Serial.println("STATE: FLASH");
- #endif
- // Pick a random length of pixels
- led_length = random(LED_LENGTH_MIN, LED_LENGTH_MAX);
- led_position = random(0, LED_COUNT - led_length);
- #ifdef DEBUG
- Serial.print("POS: ");
- Serial.print(led_position);
- Serial.print("; LEN: ");
- Serial.print(led_length);
- Serial.println();
- #endif
- // Fill the strip with the lightning color
- strip.fill(Adafruit_NeoPixel::Color(LED_COLOR_R, LED_COLOR_G, LED_COLOR_B), led_position, led_length);
- strip.show();
- // Set a timer until the lightning starts to fade
- delay_length = random(DELAY_ON_MIN, DELAY_ON_MAX);
- timer = millis() + delay_length;
- // Set the state to FLASH
- strip_state = FLASH;
- // State: FLASH
- case FLASH:
- // Check if the timer has expired
- if ( millis() > timer ) {
- // If so, set the state to INIT_FADE
- strip_state = INIT_FADE;
- }
- break;
- // State: INIT_FADE
- case INIT_FADE:
- #ifdef DEBUG
- Serial.println("STATE: FADE");
- #endif
- // Set a timer until the flash has faded
- delay_length = random(DELAY_FADE_MIN, DELAY_FADE_MAX);
- timer = millis() + delay_length;
- strip_state = FADE;
- // State: FADE
- case FADE:
- // Check if timer has expired
- if ( millis() > timer ) {
- // If it has, set state to INIT_OFF
- strip_state = INIT_OFF;
- } else {
- // Otherwise, calculate how bright the LEDs should be, based on elapsed time since flash
- float brightness = 1.0 * ( timer - millis() ) / delay_length;
- // and fill the strip with that color
- strip.fill(Adafruit_NeoPixel::Color(brightness * LED_COLOR_R, brightness * LED_COLOR_G, brightness * LED_COLOR_B), led_position, led_length );
- strip.show();
- }
- break;
- // Default State: Change to INIT_OFF
- // We should never end up here
- // .."should"..
- default:
- #ifdef DEBUG
- Serial.println("STATE: DEFAULT");
- #endif
- strip_state = INIT_OFF;
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement