marmil

DemoReel100_marquee_test.ino

Jun 20th, 2018
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.28 KB | None | 0 0
  1. //***************************************************************
  2. // Example for Tim Lukasik based on this post:
  3. // https://plus.google.com/u/0/100542328320334100166/posts/51yQaDrLbBM
  4. //
  5. // This rolls my marquee example into a function that can
  6. // be used in Mark Kriegsman's DemoReel100 example.
  7. // Things needed to be reordered a bit so the show() could be
  8. // removed from inside the function and only have it called
  9. // in the main loop.
  10. // And all the variables were made static so they could be left
  11. // down below inside the function.
  12. //
  13. // Marc Miller, June 2018
  14. //***************************************************************
  15.  
  16. #include <FastLED.h>
  17. //FASTLED_USING_NAMESPACE
  18. #define DATA_PIN    11
  19. #define CLOCK_PIN   13
  20. #define LED_TYPE    LPD8806
  21. #define COLOR_ORDER GRB
  22. #define NUM_LEDS    32
  23. CRGB leds[NUM_LEDS];
  24. #define BRIGHTNESS         64
  25. #define FRAMES_PER_SECOND  120
  26.  
  27. //---------------------------------------------------------------
  28. void setup() {
  29.   Serial.begin(115200);  // Allows serial monitor output (check baud rate)
  30.   delay(500);  // Startup delay
  31.   //FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  32.   FastLED.addLeds<LED_TYPE, DATA_PIN, CLOCK_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  33.   FastLED.setBrightness(BRIGHTNESS);
  34.   FastLED.clear();
  35. }
  36.  
  37.  
  38. //---------------------------------------------------------------
  39. // List of patterns to cycle through.  Each is defined as a separate function below.
  40. typedef void (*SimplePatternList[])();
  41. SimplePatternList gPatterns = { marquee, rainbow };
  42.  
  43. uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
  44. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  45.  
  46.  
  47. //---------------------------------------------------------------
  48. void loop()
  49. {
  50.   // Call the current pattern function once, updating the 'leds' array
  51.   gPatterns[gCurrentPatternNumber]();
  52.  
  53.   // send the 'leds' array out to the actual LED strip
  54.   FastLED.show();  
  55.   // insert a delay to keep the framerate modest
  56.   //FastLED.delay(1000/FRAMES_PER_SECOND);
  57.  
  58.   // do some periodic updates
  59.   EVERY_N_MILLISECONDS( 15 ) { gHue++; } // cycle the "base color" through the rainbow
  60.   EVERY_N_SECONDS( 60 ) { nextPattern(); } // change patterns periodically
  61.  
  62. }//end_main_loop
  63.  
  64.  
  65. //---------------------------------------------------------------
  66. #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
  67.  
  68. void nextPattern()
  69. {
  70.   // add one to the current pattern number, and wrap around at the end
  71.   gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
  72. }
  73.  
  74. //---------------------------------------------------------------
  75. void rainbow()
  76. {
  77.   // FastLED's built-in rainbow generator
  78.   fill_rainbow( leds, NUM_LEDS, gHue, 20);
  79. }
  80.  
  81.  
  82. //---------------------------------------------------------------
  83. void marquee() {
  84.  
  85.   const uint16_t holdTime = 700;  // Milliseconds to hold position before advancing.
  86.   static uint8_t spacing = 5;     // Sets pixel spacing. [Use 2 or greater]
  87.   static int8_t  delta = 1;       // Sets forward or backwards direction amount. (Can be negative.)
  88.   static uint8_t width = 3;       // Can increase the number of pixels (width) of the chase. [1 or greater]
  89.  
  90.   static boolean fadingTail = 0;  // Add fading tail? [1=true, 0=falue]
  91.   static uint8_t fadeRate = 220;  // How fast to fade out tail. [0-255]
  92.  
  93.   static uint8_t hue = 180;       // Starting color.
  94.   static uint8_t hue2_shift = 20; // Hue shift for secondary color.  Use 0 for no shift. [0-255]
  95.  
  96.   static int16_t pos;                 // Pixel position.
  97.   static int8_t  advance = -1*width;  // Stores the advance amount.
  98.   static uint8_t color;               // Stores a hue color.
  99.  
  100.  
  101.   EVERY_N_SECONDS(10){  // Demo: Change direction every N seconds.
  102.     delta = -1*delta;
  103.   }
  104.  
  105.   //EVERY_N_SECONDS(10){  // Demo: Changing the pixel spacing every N seconds.
  106.   //  spacing = spacing + 1;
  107.   //  if (spacing == 9) { spacing = 2; }  // Reset spacing to 2
  108.   //  if (spacing >5) { spacing = 8; }  // Jump spacing up to 8
  109.   //}
  110.  
  111.   EVERY_N_SECONDS(10){  // Demo: Change the hue every N seconds.
  112.     hue = hue + random8(30,61);  // Shift the hue to something new.
  113.   }
  114.  
  115.  
  116.   EVERY_N_MILLISECONDS(holdTime){  // Advance pixels to next position.
  117.  
  118.     // Advance pixel postion down strip, and rollover if needed.
  119.     advance = (advance + delta + NUM_LEDS) % NUM_LEDS;
  120.  
  121.     // Fade out tail or set back to black for next loop around.
  122.     if (fadingTail == 1) {
  123.       fadeToBlackBy(leds, NUM_LEDS,fadeRate);
  124.     } else {
  125.       for (uint8_t i=0; i<(NUM_LEDS/spacing); i++){
  126.         for (uint8_t w = 0; w<width; w++){
  127.           pos = (spacing * (i-1) + spacing + advance + w - 1) % NUM_LEDS;
  128.           leds[pos] = CRGB::Black;
  129.         }
  130.       }
  131.     }
  132.  
  133.     // Update pixels down the strip.
  134.     for (uint8_t i=0; i<(NUM_LEDS/spacing); i++){
  135.       for (uint8_t w = 0; w<width; w++){
  136.         pos = (spacing * (i-1) + spacing + advance + w) % NUM_LEDS;
  137.         if ( w % 2== 0 ){  // Is w even or odd?
  138.           color = hue;
  139.         } else {
  140.           color = hue + hue2_shift;
  141.         }
  142.  
  143.         leds[pos] = CHSV(color,255,255);
  144.       }
  145.     }
  146.  
  147.     //FastLED.show();  //Not calling show here, only in main loop.
  148.  
  149.   }//end_every_n
  150.  
  151. }//end_marquee
Advertisement
Add Comment
Please, Sign In to add comment