Skip to content

Instantly share code, notes, and snippets.

@benjamind
Created August 10, 2013 08:31
Show Gist options
  • Save benjamind/6199599 to your computer and use it in GitHub Desktop.
Save benjamind/6199599 to your computer and use it in GitHub Desktop.
More LED testing...
// include the neo pixel library
#include <Adafruit_NeoPixel.h>
// how many leds in our string?
static const int NUM_LEDS =21*6;
static const int TOTAL_BYTES = NUM_LEDS*3;
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_RGB Pixels are wired for RGB bitstream
// NEO_GRB Pixels are wired for GRB bitstream
// NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels)
// NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, 6, NEO_RGB + NEO_KHZ800);
// buffer to hold colors of our LEDs
char colorValues[TOTAL_BYTES];
char buffer[TOTAL_BYTES];
int index = 0;
boolean bufferReady = true;
boolean secondBuffer = false;
void setup() {
strip.begin();
strip.show();
// initialize to black (off)
for (int i=0; i < NUM_LEDS; i++) {
int d = i*3;
colorValues[d] = 255;
colorValues[d+1] = 0;
colorValues[d+2] = 255;
buffer[d] = 0;
buffer[d+1] = 0;
buffer[d+2] = 0;
}
// initialize the strip to the current values
for(int i=0; i<NUM_LEDS; i++) {
int d = i*3;
uint32_t c = strip.Color(colorValues[d], colorValues[d+1], colorValues[d+2]);
strip.setPixelColor(i, c);
}
// update the strip
strip.show();
//Initialize serial and wait for port to open:
Serial.begin(57600);
while (!Serial) {
; // wait for port
}
}
void loop() {
// wait for bytes on serial port
if (bufferReady) {
for(int i=0; i<NUM_LEDS; i++) {
int d = i*3;
uint32_t c;
if (secondBuffer) {
c = strip.Color(buffer[d+1], buffer[d], buffer[d+2]);
} else {
c = strip.Color(colorValues[d+1], colorValues[d], colorValues[d+2]);
}
strip.setPixelColor(i, c);
}
// update the strip
strip.show();
bufferReady = false;
}
}
int writeIndex = 0;
int remaining = TOTAL_BYTES;
char count = 0;
void serialEvent() {
while (Serial.available() > 0) {
// figure out how many bytes are remaining unwritten in the current buffer
remaining = TOTAL_BYTES-writeIndex;
// attempt to read that many bytes
count = 0;
if (secondBuffer) {
count = Serial.readBytes(&colorValues[writeIndex],remaining);
} else {
count = Serial.readBytes(&buffer[writeIndex],remaining);
}
// advance writeIndex
writeIndex += count;
// check if we've hit the total bytes
if (writeIndex >= TOTAL_BYTES) {
// we have so switch buffers now and loop
writeIndex = 0;
// set data!
bufferReady = true;
secondBuffer = !secondBuffer;
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment