First Animation

Here is the first piece of code for the start-up and shutdown animations for the rear light. I have tried to add code to control which pixels in the string will illuminate, but it’s kinda hacky. The lights turn on one way, and off in the other direction. Perhaps someone could guide me on how to “improve” this implementation.

This code assumes that the string of lights is connected to PIN 9.

#include <Adafruit_NeoPixel.h>
      #define PIN 9
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800);
    
    void setup() {
      strip.begin();
      strip.show(); // Initialize all pixels to 'off'
    }

void loop() {
  // put your main code here, to run repeatedly:
lightStart(135,0,0,0,16); // Red Value, Green Value, Blue Value, Start From Pixel, End At Pixel
delay(5000);
lightStop(0,0,0,0,16); // Red Value, Green Value, Blue Value, Start From Pixel, End At Pixel
delay(5000);
  }
  
void lightStart(uint32_t r, uint8_t g,uint8_t b,uint8_t s, uint8_t m ) {
  for(uint16_t i=s; i<m; i++) {
    strip.setPixelColor(i,r,g,b);
    strip.show();
    delay(50);
  }
}
void lightStop(uint32_t r, uint8_t g,uint8_t b,uint8_t s, uint8_t m ) {
  for(uint16_t i=m; i>s; i--) {
    
    strip.setPixelColor(i,r,g,b);
    strip.show();
    delay(50);
  }
  strip.setPixelColor(s,r,g,b); // Hack to work with value 0
  strip.show();
  delay(50);
}

Again, this is built on the Adafruit NeoPixel Library, which is required to make this sketch work.

No Comments

Post a Comment