|
| 1 | +#include <WS2812B.h> |
| 2 | + |
| 3 | +#define NUM_LEDS 30 |
| 4 | +/* |
| 5 | + * Note. Library uses SPI1 |
| 6 | + * Connect the WS2812B data input to MOSI on your board. |
| 7 | + * |
| 8 | + */ |
| 9 | +WS2812B strip = WS2812B(NUM_LEDS); |
| 10 | +// Note. Gamma is not really supported in the library, its only included as some functions used in this example require Gamma |
| 11 | +uint8_t LEDGamma[] = { |
| 12 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 13 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, |
| 14 | + 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, |
| 15 | + 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, |
| 16 | + 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, |
| 17 | + 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, |
| 18 | + 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25, |
| 19 | + 25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36, |
| 20 | + 37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50, |
| 21 | + 51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68, |
| 22 | + 69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89, |
| 23 | + 90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114, |
| 24 | + 115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142, |
| 25 | + 144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175, |
| 26 | + 177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213, |
| 27 | +215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 }; |
| 28 | + |
| 29 | +void setup() |
| 30 | +{ |
| 31 | + strip.begin();// Sets up the SPI |
| 32 | + strip.show();// Clears the strip, as by default the strip data is set to all LED's off. |
| 33 | + // strip.setBrightness(8); |
| 34 | +} |
| 35 | + |
| 36 | +void loop() |
| 37 | +{ |
| 38 | + colorWipe(strip.Color(0, 255, 0), 20); // Green |
| 39 | + colorWipe(strip.Color(255, 0, 0), 20); // Red |
| 40 | + colorWipe(strip.Color(0, 0, 255), 20); // Blue |
| 41 | + rainbow(10); |
| 42 | + rainbowCycle(10); |
| 43 | + theaterChase(strip.Color(255, 0, 0), 20); // Red |
| 44 | + theaterChase(strip.Color(0, 255, 0), 20); // Green |
| 45 | + theaterChase(strip.Color(0, 0, 255), 20); // Blue |
| 46 | + theaterChaseRainbow(10); |
| 47 | + whiteOverRainbow(20,75,5); |
| 48 | + pulseWhite(5); |
| 49 | + delay(250); |
| 50 | + fullWhite(); |
| 51 | + delay(250); |
| 52 | + rainbowFade2White(3,3,1); |
| 53 | +} |
| 54 | + |
| 55 | +// Fill the dots one after the other with a color |
| 56 | +void colorWipe(uint32_t c, uint8_t wait) |
| 57 | +{ |
| 58 | + for(uint16_t i=0; i<strip.numPixels(); i++) |
| 59 | + { |
| 60 | + strip.setPixelColor(i, c); |
| 61 | + strip.show(); |
| 62 | + delay(wait); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +void rainbow(uint8_t wait) { |
| 67 | + uint16_t i, j; |
| 68 | + |
| 69 | + for(j=0; j<256; j++) { |
| 70 | + for(i=0; i<strip.numPixels(); i++) |
| 71 | + { |
| 72 | + strip.setPixelColor(i, Wheel((i+j) & 255)); |
| 73 | + } |
| 74 | + strip.show(); |
| 75 | + delay(wait); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +// Slightly different, this makes the rainbow equally distributed throughout |
| 82 | +void rainbowCycle(uint8_t wait) |
| 83 | +{ |
| 84 | + uint16_t i, j; |
| 85 | + |
| 86 | + for(j=0; j<256*5; j++) |
| 87 | + { // 5 cycles of all colors on wheel |
| 88 | + for(i=0; i< strip.numPixels(); i++) |
| 89 | + { |
| 90 | + strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255)); |
| 91 | + } |
| 92 | + strip.show(); |
| 93 | + delay(wait); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// Input a value 0 to 255 to get a color value. |
| 98 | +// The colours are a transition r - g - b - back to r. |
| 99 | +uint32_t Wheel(byte WheelPos) |
| 100 | +{ |
| 101 | + if(WheelPos < 85) |
| 102 | + { |
| 103 | + return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + if(WheelPos < 170) |
| 108 | + { |
| 109 | + WheelPos -= 85; |
| 110 | + return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); |
| 111 | + } |
| 112 | + else |
| 113 | + { |
| 114 | + WheelPos -= 170; |
| 115 | + return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +//Theatre-style crawling lights. |
| 121 | +void theaterChase(uint32_t c, uint8_t wait) { |
| 122 | + for (int j=0; j<10; j++) { //do 10 cycles of chasing |
| 123 | + for (int q=0; q < 3; q++) { |
| 124 | + for (uint16_t i=0; i < strip.numPixels(); i=i+3) { |
| 125 | + strip.setPixelColor(i+q, c); //turn every third pixel on |
| 126 | + } |
| 127 | + strip.show(); |
| 128 | + |
| 129 | + delay(wait); |
| 130 | + |
| 131 | + for (uint16_t i=0; i < strip.numPixels(); i=i+3) { |
| 132 | + strip.setPixelColor(i+q, 0); //turn every third pixel off |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +//Theatre-style crawling lights with rainbow effect |
| 139 | +void theaterChaseRainbow(uint8_t wait) { |
| 140 | + for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel |
| 141 | + for (int q=0; q < 3; q++) { |
| 142 | + for (uint16_t i=0; i < strip.numPixels(); i=i+3) { |
| 143 | + strip.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on |
| 144 | + } |
| 145 | + strip.show(); |
| 146 | + |
| 147 | + delay(wait); |
| 148 | + |
| 149 | + for (uint16_t i=0; i < strip.numPixels(); i=i+3) { |
| 150 | + strip.setPixelColor(i+q, 0); //turn every third pixel off |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +void pulseWhite(uint8_t wait) { |
| 157 | + for(int j = 0; j < 256 ; j++){ |
| 158 | + for(uint16_t i=0; i<strip.numPixels(); i++) { |
| 159 | + strip.setPixelColor(i, strip.Color(0,0,0, LEDGamma[j] ) ); |
| 160 | + } |
| 161 | + delay(wait); |
| 162 | + strip.show(); |
| 163 | + } |
| 164 | + |
| 165 | + for(int j = 255; j >= 0 ; j--){ |
| 166 | + for(uint16_t i=0; i<strip.numPixels(); i++) { |
| 167 | + strip.setPixelColor(i, strip.Color(0,0,0, LEDGamma[j] ) ); |
| 168 | + } |
| 169 | + delay(wait); |
| 170 | + strip.show(); |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | + |
| 175 | +void rainbowFade2White(uint8_t wait, int rainbowLoops, int whiteLoops) { |
| 176 | + float fadeMax = 100.0; |
| 177 | + int fadeVal = 0; |
| 178 | + uint32_t wheelVal; |
| 179 | + int redVal, greenVal, blueVal; |
| 180 | + |
| 181 | + for(int k = 0 ; k < rainbowLoops ; k ++){ |
| 182 | + |
| 183 | + for(int j=0; j<256; j++) { // 5 cycles of all colors on wheel |
| 184 | + |
| 185 | + for(int i=0; i< strip.numPixels(); i++) { |
| 186 | + |
| 187 | + wheelVal = Wheel(((i * 256 / strip.numPixels()) + j) & 255); |
| 188 | + |
| 189 | + redVal = red(wheelVal) * float(fadeVal/fadeMax); |
| 190 | + greenVal = green(wheelVal) * float(fadeVal/fadeMax); |
| 191 | + blueVal = blue(wheelVal) * float(fadeVal/fadeMax); |
| 192 | + |
| 193 | + strip.setPixelColor( i, strip.Color( redVal, greenVal, blueVal ) ); |
| 194 | + |
| 195 | + } |
| 196 | + |
| 197 | + //First loop, fade in! |
| 198 | + if(k == 0 && fadeVal < fadeMax-1) { |
| 199 | + fadeVal++; |
| 200 | + } |
| 201 | + |
| 202 | + //Last loop, fade out! |
| 203 | + else if(k == rainbowLoops - 1 && j > 255 - fadeMax ){ |
| 204 | + fadeVal--; |
| 205 | + } |
| 206 | + |
| 207 | + strip.show(); |
| 208 | + delay(wait); |
| 209 | + } |
| 210 | + |
| 211 | + } |
| 212 | + |
| 213 | + |
| 214 | + |
| 215 | + delay(500); |
| 216 | + |
| 217 | + |
| 218 | + for(int k = 0 ; k < whiteLoops ; k ++){ |
| 219 | + |
| 220 | + for(int j = 0; j < 256 ; j++){ |
| 221 | + |
| 222 | + for(uint16_t i=0; i < strip.numPixels(); i++) { |
| 223 | + strip.setPixelColor(i, strip.Color(0,0,0, LEDGamma[j] ) ); |
| 224 | + } |
| 225 | + strip.show(); |
| 226 | + } |
| 227 | + |
| 228 | + delay(2000); |
| 229 | + for(int j = 255; j >= 0 ; j--){ |
| 230 | + |
| 231 | + for(uint16_t i=0; i < strip.numPixels(); i++) { |
| 232 | + strip.setPixelColor(i, strip.Color(0,0,0, LEDGamma[j] ) ); |
| 233 | + } |
| 234 | + strip.show(); |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + delay(500); |
| 239 | + |
| 240 | + |
| 241 | +} |
| 242 | + |
| 243 | +void whiteOverRainbow(uint8_t wait, uint8_t whiteSpeed, uint8_t whiteLength ) { |
| 244 | + |
| 245 | + if(whiteLength >= strip.numPixels()) whiteLength = strip.numPixels() - 1; |
| 246 | + |
| 247 | + int head = whiteLength - 1; |
| 248 | + int tail = 0; |
| 249 | + |
| 250 | + int loops = 3; |
| 251 | + int loopNum = 0; |
| 252 | + |
| 253 | + static unsigned long lastTime = 0; |
| 254 | + |
| 255 | + |
| 256 | + while(true){ |
| 257 | + for(int j=0; j<256; j++) { |
| 258 | + for(uint16_t i=0; i<strip.numPixels(); i++) { |
| 259 | + if((i >= tail && i <= head) || (tail > head && i >= tail) || (tail > head && i <= head) ){ |
| 260 | + strip.setPixelColor(i, strip.Color(0,0,0, 255 ) ); |
| 261 | + } |
| 262 | + else{ |
| 263 | + strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255)); |
| 264 | + } |
| 265 | + |
| 266 | + } |
| 267 | + |
| 268 | + if(millis() - lastTime > whiteSpeed) { |
| 269 | + head++; |
| 270 | + tail++; |
| 271 | + if(head == strip.numPixels()){ |
| 272 | + loopNum++; |
| 273 | + } |
| 274 | + lastTime = millis(); |
| 275 | + } |
| 276 | + |
| 277 | + if(loopNum == loops) return; |
| 278 | + |
| 279 | + head%=strip.numPixels(); |
| 280 | + tail%=strip.numPixels(); |
| 281 | + strip.show(); |
| 282 | + delay(wait); |
| 283 | + } |
| 284 | + } |
| 285 | + |
| 286 | +} |
| 287 | +void fullWhite() { |
| 288 | + |
| 289 | + for(uint16_t i=0; i<strip.numPixels(); i++) { |
| 290 | + strip.setPixelColor(i, strip.Color(0,0,0, 255 ) ); |
| 291 | + } |
| 292 | + strip.show(); |
| 293 | +} |
| 294 | + |
| 295 | +uint8_t red(uint32_t c) { |
| 296 | + return (c >> 16); |
| 297 | +} |
| 298 | +uint8_t green(uint32_t c) { |
| 299 | + return (c >> 8); |
| 300 | +} |
| 301 | +uint8_t blue(uint32_t c) { |
| 302 | + return (c); |
| 303 | +} |
0 commit comments