Components and supplies
1
Arduino Nano R3
1
5V WS2811 50 LED strip
Tools and machines
1
Multitool
1
Soldering iron (generic)
1
Scissor, Electrician
1
Solder Wire, Lead Free
Project description
Code
Arduino code
arduino
Include FastLED library
1#include <Arduino.h> 2#include <FastLED.h> 3 4// Put here the number of LEDs you have. 5#define NUM_LEDS 50 6 7// define here your type of strip. 8#define LED_TYPE WS2811 9 10// define here the order of the strip. 11#define COLOR_ORDER RGB 12 13// define the led strip output number. 14#define LED_PIN 4 15 16// pin where the button to change mode is connected 17#define I_CHANGE_MODE 5 18 19// Global variables 20/// Led array 21CRGB _leds[NUM_LEDS]; 22 23/// Current Animation 24int _mode=0; 25 26/// Total number of animations, it is used to return to the first animation. 27/// Once reached the end. 28int const MAX_MODES = 14; 29 30/// Indicates that the mode has just changed. 31bool _modeChanged = true; 32 33/// Actual speed 100 is normal. 34int _speed = 100; 35 36/// Current color 0-255 if 255 and make _hue++ it automaticly goes to 0. 37uint8_t _hue = 0; 38 39/// number of the current led in moving light animations 40int _movingLed = 0; 41 42/// direction of the animation in upd/down animations 43bool _sense; 44 45/// palette 46CRGBPalette16 _pal; 47 48// Setup 49void setup() { 50 // Serial for debugging 51 Serial.begin(9600); 52 Serial.print("start"); 53 54 // out pin for led strip 55 pinMode(LED_PIN, OUTPUT); 56 57 // Init FastLED library 58 FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(_leds, NUM_LEDS); 59 60 // Define Change mode button input with pull-up 61 pinMode(I_CHANGE_MODE, INPUT_PULLUP); 62 63 ChangeAnimation (1); 64} 65 66// Loop 67void loop() { 68 // Change mode button state and last state to detect fall-Down 69 int buttonState; 70 static int LastButtonState; 71 72 // read button, if button is just pressed change to next animation 73 buttonState = digitalRead(I_CHANGE_MODE); 74 75 if ( buttonState == LOW && LastButtonState==HIGH) 76 ChangeNextAnimation(); 77 78 LastButtonState = buttonState; 79 80 // Refresh animation 81 AnimationUpdate(); 82} 83 84/// Change animation number mode. 85void ChangeAnimation(int mode) 86{ 87 _mode = mode; 88 89 // flag that mode has changed for initializations 90 _modeChanged = true; 91} 92 93/// Change to next animation 94void ChangeNextAnimation() 95{ 96 _mode++; 97 98 // if we reached the total of animation return to firt one 99 if (_mode > MAX_MODES) 100 _mode = 1; 101 102 // flag that mode has changed for initializations 103 _modeChanged = true; 104} 105 106/// Change speed of animations 107void ChangeSpeed(int speed) 108{ 109 _speed = speed; 110} 111 112/// Change brightness 113void ChangeBrightness(int brightnes) 114{ 115 //Not implemented 116} 117 118/// Add randomly glitter 119/// From FastLight Examples 120/// @param chanceOfGlitter of 256 121void AddGlitter( fract8 chanceOfGlitter) 122{ 123 if( random8() < chanceOfGlitter) 124 _leds[ random16(NUM_LEDS) ] += CRGB::White; 125} 126 127// we move a sinus wave and iterate through different colours 128// we try to put one period in the whole led strip 129/// @param init indicates that we can make some sort of initialization 130void AnimWave(bool init = false) 131{ // waves 132 static int t = 0; // current "time" 133 134 int T = NUM_LEDS; 135 136 // copy each Led with previous one 137 for (int i = 0; i < NUM_LEDS - 1; i++) 138 _leds[i] = _leds[i + 1]; 139 140 // put color and value of last led 141 _leds[NUM_LEDS - 1] = CHSV(_hue, 255, sin8 ( t * 255 / T ) / 2 + 120); 142 143 // increase time 144 t++; 145 146 // if the end of the wave Period start a new wave again and change color 147 if (t == T) 148 { 149 t = 0; 150 _hue++; 151 } 152 153 // delay to next 154 delay ( 50 * _speed / 100 ); 155} 156 157// we move a sinus wave and iterate through different colours 158/// we try to put one period in the whole led strip 159/// @param init indicates that we can make some sort of initialization 160void AnimHeartBeat(bool init = false) 161{ 162 static int t = 0; // current "time" 163 164 int T = NUM_LEDS; 165 166 // put color and value of first led 167 _leds[0] = CHSV(_hue, 255, sin8 ( t * 255 / T ) / 2 + 32); 168 169 // Copy all the leds 170 for (int i = 1; i < NUM_LEDS; i++) 171 _leds[i] = _leds[0]; 172 173 // increase time 174 t++; 175 176 // if the end of the wave Period start a new wave again and change color 177 if (t == T) 178 { 179 t = 0; 180 _hue++; 181 } 182 183 // delay to next 184 delay ( 50 * _speed / 100 ); 185} 186 187/// Move a random color to top 188/// 189/// @param init indicates that we can make some sort of initialization 190void AnimArianna(bool init = false) 191{ //Arianna 192 193 static int lastLED = NUM_LEDS; // last led 194 195 if (init){ 196 _movingLed = 0; 197 lastLED = NUM_LEDS; 198 } 199 200 // Put a color if is the first led of an iteration or fade it 201 if (_movingLed == 0) 202 { 203 _leds[0] = CHSV(random(256), 255, 255); 204 lastLED--; 205 } 206 else 207 _leds[0].fadeToBlackBy(192); 208 209 // move every led value to next one 210 for (int i = lastLED; i > 0; i--) 211 _leds[i] = _leds[i - 1]; 212 213 // moving led is the next one 214 _movingLed++; 215 216 // if the last led is the first one 217 // begin again 218 if (lastLED == 1) 219 lastLED=NUM_LEDS; 220 221 delay ( 50 * _speed / 100 ); 222} 223 224/// KnightRider animation 225/// 226/// @param init indicates that we can make some sort of initialization 227void AnimKnight( bool init = false) 228{ 229 fadeToBlackBy(_leds, NUM_LEDS,64); // attenuate every led a little 230 231 if (init) 232 _movingLed = 0; 233 234 if (_sense) // one direction 235 _leds[_movingLed] = CRGB::Red; 236 else // other direction 237 _leds[NUM_LEDS -1 -_movingLed] = CRGB::Red; 238 239 _movingLed ++; 240 241 if (_movingLed == NUM_LEDS ) 242 _sense = !_sense; 243 244 delay( 50 * _speed / 100 ); 245} 246 247/// Alternating between odds an evens 248/// 249/// @param init indicates that we can make some sort of initialization 250void AnimAlternating(bool init = false) 251{ 252 253 // counter we change every 50 isf _speed= 100 -> 1 second 254 static int counter = 0; 255 static bool pair = false ; // current moving led 256 257 if (init){ 258 // everyled off 259 for (int i = 0; i < NUM_LEDS; i++) 260 _leds[i] = CRGB::Black; 261 counter = 0; 262 } 263 264 // put every led to the same random color 265 if (counter == 0) 266 { 267 268 // move every led value to next one 269 for (int i = NUM_LEDS - 1; i > 0; i--) 270 _leds[i] = _leds[i - 1]; 271 272 // Put a color if is the first led of an iteration or fade it 273 if (pair) 274 _leds[0] = CHSV(random(256), 255, 255); 275 else 276 _leds[0] = CRGB::Black; 277 278 //alternate pair value & start counter 279 pair = !pair; 280 counter = 10; 281 } 282 else 283 counter--; 284 285 delay ( 50 * _speed / 100 ); 286} 287 288/// Animation as painter brushes 289/// elects randomly a start led, color and direction 290/// 291/// @param init indicates that we can make some sort of initialization 292void AnimDali(bool init = false) 293{ 294 // First fade all 295 fadeToBlackBy( _leds, NUM_LEDS, 32); 296 297 static int length = 0; 298 299 // if we are not painting start a new 300 if (length == 0 ) 301 { 302 _movingLed = random(NUM_LEDS); 303 _sense = !_sense; 304 _hue = random(256); 305 length = random(NUM_LEDS); 306 } 307 else 308 { 309 // If we have not ended painting paint it 310 _leds[_movingLed] = CHSV(_hue, 255, 255); 311 312 // Mark next pixel to be painted, decrease the remaining length 313 // to be painted 314 if (_sense) 315 _movingLed++; 316 else 317 _movingLed--; 318 length --; 319 } 320 321 delay ( 50 * _speed / 100 ); 322} 323 324/// Animation glitters 325/// elects randomly a start led, color and direction 326/// @param init indicates that we can make some sort of initialization 327void AnimGlitter(bool init = false) 328{ 329 // First fade all 330 fadeToBlackBy( _leds, NUM_LEDS, 32); 331 332 // Glitter 333 _leds[ random16(NUM_LEDS) ] += CRGB::White; 334 335 delay ( 50 * _speed / 100 ); 336} 337 338/// RainDrops 339/// elects randomly a start led, and goes down due gravity 340/// 341/// @param init indicates that we can make some sort of initialization 342void AnimRainDrops(bool init = false) 343{ 344 // Generate new drops 345 AddGlitter(20); 346 347 // Copy every white led down 348 for (int i = 0; i < NUM_LEDS; i++) 349 { 350 // copy to previous if white (r=g=b=255) and is not the first 351 if ( (_leds[i].r == 255 ) && ( i != 0 ) ) 352 _leds[ i - 1 ] = _leds[i]; 353 354 // fade to black 355 _leds[i].fadeToBlackBy(64); 356 } 357 358 delay ( 25 * _speed / 100 ); 359} 360 361/// Lightings 362/// like glitter but with length 363/// 364/// @param init indicates that we can make some sort of initialization 365void AnimLightings(bool init = false) 366{ 367 // First fade all 368 fadeToBlackBy( _leds, NUM_LEDS, 128); 369 370 // Choice of lighting 371 if( random8() < 40) { 372 int nled = random16(NUM_LEDS); 373 int length = random16(NUM_LEDS/4); 374 375 // copy up and down, check limits 376 for (int i = 0; i < length; i++){ 377 if ( (nled+ i ) < NUM_LEDS ) 378 _leds[ nled + i ] += CRGB::White; 379 380 if ( (nled-i ) >= NUM_LEDS ) 381 _leds[ nled - i ] += CRGB::White; 382 } 383 384 } 385 delay ( 50 * _speed / 100 ); 386} 387 388// From FasLED examples 389void AnimFLStaticRandom(bool init = false) 390{ 391 // counter we change every 50 isf _speed= 100 -> 1 second 392 static int counter = 0; 393 394 // At start counter to cero to start animation 395 if (init) 396 counter = 0; 397 398 // put every led to the same random color 399 if (counter == 0) 400 { 401 _leds[0] = CHSV(random(256), 255, 255); 402 403 for (int i = 1; i < NUM_LEDS-1; i++) 404 _leds[i] = _leds[0]; 405 406 counter = 40; 407 } 408 else 409 counter--; 410 411 delay ( 50 * _speed / 100 ); 412} 413 414 415// first light, a running white led 416void AnimFLFirstLight(bool init = false) 417{ 418 // Delete last one 419 _leds[_movingLed] = CRGB::Black; 420 421 // moving led is the next one 422 _movingLed++; 423 424 // we do it here since we can have array overflow 425 if (_movingLed >= NUM_LEDS ) 426 _movingLed = 0; 427 428 // light this one 429 _leds[_movingLed] = CRGB::White; 430 431 delay ( 50 * _speed / 100 ); 432} 433 434// no idea what is this 435void AnimFLCylon(bool init = false) 436{ 437 fadeToBlackBy(_leds, NUM_LEDS,16); // atenuate every led a little 438 439 if (init) 440 _movingLed = 0; 441 442 if (_sense) // one direction 443 _leds[_movingLed] = CHSV(_hue++, 255, 255); 444 else // other direction 445 _leds[NUM_LEDS -1 -_movingLed] = CHSV(_hue++, 255, 255); 446 447 _movingLed ++; 448 449 if (_movingLed == NUM_LEDS ) 450 _sense = !_sense; 451 452 delay ( 50 * _speed / 100 ); 453 } 454 455// Fire from fastLED examples 456void AnimFLFire2012(bool init = false) 457{ 458 if (init) 459 _pal = HeatColors_p; 460 461 // Add entropy to random number generator; we use a lot of it. 462 random16_add_entropy( random()); 463 464 // Array of temperature readings at each simulation cell 465 static byte heat[NUM_LEDS]; 466 static byte cooling = 50; 467 static byte sparkling = 120; 468 469 // Step 1. Cool down every cell a little 470 for( int i = 0; i < NUM_LEDS; i++) { 471 heat[i] = qsub8( heat[i], random8(0, ((cooling * 10) / NUM_LEDS) + 2)); 472 } 473 474 // Step 2. Heat from each cell drifts 'up' and diffuses a little 475 for( int k= NUM_LEDS - 1; k >= 2; k--) { 476 heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3; 477 } 478 479 // Step 3. Randomly ignite new 'sparks' of heat near the bottom 480 if( random8() < sparkling ) { 481 int y = random8(7); 482 heat[y] = qadd8( heat[y], random8(160,255) ); 483 } 484 485 // Step 4. Map from heat cells to LED colors 486 for( int j = 0; j < NUM_LEDS; j++) { 487 // Scale the heat value from 0-255 down to 0-240 488 // for best results with color palettes. 489 _leds[j] = ColorFromPalette( _pal, scale8(heat[j], 240)); 490 491 } 492 delay ( 50 * _speed / 100 ); 493} 494 495// Juggle form fastLED examples 496void AnimFLJuggle(bool init = false) { 497 // eight colored dots, weaving in and out of sync with each other 498 fadeToBlackBy( _leds, NUM_LEDS, 32); 499 byte dothue = 0; 500 for( int i = 0; i < 8; i++) { 501 _leds[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255); 502 dothue += 32; 503 } 504 delay ( 50 * _speed / 100 ); 505} 506 507/// Updates the animation, if there is a mode change it changes the mode informating tha mode has just changed 508void AnimationUpdate() 509{ 510 switch (_mode) 511 { 512 case 1: 513 AnimWave(_modeChanged); 514 break; 515 case 2: 516 AnimHeartBeat(_modeChanged); 517 break; 518 case 3: 519 AnimArianna(_modeChanged); 520 break; 521 case 4: 522 AnimKnight(_modeChanged); 523 break; 524 case 5: 525 AnimAlternating(_modeChanged); 526 break; 527 case 6: 528 AnimDali(_modeChanged); 529 break; 530 case 7: 531 AnimGlitter(_modeChanged); 532 break; 533 case 8: 534 AnimRainDrops(_modeChanged); 535 break; 536 case 9: 537 AnimLightings(_modeChanged); 538 break; 539 case 10: 540 AnimFLStaticRandom(_modeChanged); 541 break; 542 case 11: 543 AnimFLFirstLight(_modeChanged); 544 break; 545 case 12: 546 AnimFLFire2012(_modeChanged); 547 break; 548 case 13: 549 AnimFLCylon(_modeChanged); 550 break; 551 case 14: 552 AnimFLJuggle(_modeChanged); 553 break; 554 } 555 556 // Control of _movingled if reaches any of both ends 557 // if reaches the end or begin, begin again 558 if (_movingLed >= NUM_LEDS ) 559 _movingLed = 0; 560 561 if (_movingLed <= -1 ) 562 _movingLed = NUM_LEDS -1; 563 564 // return to mode not changed 565 _modeChanged = false; 566 567 // Show leds 568 FastLED.show(); 569} 570 571 572
Downloadable files
Wiring
Wiring

Wiring
Wiring

Comments
Only logged in users can leave comments