Skip to content

Instantly share code, notes, and snippets.

@suhajdab
Last active September 5, 2020 20:33
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save suhajdab/10561606 to your computer and use it in GitHub Desktop.
Save suhajdab/10561606 to your computer and use it in GitHub Desktop.
#include <Adafruit_NeoPixel.h>
// Pin used on Arduino board
#define PIN 2
// Number of NeoPixels
#define PIXELS 24
// Number of pixels on either side continuously orange to indicate side of vehicle
#define FIXEDPIXELS 1
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXELS, PIN, NEO_GRB + NEO_KHZ800);
uint32_t red = strip.Color(80, 0, 0);
uint32_t orange = strip.Color(80, 40, 0);
uint32_t black = strip.Color(0, 0, 0);
void setup() {
strip.begin();
setFixedPixels();
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
swipe();
scan();
alternate();
rainbowCycle(3);
rainbowCycleReverse(3);
}
void setFixedPixels() {
for (uint16_t x = 0; x < FIXEDPIXELS; x++) {
strip.setPixelColor(x, orange);
strip.setPixelColor(PIXELS - x - 1, orange);
}
}
void swipe() {
for (uint16_t x = 0; x < 3; x++) {
colorWipe(red, 20); // Red
colorWipe(black, 20); // Black
colorWipeReverse(red, 20); // Red
colorWipeReverse(black, 20); // Black
}
}
void clearAll() {
for (uint16_t i = FIXEDPIXELS; i < PIXELS - FIXEDPIXELS; i++) {
strip.setPixelColor(i, black);
}
}
void scan() {
for (uint16_t x = 0; x < 4; x++) {
for (uint16_t i = FIXEDPIXELS; i < PIXELS - FIXEDPIXELS - 1; i++) {
clearAll();
strip.setPixelColor(i, red);
strip.setPixelColor(PIXELS - i - 1, red);
strip.show();
delay(50);
}
}
}
void alternate() {
for (uint16_t x=0; x < 6; x++) {
for (uint16_t j=0; j< 2; j++) {
clearAll();
for (uint16_t i = FIXEDPIXELS; i < PIXELS - FIXEDPIXELS - 1; i+=2) {
strip.setPixelColor(i + j, red);
}
strip.show();
delay(200);
}
}
}
/* Helper functions */
// Create a 24 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b)
{
uint32_t c;
c = r;
c <<= 8;
c |= g;
c <<= 8;
c |= b;
return c;
}
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i = FIXEDPIXELS; i < PIXELS - FIXEDPIXELS; i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
// ColorWipe reverse
void colorWipeReverse(uint32_t c, uint8_t wait) {
for(uint16_t i = PIXELS - FIXEDPIXELS - 1; i > FIXEDPIXELS - 1; i--) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) { // 5 cycles of all colors on wheel
for(i = FIXEDPIXELS; i < PIXELS - FIXEDPIXELS; i++) {
strip.setPixelColor(i, Wheel(((i * 256 / (PIXELS - FIXEDPIXELS)) + j) & 255));
}
strip.show();
delay(wait);
}
}
void rainbowCycleReverse(uint8_t wait) {
uint16_t i, j;
for(j=256; j>0; j--) { // 5 cycles of all colors on wheel
for(i = FIXEDPIXELS; i< PIXELS - FIXEDPIXELS; i++) {
strip.setPixelColor(i, Wheel(((i * 256 / (PIXELS - FIXEDPIXELS)) + j) & 255));
}
strip.show();
delay(wait);
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
return strip.Color(WheelPos, 85 - WheelPos, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(85 - WheelPos, 0, WheelPos);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos, 85 - WheelPos);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment