Created
January 18, 2014 22:37
-
-
Save ennui2342/8497773 to your computer and use it in GitHub Desktop.
Example code to produce the dual 'Knight Rider' effect in the 'Wearable Neopixel bike lights' project as described in this blog post: http://ecafe.org/blog/2014/01/18/wearable-neopixel-bike-lights.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Adafruit_NeoPixel.h> | |
#define PIN 1 | |
#define NUM_PIXELS 20 | |
#define NUM_NODES 2 | |
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_PIXELS, PIN, NEO_GRB + | |
NEO_KHZ800); | |
uint32_t state[NUM_PIXELS]; | |
// Pixel positions start at 1 | |
// start pixel, end pixel, direction of travel 1|2, initial pixel | |
position, color | |
uint32_t nodes[NUM_NODES][6] = { | |
{ 1, 10, 1, 5, 0xFF1000 }, | |
{ 11, 20, 2, 20, 0xFFFFFF } | |
}; | |
void setup() { | |
strip.begin(); | |
strip.setBrightness(32); | |
strip.show(); | |
} | |
void loop() { | |
fadeStrip(2); | |
updateNodes(); | |
strip.show(); | |
delay(70); | |
} | |
void updateNodes() { | |
for(int i = 0; i<NUM_NODES; i++) { | |
switch(nodes[i][2]) { | |
case 1: | |
nodes[i][3]++; | |
if(nodes[i][3] == nodes[i][1]) { | |
nodes[i][2] = 2; | |
} | |
break; | |
case 2: | |
nodes[i][3]--; | |
if(nodes[i][3] == nodes[i][0]) { | |
nodes[i][2] = 1; | |
} | |
break; | |
} | |
strip.setPixelColor(nodes[i][3]-1, nodes[i][4]); | |
state[nodes[i][3]-1] = nodes[i][4]; | |
} | |
} | |
void fadeStrip(uint8_t rate) { | |
for(int i = 0; i<NUM_PIXELS; i++) { | |
state[i] = dimColor(state[i], rate); | |
strip.setPixelColor(i, state[i]); | |
} | |
} | |
// Borrowed the below from NeoPixel-KnightRider | |
uint32_t dimColor(uint32_t color, uint8_t width) { | |
return (((color&0xFF0000)/width)&0xFF0000) + (((color&0x00FF00)/width)&0x00FF00) + (((color&0x0000FF)/width)&0x0000FF); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment