ledStrip_control.ino 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include <Adafruit_NeoPixel.h>
  2. Adafruit_NeoPixel leds = Adafruit_NeoPixel(LED_COUNT, LEDSTRIP_PIN, NEO_GRB + NEO_KHZ800);
  3. void initLedStrip()
  4. {
  5. leds.begin(); // Call this to start up the LED strip.
  6. clearLEDs(); // This function, defined below, turns all LEDs off...
  7. }
  8. void startShow(int i)
  9. {
  10. switch (i)
  11. {
  12. case WIPE_RED:
  13. colorWipe(leds.Color(255, 0, 0), 20); // Red
  14. break;
  15. case WIPE_GREEN:
  16. colorWipe(leds.Color(0, 255, 0), 20); // Green
  17. break;
  18. case WIPE_BLUE:
  19. colorWipe(leds.Color(0, 0, 255), 20); // Blue
  20. break;
  21. case CHASE_WHITE:
  22. theaterChase(leds.Color(127, 127, 127), 20); // White
  23. break;
  24. case CHASE_RED:
  25. theaterChase(leds.Color(127, 0, 0), 20); // Red
  26. break;
  27. case CHASE_GREEN:
  28. theaterChase(leds.Color(0, 255, 0), 20); // Green
  29. break;
  30. case CHASE_BLUE:
  31. theaterChase(leds.Color( 0, 0, 127), 20); // Blue
  32. break;
  33. case RAINBOW_LIGHT:
  34. rainbowLight();
  35. break;
  36. default:
  37. clearLEDs();
  38. break;
  39. }
  40. }
  41. //show rainbow light effect
  42. void rainbowLight()
  43. {
  44. for (int i = 0; i < LED_COUNT; i++)
  45. {
  46. rainbow(i);
  47. delay(20); // Delay between rainbow slides
  48. }
  49. clearLEDs();
  50. }
  51. // Sets all LEDs to off, but DOES NOT update the display;
  52. // call leds.show() to actually turn them off after this.
  53. void clearLEDs()
  54. {
  55. for (int i = 0; i < LED_COUNT; i++)
  56. {
  57. leds.setPixelColor(i, 0);
  58. }
  59. leds.show(); //but the LEDs don't actually update until you call this.
  60. }
  61. // Prints a rainbow on the ENTIRE LED strip.
  62. // The rainbow begins at a specified position.
  63. // ROY G BIV!
  64. void rainbow(byte startPosition)
  65. {
  66. // Need to scale our rainbow. We want a variety of colors, even if there
  67. // are just 10 or so pixels.
  68. int rainbowScale = 192 / LED_COUNT;
  69. // Next we setup each pixel with the right color
  70. for (int i = 0; i < LED_COUNT; i++)
  71. {
  72. // There are 192 total colors we can get out of the rainbowOrder function.
  73. // It'll return a color between red->orange->green->...->violet for 0-191.
  74. leds.setPixelColor(i, rainbowOrder((rainbowScale * (i + startPosition)) % 192));
  75. }
  76. // Finally, actually turn the LEDs on:
  77. leds.show();
  78. }
  79. // Input a value 0 to 191 to get a color value.
  80. // The colors are a transition red->yellow->green->aqua->blue->fuchsia->red...
  81. // Adapted from Wheel function in the Adafruit_NeoPixel library example sketch
  82. uint32_t rainbowOrder(byte position)
  83. {
  84. // 6 total zones of color change:
  85. if (position < 10) // Red -> Yellow (Red = FF, blue = 0, green goes 00-FF)
  86. {
  87. return leds.Color(0xFF, position * 8, 0);
  88. }
  89. else if (position < 20) // Yellow -> Green (Green = FF, blue = 0, red goes FF->00)
  90. {
  91. position -= 10;
  92. return leds.Color(0xFF - position * 8, 0xFF, 0);
  93. }
  94. else if (position < 30) // Green->Aqua (Green = FF, red = 0, blue goes 00->FF)
  95. {
  96. position -= 20;
  97. return leds.Color(0, 0xFF, position * 8);
  98. }
  99. else if (position < 40) // Aqua->Blue (Blue = FF, red = 0, green goes FF->00)
  100. {
  101. position -= 30;
  102. return leds.Color(0, 0xFF - position * 8, 0xFF);
  103. }
  104. else if (position < 50) // Blue->Fuchsia (Blue = FF, green = 0, red goes 00->FF)
  105. {
  106. position -= 40;
  107. return leds.Color(position * 8, 0, 0xFF);
  108. }
  109. else //160 <position< 191 Fuchsia->Red (Red = FF, green = 0, blue goes FF->00)
  110. {
  111. position -= 50;
  112. return leds.Color(0xFF, 0x00, 0xFF - position * 8);
  113. }
  114. }
  115. // Fill the dots one after the other with a color
  116. void colorWipe(uint32_t c, uint8_t wait)
  117. {
  118. for (uint16_t i = 0; i < leds.numPixels(); i++)
  119. {
  120. leds.setPixelColor(i, c);
  121. leds.show();
  122. delay(wait);
  123. }
  124. clearLEDs();
  125. }
  126. //Theatre-style crawling lights.
  127. void theaterChase(uint32_t c, uint8_t wait)
  128. {
  129. for (int j = 0; j < 10; j++) //do 10 cycles of chasing
  130. {
  131. for (int q = 0; q < 3; q++)
  132. {
  133. for (int i = 0; i < leds.numPixels(); i = i + 3)
  134. {
  135. leds.setPixelColor(i + q, c); //turn every third pixel on
  136. }
  137. leds.show();
  138. delay(wait);
  139. for (int i = 0; i < leds.numPixels(); i = i + 3)
  140. {
  141. leds.setPixelColor(i + q, 0); //turn every third pixel off
  142. }
  143. }
  144. }
  145. clearLEDs();
  146. }