Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "FastLED.h"
- //The way this code should behave is there should be 100 LEDS that are lit green
- // and 152 LEDS that are lit blue. And they "chase" eachother.
- // The animation should repeat every 1 second.
- class LEDController {
- private:
- static const uint8_t LED_COUNT{252};
- CRGB colors[LED_COUNT];
- uint32_t animationDurationMS{1000};
- //color1 is green, color2 is blue
- const CRGB color1{0,255,0}, color2{0,0,100};
- //There will be 100 green LEDs and 152 blue LEDs
- const uint8_t COLOR_1_LED_COUNT{100};
- void updateColors(uint8_t color1StartPos);
- public:
- LEDController();
- void advance(uint32_t millisecondsNow);
- void show();
- };
- void LEDController::updateColors(uint8_t color1StartPos) {
- //Update the values of the array of colors
- for (uint16_t i=0; i<COLOR_1_LED_COUNT; ++i) {
- colors[(i+color1StartPos)%LED_COUNT] = color1;
- }
- for (uint16_t i=COLOR_1_LED_COUNT; i<LED_COUNT; ++i) {
- colors[(i+color1StartPos)%LED_COUNT] = color2;
- }
- }
- LEDController::LEDController() {
- const uint8_t DATA_PIN = 0;
- FastLED.addLeds<WS2812B, DATA_PIN, GRB>(colors, LED_COUNT);
- }
- void LEDController::advance(uint32_t millisecondsNow) {
- //This determines where the start of the green LEDs should be
- double percentComplete = static_cast<double>(millisecondsNow)/animationDurationMS;
- uint8_t startingPos = round(percentComplete*LED_COUNT) % LED_COUNT;
- updateColors(startingPos);
- }
- void LEDController::show() {
- FastLED.show();
- }
- void setup() {
- //Set serial baud rate
- const int BAUD_RATE = 2400;
- Serial.begin(BAUD_RATE);
- const unsigned long MILLISECOND_SERIAL_WAIT_DELAY = 5;
- while (!Serial) {
- //Wait for serial to init
- delay(MILLISECOND_SERIAL_WAIT_DELAY);
- }
- //Turn orange onboard-LED on to indicate ready status
- //config cpu led pin as output
- pinMode(13, OUTPUT);
- digitalWrite(13, true);
- }
- void loop() {
- //It's expected that this function will never exit.
- //If it does, it's probably some sort of abnormal failure.
- //In that case, 'loop' will just start over and
- // it will seem like the board was power cycled
- // (global function 'setup' is not called again)
- LEDController ledController;
- while (1) {
- uint32_t now = millis();
- //Let the colors advance to the next state.
- ledController.advance(now);
- //Update LEDs respectively
- ledController.show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment