S_Robertson

Rainbow/Blue NeoPixel Code

Nov 21st, 2020 (edited)
1,180
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import <Adafruit_NeoPixel.h>;
  2.  
  3. //---------------------------
  4. // Parameters
  5. // NeoPixel parameters
  6. #define LED_PIN           3
  7. #define LED_COUNT         12
  8. #define LED_BRIGHTNESS    64
  9.  
  10. // Define the input pin
  11. #define BUTTON_PIN        2
  12.  
  13. // Define the delay length (aka speed; higher numbers are slower)
  14. #define DELAY_LENGTH_RAINBOW      10
  15. #define DELAY_LENGTH_BLUE         150
  16.  
  17.  
  18. //#define DEBUG
  19. //---------------------------
  20.  
  21. //---------------------------
  22. // Globals
  23. // Create the strip state machine
  24. enum StateMachine { OFF, INIT_RAINBOW, RAINBOW, UPDATE_RAINBOW, INIT_BLUE, BLUE, UPDATE_BLUE, INIT_OFF };
  25. StateMachine strip_state;
  26.  
  27. // Create the timer
  28. unsigned long timer;
  29.  
  30. // Create the rainbow color index
  31. int index;
  32.  
  33. // Create the debounce flag for the button
  34. bool debounce;
  35.  
  36. // Create the strip
  37. Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
  38. //---------------------------
  39.  
  40. void setup() {
  41.   // Initialize and clear the strip
  42.   strip.begin();
  43.   strip.setBrightness(LED_BRIGHTNESS);
  44.   strip.clear();
  45.  
  46.   // Set the state of the LED strip to INIT_RAINBOW
  47.   strip_state = INIT_RAINBOW;
  48.  
  49.   // Set the rainbow index to 0
  50.   index = 0;
  51.  
  52.   // Initialize the button
  53.   pinMode(BUTTON_PIN, INPUT_PULLUP);
  54.  
  55.   // Initialize the debounce flag;
  56.   debounce = 0;
  57.  
  58. #ifdef DEBUG
  59.   //Init serial
  60.   Serial.begin(9600);
  61. #endif
  62. }
  63.  
  64. void loop() {
  65.   // Run the state machine
  66.   switch (strip_state) {
  67.     //    State: INIT_OFF
  68.     case INIT_OFF:
  69. #ifdef DEBUG
  70.       Serial.println("STATE: OFF");
  71. #endif
  72.       //    Turn all LEDs off
  73.       strip.clear();
  74.       strip.show();
  75.       //    Set the state to OFF
  76.       strip_state = OFF;
  77.  
  78.     //    State: OFF
  79.     case OFF:
  80.       break;
  81.  
  82.     //    State: INIT_RAINBOW
  83.     case INIT_RAINBOW:
  84. #ifdef DEBUG
  85.       Serial.println("STATE: RAINBOW");
  86. #endif
  87.  
  88.       // Set timer
  89.       timer = millis() + DELAY_LENGTH_RAINBOW;
  90.  
  91.       // Set the state to RAINBOW
  92.       strip_state = RAINBOW;
  93.  
  94.     // State: RAINBOW
  95.     case RAINBOW:
  96.       // If timer has expired, update the effect
  97.       if (millis() > timer) {
  98.         strip_state = UPDATE_RAINBOW;
  99.       }
  100.  
  101.       //      Check if the debounce flag is still set
  102.       if ( debounce == 1 ) {
  103.         // If so, check if button has been released
  104.         if ( digitalRead(BUTTON_PIN) == HIGH ) {
  105.           debounce = 0;
  106.         }
  107.       } else {
  108.         //    Check if button has been pressed
  109.         if ( digitalRead(BUTTON_PIN) == LOW ) {
  110.           //      If so, set the state to INIT_BLUE
  111.           strip_state = INIT_BLUE;
  112.         }
  113.       }
  114.  
  115.       break;
  116.  
  117.     case UPDATE_RAINBOW:
  118.  
  119. #ifdef DEBUG
  120.       Serial.println("STATE: RAINBOW_UPDATE");
  121. #endif
  122.  
  123.       // Set timer
  124.       timer = millis() + DELAY_LENGTH_RAINBOW;
  125.  
  126. #ifdef DEBUG
  127.       Serial.print("Millis: ");
  128.       Serial.print(millis());
  129.       Serial.print("     Timer: ");
  130.       Serial.print(timer);
  131.       Serial.println();
  132. #endif
  133.  
  134.       // Update the rainbow effect
  135.       index++;
  136.       //    Fill the strip with rainbow colors
  137.       for (int i = 0; i < LED_COUNT; i++) {
  138.         strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + index) & 255));
  139.       }
  140.       // Display effect
  141.       strip.show();
  142.  
  143.       strip_state = RAINBOW;
  144.  
  145.       break;
  146.  
  147.     //    State: INIT_BLUE
  148.     case INIT_BLUE:
  149. #ifdef DEBUG
  150.       Serial.println("STATE: BLUE");
  151. #endif
  152.       // Set the debounce flag
  153.       debounce = 1;
  154.  
  155.       // Set timer
  156.       timer = millis() + DELAY_LENGTH_BLUE;
  157.  
  158.       // Set the state to BLUE
  159.       strip_state = BLUE;
  160.  
  161.     //    State: BLUE
  162.     case BLUE:
  163.       if (millis() > timer) {
  164.         strip_state = UPDATE_BLUE;
  165.       }
  166.  
  167.       //      Check if the debounce flag is still set
  168.       if ( debounce == 1 ) {
  169.         // If so, check if button has been released
  170.         if ( digitalRead(BUTTON_PIN) == HIGH ) {
  171.           debounce = 0;
  172.         }
  173.       } else {
  174.         if ( digitalRead(BUTTON_PIN) == LOW ) {
  175.           debounce = 1;
  176.           strip_state = INIT_RAINBOW;
  177.         }
  178.       }
  179.  
  180.       break;
  181.  
  182.     case UPDATE_BLUE:
  183.  
  184.       // Set timer
  185.       timer = millis() + DELAY_LENGTH_BLUE;
  186.  
  187.       // Increment the index counter
  188.       index++;
  189.  
  190.       //    Fill the strip with the blue effect
  191.       for (int i = 0; i < LED_COUNT; i++) {
  192.         strip.setPixelColor(i, strip.Color(0, 0, (((i + index) * 256 / strip.numPixels()) & 255)));
  193.       }
  194.       // Display effect
  195.       strip.show();
  196.  
  197.       strip_state = BLUE;
  198.  
  199.       break;
  200.  
  201.     //  Default State: Change to INIT_RAINBOW
  202.     //    We should never end up here
  203.     //    .."should"..
  204.     default:
  205. #ifdef DEBUG
  206.       Serial.println("STATE: DEFAULT");
  207. #endif
  208.       strip_state = INIT_RAINBOW;
  209.       break;
  210.   }
  211. }
  212.  
  213.  
  214. // Input a value 0 to 255 to get a color value.
  215. // The colours are a transition r - g - b - back to r.
  216. uint32_t Wheel(byte WheelPos)
  217. {
  218.   WheelPos = 255 - WheelPos;
  219.   if (WheelPos < 85)
  220.   {
  221.     return Adafruit_NeoPixel::Color(255 - WheelPos * 3, 0, WheelPos * 3);
  222.   }
  223.   else if (WheelPos < 170)
  224.   {
  225.     WheelPos -= 85;
  226.     return Adafruit_NeoPixel::Color(0, WheelPos * 3, 255 - WheelPos * 3);
  227.   }
  228.   else
  229.   {
  230.     WheelPos -= 170;
  231.     return Adafruit_NeoPixel::Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  232.   }
  233. }
RAW Paste Data