public

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

  • Download Gist
Wearable NeoPixel bike lights
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#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);
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.