Advertisement
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 the number of times to repeat each pattern
- #define CYCLE_MAX_RAINBOW 75
- #define CYCLE_MAX_BLUE 5
- #define CYCLE_MAX 1474559 // 1,474,559 is the max possible value before the int rolls over into negatives and the comparison breaks.
- #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 color index
- int index;
- // Create the cycle counter
- int cycle_count;
- // 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;
- }
- checkButton(BUTTON_PIN);
- if (cycle_count >= CYCLE_MAX_RAINBOW) {
- cycle_count = 0;
- index = index % 256;
- 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.print(" Cycle: ");
- Serial.print(cycle_count);
- Serial.println();
- #endif
- updateStrip(strip);
- strip_state = RAINBOW;
- break;
- // State: INIT_BLUE
- case INIT_BLUE:
- #ifdef DEBUG
- Serial.println("STATE: BLUE");
- #endif
- // 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;
- }
- checkButton(BUTTON_PIN);
- if (cycle_count >= CYCLE_MAX_BLUE) {
- cycle_count = 0;
- index = index % 256;
- strip_state = INIT_RAINBOW;
- }
- break;
- case UPDATE_BLUE:
- #ifdef DEBUG
- Serial.println("STATE: BLUE_UPDATE");
- #endif
- // Set timer
- timer = millis() + DELAY_LENGTH_BLUE;
- #ifdef DEBUG
- Serial.print("Millis: ");
- Serial.print(millis());
- Serial.print(" Timer: ");
- Serial.print(timer);
- Serial.print(" Cycle: ");
- Serial.print(cycle_count);
- Serial.println();
- #endif
- updateStrip(strip);
- 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;
- }
- }
- void checkButton(int _pin) {
- // Check if the debounce flag is still set
- if ( debounce == 1 ) {
- // If so, check if button has been released
- if ( digitalRead(_pin) == HIGH ) {
- debounce = 0;
- #ifdef DEBUG
- Serial.print("Button ");
- Serial.print(_pin);
- Serial.println(" released");
- #endif
- }
- } else {
- // Check if button has been pressed
- if ( digitalRead(_pin) == LOW ) {
- // If so, set the cycle_count to it's maximum value
- cycle_count = CYCLE_MAX;
- // Set the debounce flag
- debounce = 1;
- #ifdef DEBUG
- Serial.print("Button ");
- Serial.print(_pin);
- Serial.println(" activated");
- #endif
- }
- }
- }
- void updateStrip(Adafruit_NeoPixel &_strip) {
- index++;
- // Check if the cycle has completed
- if (index >= 256) {
- index = index % 256;
- cycle_count++;
- }
- // Fill the strip with colors
- for (int i = 0; i < LED_COUNT; i++) {
- //_strip.setPixelColor(i, Wheel(((i * 256 / _strip.numPixels()) + index) & 255) );
- _strip.setPixelColor(i, getColorByState(strip.numPixels(), strip_state, i) );
- }
- // Display effect
- _strip.show();
- }
- uint32_t getColorByState(int _maxPixels, StateMachine _state, int _i) {
- switch (_state) {
- case INIT_OFF:
- case OFF:
- return Adafruit_NeoPixel::Color(0, 0, 0);
- break;
- case INIT_RAINBOW:
- case RAINBOW:
- case UPDATE_RAINBOW:
- return Wheel(((_i * 256 / _maxPixels) + index) & 255);
- break;
- case INIT_BLUE:
- case BLUE:
- case UPDATE_BLUE:
- return Adafruit_NeoPixel::Color(0, 0, (((_i + index) * 256 / _maxPixels) & 255));
- break;
- default:
- return Adafruit_NeoPixel::Color(0, 0, 0);
- break;
- }
- }
- // Color FX
- // 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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement