Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Adafruit_NeoPixel.h>
- // Parameters
- // -- on a bigger project, I would move this to a separate "params.h" file
- // -- which would need to be #included like the Adafruit_NeoPixel.h file was
- #define LED_STRIP_PIN 10
- #define LED_STRIP_COUNT 6
- #define SWITCH_PIN 2
- // State Machine
- enum StateMachine { OFF, INIT_BLUE, BLUE, INIT_RED, RED, INIT_OFF };
- StateMachine state;
- // Create the NeoPixel strip
- Adafruit_NeoPixel strip(LED_STRIP_COUNT, LED_STRIP_PIN, NEO_GRB + NEO_KHZ800);
- // Argument 1 = Number of pixels in NeoPixel strip
- // Argument 2 = Arduino pin number (most are valid)
- // Argument 3 = Pixel type flags, add together as needed:
- // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
- // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
- // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
- // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
- // NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
- void setup() {
- // Initialize the strip
- strip.begin();
- // Set all LEDs to off
- strip.clear();
- strip.show();
- // Set LED brightness to ~20% (50/255 = ~20%)
- strip.setBrightness(50);
- // Initialize the switch
- // -- I use INPUT_PULLUP so the switch only needs to be connected to ground
- // -- Pin will read HIGH when switch is open
- pinMode(SWITCH_PIN, INPUT_PULLUP);
- // Set the initial state of the state machine
- state = INIT_BLUE;
- }
- void loop() {
- switch (state) {
- case (INIT_OFF):
- // Set all LEDs to off
- strip.clear();
- strip.show();
- // Set state machine to OFF
- state = OFF;
- break;
- case (OFF):
- // Do nothing because it's turned off
- // -- Code to wake up again, if that function is required
- // -- which it's not, so do nothing
- break;
- case (INIT_BLUE):
- // Set all LEDs to blue
- strip.fill(strip.Color(0, 0, 128), 0, LED_STRIP_COUNT);
- strip.show();
- // Set state machine to BLUE
- state = BLUE;
- case (BLUE):
- // If the SWITCH_PIN state is LOW
- // aka the switch is closed
- if (digitalRead(SWITCH_PIN) == LOW) {
- // change state to INIT_RED
- state = INIT_RED;
- }
- // otherwise do nothing
- break;
- case (INIT_RED):
- // Set all LEDs to red
- strip.fill(strip.Color(128, 0, 0), 0, LED_STRIP_COUNT);
- strip.show();
- // Set state machine to RED
- state = RED;
- case (RED):
- // If the SWITCH_PIN state is HIGH
- // aka the switch is open
- if (digitalRead(SWITCH_PIN) == HIGH) {
- // change state to INIT_BLUE
- state == INIT_BLUE;
- }
- // otherwise do nothing
- break;
- default:
- // If the state machine is in an unknown state somehow
- // set the state to INIT_BLUE
- state = INIT_BLUE;
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement