PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
4.9KB

  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. #include <Adafruit_NeoPixel.h>
  6. #define BUTTON_PIN 2 // Digital IO pin connected to the button. This will be
  7. // driven with a pull-up resistor so the switch should
  8. // pull the pin to ground momentarily. On a high -> low
  9. // transition the button press logic will execute.
  10. #define PIXEL_PIN 6 // Digital IO pin connected to the NeoPixels.
  11. #define PIXEL_COUNT 16
  12. // Parameter 1 = number of pixels in strip, neopixel stick has 8
  13. // Parameter 2 = pin number (most are valid)
  14. // Parameter 3 = pixel type flags, add together as needed:
  15. // NEO_RGB Pixels are wired for RGB bitstream
  16. // NEO_GRB Pixels are wired for GRB bitstream, correct for neopixel stick
  17. // NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels)
  18. // NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
  19. Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
  20. bool oldState = HIGH;
  21. int showType = 0;
  22. void setup() {
  23. pinMode(BUTTON_PIN, INPUT_PULLUP);
  24. strip.begin();
  25. strip.show(); // Initialize all pixels to 'off'
  26. }
  27. void loop() {
  28. // Get current button state.
  29. bool newState = digitalRead(BUTTON_PIN);
  30. // Check if state changed from high to low (button press).
  31. if (newState == LOW && oldState == HIGH) {
  32. // Short delay to debounce button.
  33. delay(20);
  34. // Check if button is still low after debounce.
  35. newState = digitalRead(BUTTON_PIN);
  36. if (newState == LOW) {
  37. showType++;
  38. if (showType > 9)
  39. showType=0;
  40. startShow(showType);
  41. }
  42. }
  43. // Set the last button state to the old state.
  44. oldState = newState;
  45. }
  46. void startShow(int i) {
  47. switch(i){
  48. case 0: colorWipe(strip.Color(0, 0, 0), 50); // Black/off
  49. break;
  50. case 1: colorWipe(strip.Color(255, 0, 0), 50); // Red
  51. break;
  52. case 2: colorWipe(strip.Color(0, 255, 0), 50); // Green
  53. break;
  54. case 3: colorWipe(strip.Color(0, 0, 255), 50); // Blue
  55. break;
  56. case 4: theaterChase(strip.Color(127, 127, 127), 50); // White
  57. break;
  58. case 5: theaterChase(strip.Color(127, 0, 0), 50); // Red
  59. break;
  60. case 6: theaterChase(strip.Color( 0, 0, 127), 50); // Blue
  61. break;
  62. case 7: rainbow(20);
  63. break;
  64. case 8: rainbowCycle(20);
  65. break;
  66. case 9: theaterChaseRainbow(50);
  67. break;
  68. }
  69. }
  70. // Fill the dots one after the other with a color
  71. void colorWipe(uint32_t c, uint8_t wait) {
  72. for(uint16_t i=0; i<strip.numPixels(); i++) {
  73. strip.setPixelColor(i, c);
  74. strip.show();
  75. delay(wait);
  76. }
  77. }
  78. void rainbow(uint8_t wait) {
  79. uint16_t i, j;
  80. for(j=0; j<256; j++) {
  81. for(i=0; i<strip.numPixels(); i++) {
  82. strip.setPixelColor(i, Wheel((i+j) & 255));
  83. }
  84. strip.show();
  85. delay(wait);
  86. }
  87. }
  88. // Slightly different, this makes the rainbow equally distributed throughout
  89. void rainbowCycle(uint8_t wait) {
  90. uint16_t i, j;
  91. for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
  92. for(i=0; i< strip.numPixels(); i++) {
  93. strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  94. }
  95. strip.show();
  96. delay(wait);
  97. }
  98. }
  99. //Theatre-style crawling lights.
  100. void theaterChase(uint32_t c, uint8_t wait) {
  101. for (int j=0; j<10; j++) { //do 10 cycles of chasing
  102. for (int q=0; q < 3; q++) {
  103. for (int i=0; i < strip.numPixels(); i=i+3) {
  104. strip.setPixelColor(i+q, c); //turn every third pixel on
  105. }
  106. strip.show();
  107. delay(wait);
  108. for (int i=0; i < strip.numPixels(); i=i+3) {
  109. strip.setPixelColor(i+q, 0); //turn every third pixel off
  110. }
  111. }
  112. }
  113. }
  114. //Theatre-style crawling lights with rainbow effect
  115. void theaterChaseRainbow(uint8_t wait) {
  116. for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel
  117. for (int q=0; q < 3; q++) {
  118. for (int i=0; i < strip.numPixels(); i=i+3) {
  119. strip.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on
  120. }
  121. strip.show();
  122. delay(wait);
  123. for (int i=0; i < strip.numPixels(); i=i+3) {
  124. strip.setPixelColor(i+q, 0); //turn every third pixel off
  125. }
  126. }
  127. }
  128. }
  129. // Input a value 0 to 255 to get a color value.
  130. // The colours are a transition r - g - b - back to r.
  131. uint32_t Wheel(byte WheelPos) {
  132. WheelPos = 255 - WheelPos;
  133. if(WheelPos < 85) {
  134. return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  135. }
  136. if(WheelPos < 170) {
  137. WheelPos -= 85;
  138. return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  139. }
  140. WheelPos -= 170;
  141. return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  142. }