Advertisement
S_Robertson

NeoPixel strip Example

Jun 4th, 2020
1,612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Adafruit_NeoPixel.h>
  2.  
  3. // Parameters
  4. // -- on a bigger project, I would move this to a separate "params.h" file
  5. // -- which would need to be #included like the Adafruit_NeoPixel.h file was
  6. #define LED_STRIP_PIN     10
  7. #define LED_STRIP_COUNT   6
  8. #define SWITCH_PIN        2
  9.  
  10. // State Machine
  11. enum StateMachine { OFF, INIT_BLUE, BLUE, INIT_RED, RED, INIT_OFF };
  12. StateMachine state;
  13.  
  14. // Create the NeoPixel strip
  15. Adafruit_NeoPixel strip(LED_STRIP_COUNT, LED_STRIP_PIN, NEO_GRB + NEO_KHZ800);
  16. // Argument 1 = Number of pixels in NeoPixel strip
  17. // Argument 2 = Arduino pin number (most are valid)
  18. // Argument 3 = Pixel type flags, add together as needed:
  19. //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  20. //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  21. //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
  22. //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  23. //   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
  24.  
  25.  
  26. void setup() {
  27.   // Initialize the strip
  28.   strip.begin();
  29.   // Set all LEDs to off
  30.   strip.clear();
  31.   strip.show();
  32.   // Set LED brightness to ~20% (50/255 = ~20%)
  33.   strip.setBrightness(50);
  34.  
  35.   // Initialize the switch
  36.   // -- I use INPUT_PULLUP so the switch only needs to be connected to ground
  37.   // -- Pin will read HIGH when switch is open
  38.   pinMode(SWITCH_PIN, INPUT_PULLUP);
  39.  
  40.   // Set the initial state of the state machine
  41.   state = INIT_BLUE;
  42. }
  43.  
  44. void loop() {
  45.   switch (state) {
  46.     case (INIT_OFF):
  47.       // Set all LEDs to off
  48.       strip.clear();
  49.       strip.show();
  50.       // Set state machine to OFF
  51.       state = OFF;
  52.       break;
  53.     case (OFF):
  54.       // Do nothing because it's turned off
  55.       // -- Code to wake up again, if that function is required
  56.       // -- which it's not, so do nothing
  57.       break;
  58.     case (INIT_BLUE):
  59.       // Set all LEDs to blue
  60.       strip.fill(strip.Color(0, 0, 128), 0, LED_STRIP_COUNT);
  61.       strip.show();
  62.       // Set state machine to BLUE
  63.       state = BLUE;
  64.     case (BLUE):
  65.       // If the SWITCH_PIN state is LOW
  66.       // aka the switch is closed
  67.       if (digitalRead(SWITCH_PIN) == LOW) {
  68.         // change state to INIT_RED
  69.         state = INIT_RED;
  70.       }
  71.       // otherwise do nothing
  72.       break;
  73.     case (INIT_RED):
  74.       // Set all LEDs to red
  75.       strip.fill(strip.Color(128, 0, 0), 0, LED_STRIP_COUNT);
  76.       strip.show();
  77.       // Set state machine to RED
  78.       state = RED;
  79.     case (RED):
  80.       // If the SWITCH_PIN state is HIGH
  81.       // aka the switch is open
  82.       if (digitalRead(SWITCH_PIN) == HIGH) {
  83.         // change state to INIT_BLUE
  84.         state == INIT_BLUE;
  85.       }
  86.       // otherwise do nothing
  87.       break;
  88.     default:
  89.       // If the state machine is in an unknown state somehow
  90.       // set the state to INIT_BLUE
  91.       state = INIT_BLUE;
  92.       break;
  93.   }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement