atuline

matrix

Apr 27th, 2021
1,251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.68 KB | None | 0 0
  1.  
  2. /* Matrix
  3.  
  4. By: funkboxing LED
  5.  
  6. Modified by: Andrew Tuline
  7.  
  8. Date: Oct, 2014
  9.  
  10. Green (or other coloured) characters going up/down the strand, a la 'The Matrix'.
  11.  
  12. */
  13.  
  14.  
  15. #include "FastLED.h"                                          // FastLED library.
  16.  
  17. #if FASTLED_VERSION < 3001000
  18. #error "Requires FastLED 3.1 or later; check github for latest code."
  19. #endif
  20.  
  21. // Fixed definitions cannot change on the fly.
  22. #define LED_DT 12                                             // Serial data pin
  23. #define LED_CK 11                                             // Clock pin for WS2801 or APA102
  24. #define COLOR_ORDER GRB                                       // It's GRB for WS2812B
  25. #define LED_TYPE WS2812                                       // What kind of strip are you using (APA102, WS2801 or WS2812B)?
  26. #define NUM_LEDS 40                                           // Number of LED's
  27.  
  28. // Initialize changeable global variables.
  29. uint8_t max_bright = 128;                                     // Overall brightness definition. It can be changed on the fly.
  30.  
  31. struct CRGB leds[NUM_LEDS];                                   // Initialize our LED array.
  32.  
  33. // Initialize global variables for sequences
  34. int thisdelay =  50;                                          // A delay value for the sequence(s)
  35. int   thishue =  95;
  36. int   thissat = 255;
  37. int   thisdir =   0;
  38. bool   huerot =   0;                                          // Does the hue rotate? 1 = yes
  39. uint8_t bgclr =   0;
  40. uint8_t bgbri =   0;
  41.  
  42.  
  43. void setup() {
  44.   Serial.begin(115200);
  45.  
  46.   LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);      // For WS2812B
  47.  
  48.   FastLED.setBrightness(max_bright);
  49.   FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);               // FastLED power management set at 5V, 500mA
  50. } // setup()
  51.  
  52.  
  53.  
  54. void loop () {
  55.   ChangeMe();
  56.  
  57.   EVERY_N_MILLISECONDS(thisdelay) {                           // FastLED based non-blocking delay to update/display the sequence.
  58.     matrix();                                                   // Routine is still delay based, but at least it's now a non-blocking day.
  59.   }
  60.   FastLED.show();
  61. } // loop()
  62.  
  63.  
  64. void matrix() {                                               // One line matrix
  65.  
  66.   if (huerot) thishue=thishue+5;
  67.  
  68.   if (random16(90) > 80) {
  69.     if (thisdir == 0) leds[0] = CHSV(thishue, thissat, 255); else leds[NUM_LEDS-1] = CHSV(thishue, thissat, 255);
  70.   }
  71.   else {
  72.     if (thisdir ==0) leds[0] = CHSV(bgclr, thissat, bgbri); else leds[NUM_LEDS-1] = CHSV(bgclr, thissat, bgbri);
  73.   }
  74.  
  75.   if (thisdir == 0) {
  76.     for (int i = NUM_LEDS-1; i >0 ; i-- ) leds[i] = leds[i-1];
  77.   } else {
  78.     for (int i = 0; i < NUM_LEDS-1 ; i++ ) leds[i] = leds[i+1];
  79.   }
  80. } // matrix()
  81.  
  82.  
  83. void ChangeMe() {                                             // A time (rather than loop) based demo sequencer. This gives us full control over the length of each sequence.
  84.   uint8_t secondHand = (millis() / 1000) % 25;                // Change '25' to a different value to change length of the loop.
  85.   static uint8_t lastSecond = 99;                             // Static variable, means it's only defined once. This is our 'debounce' variable.
  86.   if (lastSecond != secondHand) {                             // Debounce to make sure we're not repeating an assignment.
  87.     lastSecond = secondHand;
  88.     switch(secondHand) {
  89.       case  0: thisdelay=50; thishue=95; bgclr=140; bgbri=20; huerot=0; break;
  90.       case  5: thisdir=1; bgbri=0; huerot=1; break;
  91.       case 10: thisdelay=30; thishue=0; bgclr=50; bgbri=20; huerot=0; break;
  92.       case 15: thisdelay=80; bgbri = 10; thishue=random8(); break;
  93.       case 20: thishue=random8(); huerot=1; break;
  94.       case 25: break;
  95.     }
  96.   }
  97. } // ChangeMe()
Advertisement
Add Comment
Please, Sign In to add comment