Advertisement
atuline

aanimations.ino

Jul 28th, 2020
3,269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.96 KB | None | 0 0
  1. /* aanimations
  2.  *
  3.  * By: Can't recall where I found this. Maybe Stefan Petrick.
  4.  *
  5.  * Modified by: Andrew Tuline
  6.  *
  7.  * Date: January, 2017
  8.  *
  9.  * This sketch demonstrates how to blend between two animations running at the same time.
  10.  *
  11.  */
  12.  
  13.  
  14. #include "FastLED.h"                                          // FastLED library.
  15.  
  16. #if FASTLED_VERSION < 3001000
  17. #error "Requires FastLED 3.1 or later; check github for latest code."
  18. #endif
  19.  
  20. #define LED_DT 12                                             // Data pin to connect to the strip.
  21. #define LED_CK 11                                             // Clock pin for WS2801 or APA102.
  22. #define COLOR_ORDER BGR                                       // It's GRB for WS2812 and BGR for APA102.
  23. #define LED_TYPE APA102                                       // Using APA102, WS2812, WS2801. Don't forget to change LEDS.addLeds.
  24. #define NUM_LEDS 60                                           // Number of LED's.
  25.  
  26. // Global variables can be changed on the fly.
  27. uint8_t max_bright = 128;                                     // Overall brightness definition. It can be changed on the fly.
  28.  
  29. // have 3 independent CRGBs - 2 for the sources and one for the output
  30. CRGB leds[NUM_LEDS];
  31. CRGB leds2[NUM_LEDS];
  32. CRGB leds3[NUM_LEDS];
  33.  
  34.  
  35.  
  36. void setup() {
  37.  
  38.   delay(1000);                                                // Power-up safety delay.
  39.   Serial.begin(115200);                                        // Initialize serial port for debugging.
  40.  
  41.   LEDS.addLeds<LED_TYPE, LED_DT, LED_CK, COLOR_ORDER>(leds, NUM_LEDS);  // Use this for WS2801 or APA102
  42. //  LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);  // Use this for WS2812
  43.  
  44.   FastLED.setBrightness(max_bright);
  45.   FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);               // FastLED Power management set at 5V, 500mA.
  46.  
  47. } // setup()
  48.  
  49.  
  50.  
  51. void loop() {
  52.  
  53.   animationA(10, 12);                                         // render the first animation into leds2  
  54.   animationB(5, 12);                                          // render the second animation into leds3
  55.  
  56.   uint8_t ratio = beatsin8(2);                                // Alternate between 0 and 255 every minute
  57.  
  58.   for (int i = 0; i < NUM_LEDS; i++) {                        // mix the 2 arrays together
  59.     leds[i] = blend( leds2[i], leds3[i], ratio );
  60.   }
  61.  
  62.   FastLED.show();
  63.  
  64. } // loop()
  65.  
  66.  
  67.  
  68. void animationA(uint8_t speed, uint8_t length) {              // running red stripe.
  69.  
  70.   for (int i = 0; i < NUM_LEDS; i++) {
  71.     uint8_t red = (millis() / speed) + (i * length);          // speed, length
  72.     if (red > 128) red = 0;
  73.     leds2[i] = CRGB(red, 0, 0);
  74.   }
  75. } // animationA()
  76.  
  77.  
  78.  
  79. void animationB(uint8_t speed, uint8_t length) {              // running green stripe in opposite direction.
  80.   for (int i = 0; i < NUM_LEDS; i++) {
  81.     uint8_t green = (millis() / speed) - (i * length);        // speed, length
  82.     if (green > 128) green = 0;
  83.     leds3[i] = CRGB(0, green, 0);
  84.   }
  85. } // animationB()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement