Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. // This is a demonstration on how to use an input device to trigger changes on your neo pixels.
  2. // You should wire a momentary push button to connect from ground to a digital IO pin. When you
  3. // press the button it will change to a new pixel animation. Note that you need to press the
  4. // button once to start the first animation!
  5.  
  6. #include <Adafruit_NeoPixel.h>
  7.  
  8. #define BUTTON_PIN 2 // Digital IO pin connected to the button. This will be
  9. // driven with a pull-up resistor so the switch should
  10. // pull the pin to ground momentarily. On a high -> low
  11. // transition the button press logic will execute.
  12.  
  13. #define PIXEL_PIN 6 // Digital IO pin connected to the NeoPixels.
  14.  
  15. #define PIXEL_COUNT 60
  16.  
  17. #define Blinker_left 3 // Digitial pin that will flash left side of leds as long as button as hold
  18. #define Blinker_right 4 // Digitial pin that will flash right side of leds as long as button as hold
  19.  
  20. // Parameter 1 = number of pixels in strip, neopixel stick has 8
  21. // Parameter 2 = pin number (most are valid)
  22. // Parameter 3 = pixel type flags, add together as needed:
  23. // NEO_RGB Pixels are wired for RGB bitstream
  24. // NEO_GRB Pixels are wired for GRB bitstream, correct for neopixel stick
  25. // NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels)
  26. // NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
  27. Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
  28.  
  29. bool oldState = HIGH;
  30. int showType = 0;
  31.  
  32. void setup() {
  33. pinMode(BUTTON_PIN, INPUT_PULLUP);
  34. strip.begin();
  35. strip.show(); // Initialize all pixels to 'off'
  36. }
  37.  
  38. void loop() {
  39. // Get current button state.
  40. bool newState = digitalRead(BUTTON_PIN);
  41.  
  42. // Check if state changed from high to low (button press).
  43. if (newState == LOW && oldState == HIGH) {
  44. // Short delay to debounce button.
  45. delay(20);
  46. // Check if button is still low after debounce.
  47. newState = digitalRead(BUTTON_PIN);
  48. if (newState == LOW) {
  49. showType++;
  50. if (showType > 7)
  51. showType=0;
  52. startShow(showType);
  53. }
  54. }
  55.  
  56. // Set the last button state to the old state.
  57.  
  58. }
  59.  
  60.  
  61. void startShow(int i) {
  62. switch(i){
  63. case 0: colorWipe(strip.Color(0, 0, 0), 50); // Black/off
  64. break;
  65. case 1: colorWipe(strip.Color(255, 0, 0), 50); // Red
  66. break;
  67. case 2: colorWipe(strip.Color(0, 255, 0), 50); // Green
  68. break;
  69. case 3: colorWipe(strip.Color(0, 0, 255), 50); // Blue
  70. break;
  71. case 4: rainbow(20);
  72. break;
  73. case 5: rainbowCycle(20);
  74. break;
  75. case 6: theaterChaseRainbow(50);
  76. break;
  77. }
  78. }
  79.  
  80. // Fill the dots one after the other with a color
  81. void colorWipe(uint32_t c, uint8_t wait) {
  82. for(uint16_t i=0; i<strip.numPixels(); i++) {
  83. strip.setPixelColor(i, c);
  84. strip.show();
  85. delay(wait);
  86. }
  87. }
  88.  
  89. void rainbow(uint8_t wait) {
  90. uint16_t i, j;
  91.  
  92. for(j=0; j<256; j++) {
  93. for(i=0; i<strip.numPixels(); i++) {
  94. strip.setPixelColor(i, Wheel((i+j) & 255));
  95. }
  96. strip.show();
  97. delay(wait);
  98. }
  99. }
  100.  
  101. // Slightly different, this makes the rainbow equally distributed throughout
  102. void rainbowCycle(uint8_t wait) {
  103. uint16_t i, j;
  104.  
  105. for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
  106. for(i=0; i< strip.numPixels(); i++) {
  107. strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  108. }
  109. strip.show();
  110. delay(wait);
  111. }
  112. }
  113.  
  114. //Theatre-style crawling lights.
  115. void theaterChase(uint32_t c, uint8_t wait) {
  116. for (int j=0; j<10; j++) { //do 10 cycles of chasing
  117. for (int q=0; q < 3; q++) {
  118. for (int i=0; i < strip.numPixels(); i=i+3) {
  119. strip.setPixelColor(i+q, c); //turn every third pixel on
  120. }
  121. strip.show();
  122.  
  123. delay(wait);
  124.  
  125. for (int i=0; i < strip.numPixels(); i=i+3) {
  126. strip.setPixelColor(i+q, 0); //turn every third pixel off
  127. }
  128. }
  129. }
  130. }
  131.  
  132. //Theatre-style crawling lights with rainbow effect
  133. void theaterChaseRainbow(uint8_t wait) {
  134. for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel
  135. for (int q=0; q < 3; q++) {
  136. for (int i=0; i < strip.numPixels(); i=i+3) {
  137. strip.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on
  138. }
  139. strip.show();
  140.  
  141. delay(wait);
  142.  
  143. for (int i=0; i < strip.numPixels(); i=i+3) {
  144. strip.setPixelColor(i+q, 0); //turn every third pixel off
  145. }
  146. }
  147. }
  148. }
  149.  
  150. // Input a value 0 to 255 to get a color value.
  151. // The colours are a transition r - g - b - back to r.
  152. uint32_t Wheel(byte WheelPos) {
  153. WheelPos = 255 - WheelPos;
  154. if(WheelPos < 85) {
  155. return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  156. }
  157. if(WheelPos < 170) {
  158. WheelPos -= 85;
  159. return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  160. }
  161. WheelPos -= 170;
  162. return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement