Guest User

FastLED TeensyLC weird timing behavior

a guest
Apr 5th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include "FastLED.h"
  2.  
  3. //The way this code should behave is there should be 100 LEDS that are lit green
  4. //  and 152 LEDS that are lit blue. And they "chase" eachother.
  5. //  The animation should repeat every 1 second.
  6.  
  7. class LEDController {
  8. private:
  9.     static const uint8_t LED_COUNT{252};
  10.     CRGB colors[LED_COUNT];
  11.     uint32_t animationDurationMS{1000};
  12.     //color1 is green, color2 is blue
  13.     const CRGB color1{0,255,0}, color2{0,0,100};
  14.     //There will be 100 green LEDs and 152 blue LEDs
  15.     const uint8_t COLOR_1_LED_COUNT{100};
  16.     void updateColors(uint8_t color1StartPos);
  17. public:
  18.     LEDController();
  19.     void advance(uint32_t millisecondsNow);
  20.     void show();
  21. };
  22.    
  23. void LEDController::updateColors(uint8_t color1StartPos) {
  24.     //Update the values of the array of colors
  25.     for (uint16_t i=0; i<COLOR_1_LED_COUNT; ++i) {
  26.         colors[(i+color1StartPos)%LED_COUNT] = color1;
  27.     }
  28.     for (uint16_t i=COLOR_1_LED_COUNT; i<LED_COUNT; ++i) {
  29.         colors[(i+color1StartPos)%LED_COUNT] = color2;
  30.     }
  31. }
  32.  
  33. LEDController::LEDController() {
  34.     const uint8_t DATA_PIN = 0;
  35.     FastLED.addLeds<WS2812B, DATA_PIN, GRB>(colors, LED_COUNT);
  36. }
  37.  
  38. void LEDController::advance(uint32_t millisecondsNow) {
  39.     //This determines where the start of the green LEDs should be
  40.     double percentComplete = static_cast<double>(millisecondsNow)/animationDurationMS;
  41.     uint8_t startingPos = round(percentComplete*LED_COUNT) % LED_COUNT;
  42.     updateColors(startingPos);
  43. }
  44.  
  45. void LEDController::show() {
  46.     FastLED.show();
  47. }
  48.  
  49. void setup() {
  50.     //Set serial baud rate
  51.     const int BAUD_RATE = 2400;
  52.     Serial.begin(BAUD_RATE);
  53.  
  54.     const unsigned long MILLISECOND_SERIAL_WAIT_DELAY = 5;
  55.     while (!Serial) {
  56.         //Wait for serial to init
  57.         delay(MILLISECOND_SERIAL_WAIT_DELAY);
  58.     }
  59.  
  60.     //Turn orange onboard-LED on to indicate ready status
  61.     //config cpu led pin as output
  62.     pinMode(13, OUTPUT);
  63.     digitalWrite(13, true);
  64. }
  65.  
  66.  
  67. void loop() {
  68.     //It's expected that this function will never exit.
  69.     //If it does, it's probably some sort of abnormal failure.
  70.     //In that case, 'loop' will just start over and
  71.     //  it will seem like the board was power cycled
  72.     //  (global function 'setup' is not called again)
  73.  
  74.     LEDController ledController;
  75.    
  76.     while (1) {
  77.         uint32_t now = millis();
  78.  
  79.         //Let the colors advance to the next state.
  80.         ledController.advance(now);
  81.  
  82.         //Update LEDs respectively
  83.         ledController.show();
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment