Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import <Adafruit_NeoPixel.h>;
- //---------------------------
- // Parameters
- // NeoPixel parameters
- #define LED_PIN 3
- #define LED_COUNT 12
- #define LED_BRIGHTNESS 64
- // Define the input pin
- #define BUTTON_PIN 2
- // Define the delay length (aka speed; higher numbers are slower)
- #define DELAY_LENGTH_RAINBOW 10
- #define DELAY_LENGTH_BLUE 150
- //#define DEBUG
- //---------------------------
- //---------------------------
- // Globals
- // Create the strip state machine
- enum StateMachine { OFF, INIT_RAINBOW, RAINBOW, UPDATE_RAINBOW, INIT_BLUE, BLUE, UPDATE_BLUE, INIT_OFF };
- StateMachine strip_state;
- // Create the timer
- unsigned long timer;
- // Create the rainbow color index
- int index;
- // Create the debounce flag for the button
- bool debounce;
- // 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_RAINBOW
- strip_state = INIT_RAINBOW;
- // Set the rainbow index to 0
- index = 0;
- // Initialize the button
- pinMode(BUTTON_PIN, INPUT_PULLUP);
- // Initialize the debounce flag;
- debounce = 0;
- #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 the state to OFF
- strip_state = OFF;
- // State: OFF
- case OFF:
- break;
- // State: INIT_RAINBOW
- case INIT_RAINBOW:
- #ifdef DEBUG
- Serial.println("STATE: RAINBOW");
- #endif
- // Set timer
- timer = millis() + DELAY_LENGTH_RAINBOW;
- // Set the state to RAINBOW
- strip_state = RAINBOW;
- // State: RAINBOW
- case RAINBOW:
- // If timer has expired, update the effect
- if (millis() > timer) {
- strip_state = UPDATE_RAINBOW;
- }
- // Check if the debounce flag is still set
- if ( debounce == 1 ) {
- // If so, check if button has been released
- if ( digitalRead(BUTTON_PIN) == HIGH ) {
- debounce = 0;
- }
- } else {
- // Check if button has been pressed
- if ( digitalRead(BUTTON_PIN) == LOW ) {
- // If so, set the state to INIT_BLUE
- strip_state = INIT_BLUE;
- }
- }
- break;
- case UPDATE_RAINBOW:
- #ifdef DEBUG
- Serial.println("STATE: RAINBOW_UPDATE");
- #endif
- // Set timer
- timer = millis() + DELAY_LENGTH_RAINBOW;
- #ifdef DEBUG
- Serial.print("Millis: ");
- Serial.print(millis());
- Serial.print(" Timer: ");
- Serial.print(timer);
- Serial.println();
- #endif
- // Update the rainbow effect
- index++;
- // Fill the strip with rainbow colors
- for (int i = 0; i < LED_COUNT; i++) {
- strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + index) & 255));
- }
- // Display effect
- strip.show();
- strip_state = RAINBOW;
- break;
- // State: INIT_BLUE
- case INIT_BLUE:
- #ifdef DEBUG
- Serial.println("STATE: BLUE");
- #endif
- // Set the debounce flag
- debounce = 1;
- // Set timer
- timer = millis() + DELAY_LENGTH_BLUE;
- // Set the state to BLUE
- strip_state = BLUE;
- // State: BLUE
- case BLUE:
- if (millis() > timer) {
- strip_state = UPDATE_BLUE;
- }
- // Check if the debounce flag is still set
- if ( debounce == 1 ) {
- // If so, check if button has been released
- if ( digitalRead(BUTTON_PIN) == HIGH ) {
- debounce = 0;
- }
- } else {
- if ( digitalRead(BUTTON_PIN) == LOW ) {
- debounce = 1;
- strip_state = INIT_RAINBOW;
- }
- }
- break;
- case UPDATE_BLUE:
- // Set timer
- timer = millis() + DELAY_LENGTH_BLUE;
- // Increment the index counter
- index++;
- // Fill the strip with the blue effect
- for (int i = 0; i < LED_COUNT; i++) {
- strip.setPixelColor(i, strip.Color(0, 0, (((i + index) * 256 / strip.numPixels()) & 255)));
- }
- // Display effect
- strip.show();
- strip_state = BLUE;
- break;
- // Default State: Change to INIT_RAINBOW
- // We should never end up here
- // .."should"..
- default:
- #ifdef DEBUG
- Serial.println("STATE: DEFAULT");
- #endif
- strip_state = INIT_RAINBOW;
- break;
- }
- }
- // Input a value 0 to 255 to get a color value.
- // The colours are a transition r - g - b - back to r.
- uint32_t Wheel(byte WheelPos)
- {
- WheelPos = 255 - WheelPos;
- if (WheelPos < 85)
- {
- return Adafruit_NeoPixel::Color(255 - WheelPos * 3, 0, WheelPos * 3);
- }
- else if (WheelPos < 170)
- {
- WheelPos -= 85;
- return Adafruit_NeoPixel::Color(0, WheelPos * 3, 255 - WheelPos * 3);
- }
- else
- {
- WheelPos -= 170;
- return Adafruit_NeoPixel::Color(WheelPos * 3, 255 - WheelPos * 3, 0);
- }
- }
RAW Paste Data