S_Robertson

Rainbow/Blue NeoPixel Code v0.02

Nov 21st, 2020
1,229
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.  
  11. // Define the input pin
  12. #define BUTTON_PIN        2
  13.  
  14. // Define the delay length (aka speed; higher numbers are slower)
  15. #define DELAY_LENGTH_RAINBOW      10
  16. #define DELAY_LENGTH_BLUE         150
  17.  
  18. // Define the number of times to repeat each pattern
  19. #define CYCLE_MAX_RAINBOW         75
  20. #define CYCLE_MAX_BLUE            5
  21. #define CYCLE_MAX                 1474559   // 1,474,559 is the max possible value before the int rolls over into negatives and the comparison breaks.
  22.  
  23.  
  24. #define DEBUG
  25. //---------------------------
  26.  
  27. //---------------------------
  28. // Globals
  29. // Create the strip state machine
  30. enum StateMachine { OFF, INIT_RAINBOW, RAINBOW, UPDATE_RAINBOW, INIT_BLUE, BLUE, UPDATE_BLUE, INIT_OFF };
  31. StateMachine strip_state;
  32.  
  33. // Create the timer
  34. unsigned long timer;
  35.  
  36. // Create the color index
  37. int index;
  38. // Create the cycle counter
  39. int cycle_count;
  40.  
  41. // Create the debounce flag for the button
  42. bool debounce;
  43.  
  44.  
  45. // Create the strip
  46. Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
  47. //---------------------------
  48.  
  49. void setup() {
  50.   // Initialize and clear the strip
  51.   strip.begin();
  52.   strip.setBrightness(LED_BRIGHTNESS);
  53.   strip.clear();
  54.  
  55.   // Set the state of the LED strip to INIT_RAINBOW
  56.   strip_state = INIT_RAINBOW;
  57.  
  58.   // Set the rainbow index to 0
  59.   index = 0;
  60.  
  61.   // Initialize the button
  62.   pinMode(BUTTON_PIN, INPUT_PULLUP);
  63.  
  64.   // Initialize the debounce flag;
  65.   debounce = 0;
  66.  
  67. #ifdef DEBUG
  68.   //Init serial
  69.   Serial.begin(9600);
  70. #endif
  71. }
  72.  
  73. void loop() {
  74.   // Run the state machine
  75.   switch (strip_state) {
  76.     //    State: INIT_OFF
  77.     case INIT_OFF:
  78. #ifdef DEBUG
  79.       Serial.println("STATE: OFF");
  80. #endif
  81.       //    Turn all LEDs off
  82.       strip.clear();
  83.       strip.show();
  84.       //    Set the state to OFF
  85.       strip_state = OFF;
  86.  
  87.     //    State: OFF
  88.     case OFF:
  89.       break;
  90.  
  91.     //    State: INIT_RAINBOW
  92.     case INIT_RAINBOW:
  93. #ifdef DEBUG
  94.       Serial.println("STATE: RAINBOW");
  95. #endif
  96.  
  97.       // Set timer
  98.       timer = millis() + DELAY_LENGTH_RAINBOW;
  99.  
  100.       // Set the state to RAINBOW
  101.       strip_state = RAINBOW;
  102.  
  103.     // State: RAINBOW
  104.     case RAINBOW:
  105.       // If timer has expired, update the effect
  106.       if (millis() > timer) {
  107.         strip_state = UPDATE_RAINBOW;
  108.       }
  109.  
  110.       checkButton(BUTTON_PIN);
  111.  
  112.       if (cycle_count >= CYCLE_MAX_RAINBOW) {
  113.         cycle_count = 0;
  114.         index = index % 256;
  115.         strip_state = INIT_BLUE;
  116.       }
  117.  
  118.       break;
  119.  
  120.     case UPDATE_RAINBOW:
  121.  
  122. #ifdef DEBUG
  123.       Serial.println("STATE: RAINBOW_UPDATE");
  124. #endif
  125.  
  126.       // Set timer
  127.       timer = millis() + DELAY_LENGTH_RAINBOW;
  128.  
  129. #ifdef DEBUG
  130.       Serial.print("Millis: ");
  131.       Serial.print(millis());
  132.       Serial.print("     Timer: ");
  133.       Serial.print(timer);
  134.       Serial.print("     Cycle: ");
  135.       Serial.print(cycle_count);
  136.       Serial.println();
  137. #endif
  138.  
  139.       updateStrip(strip);
  140.  
  141.       strip_state = RAINBOW;
  142.  
  143.       break;
  144.  
  145.     //    State: INIT_BLUE
  146.     case INIT_BLUE:
  147. #ifdef DEBUG
  148.       Serial.println("STATE: BLUE");
  149. #endif
  150.  
  151.       // Set timer
  152.       timer = millis() + DELAY_LENGTH_BLUE;
  153.  
  154.       // Set the state to BLUE
  155.       strip_state = BLUE;
  156.  
  157.     //    State: BLUE
  158.     case BLUE:
  159.       if (millis() > timer) {
  160.         strip_state = UPDATE_BLUE;
  161.       }
  162.  
  163.       checkButton(BUTTON_PIN);
  164.  
  165.       if (cycle_count >= CYCLE_MAX_BLUE) {
  166.         cycle_count = 0;
  167.         index = index % 256;
  168.         strip_state = INIT_RAINBOW;
  169.       }
  170.  
  171.       break;
  172.  
  173.     case UPDATE_BLUE:
  174.  
  175. #ifdef DEBUG
  176.       Serial.println("STATE: BLUE_UPDATE");
  177. #endif
  178.  
  179.       // Set timer
  180.       timer = millis() + DELAY_LENGTH_BLUE;
  181.  
  182. #ifdef DEBUG
  183.       Serial.print("Millis: ");
  184.       Serial.print(millis());
  185.       Serial.print("     Timer: ");
  186.       Serial.print(timer);
  187.       Serial.print("     Cycle: ");
  188.       Serial.print(cycle_count);
  189.       Serial.println();
  190. #endif
  191.  
  192.       updateStrip(strip);
  193.  
  194.       strip_state = BLUE;
  195.  
  196.       break;
  197.  
  198.     //  Default State: Change to INIT_RAINBOW
  199.     //    We should never end up here
  200.     //    .."should"..
  201.     default:
  202. #ifdef DEBUG
  203.       Serial.println("STATE: DEFAULT");
  204. #endif
  205.       strip_state = INIT_RAINBOW;
  206.       break;
  207.   }
  208. }
  209.  
  210. void checkButton(int _pin) {
  211.  
  212.   //      Check if the debounce flag is still set
  213.   if ( debounce == 1 ) {
  214.     // If so, check if button has been released
  215.     if ( digitalRead(_pin) == HIGH ) {
  216.       debounce = 0;
  217. #ifdef DEBUG
  218.       Serial.print("Button ");
  219.       Serial.print(_pin);
  220.       Serial.println(" released");
  221. #endif
  222.     }
  223.   } else {
  224.     //    Check if button has been pressed
  225.     if ( digitalRead(_pin) == LOW ) {
  226.       //      If so, set the cycle_count to it's maximum value
  227.       cycle_count = CYCLE_MAX;
  228.       // Set the debounce flag
  229.       debounce = 1;
  230.  
  231. #ifdef DEBUG
  232.       Serial.print("Button ");
  233.       Serial.print(_pin);
  234.       Serial.println(" activated");
  235. #endif
  236.     }
  237.   }
  238. }
  239.  
  240. void updateStrip(Adafruit_NeoPixel &_strip) {
  241.   index++;
  242.   // Check if the cycle has completed
  243.   if (index >= 256) {
  244.     index = index % 256;
  245.     cycle_count++;
  246.   }
  247.  
  248.   //    Fill the strip with colors
  249.   for (int i = 0; i < LED_COUNT; i++) {
  250.     //_strip.setPixelColor(i, Wheel(((i * 256 / _strip.numPixels()) + index) & 255) );
  251.     _strip.setPixelColor(i, getColorByState(strip.numPixels(), strip_state, i) );
  252.   }
  253.   // Display effect
  254.   _strip.show();
  255. }
  256.  
  257.  
  258. uint32_t getColorByState(int _maxPixels, StateMachine _state, int _i) {
  259.   switch (_state) {
  260.     case INIT_OFF:
  261.     case OFF:
  262.       return Adafruit_NeoPixel::Color(0, 0, 0);
  263.       break;
  264.     case INIT_RAINBOW:
  265.     case RAINBOW:
  266.     case UPDATE_RAINBOW:
  267.       return Wheel(((_i * 256 / _maxPixels) + index) & 255);
  268.       break;
  269.     case INIT_BLUE:
  270.     case BLUE:
  271.     case UPDATE_BLUE:
  272.       return Adafruit_NeoPixel::Color(0, 0, (((_i + index) * 256 / _maxPixels) & 255));
  273.       break;
  274.     default:
  275.       return Adafruit_NeoPixel::Color(0, 0, 0);
  276.       break;
  277.   }
  278. }
  279.  
  280.  
  281.  
  282. // Color FX
  283.  
  284. // Input a value 0 to 255 to get a color value.
  285. // The colours are a transition r - g - b - back to r.
  286. uint32_t Wheel(byte WheelPos)
  287. {
  288.   WheelPos = 255 - WheelPos;
  289.   if (WheelPos < 85)
  290.   {
  291.     return Adafruit_NeoPixel::Color(255 - WheelPos * 3, 0, WheelPos * 3);
  292.   }
  293.   else if (WheelPos < 170)
  294.   {
  295.     WheelPos -= 85;
  296.     return Adafruit_NeoPixel::Color(0, WheelPos * 3, 255 - WheelPos * 3);
  297.   }
  298.   else
  299.   {
  300.     WheelPos -= 170;
  301.     return Adafruit_NeoPixel::Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  302.   }
  303. }
RAW Paste Data