Skip to content

Instantly share code, notes, and snippets.

@greymechanic
Created June 18, 2013 19:19
Show Gist options
  • Save greymechanic/5808391 to your computer and use it in GitHub Desktop.
Save greymechanic/5808391 to your computer and use it in GitHub Desktop.
// Receive multiple numeric fields using Arduino 1.0 Stream parsing
#include "SPI.h"
#include "Adafruit_WS2801.h"
#include "TimerOne.h"
Adafruit_WS2801 strip = Adafruit_WS2801(32);
const int NUMBER_OF_FIELDS = 96; // how many comma-separated fields we expect
int fieldIndex = 0; // the current field being received
int values[NUMBER_OF_FIELDS]; // array holding values for all the fields
void setup() {
Serial.begin(115200); // Initialize serial port to send and receive at 9600 baud
Serial.flush();
strip.begin();
strip.show();
Timer1.initialize();
Timer1.attachInterrupt(callback, 1000000 / 60); // 60 frames/second
}
void loop() {
if( Serial.available() > 0) {
while(Serial.available() > 0) {
for(fieldIndex = 0; fieldIndex < NUMBER_OF_FIELDS; fieldIndex ++) {
values[fieldIndex] = Serial.parseInt(); // get a numeric value
}
//Serial.print( fieldIndex);
//Serial.println(" fields received:");
for(int i=0; i < fieldIndex; i+=3) {
//Serial.println(values[i]);\
strip.setPixelColor(i/3, Color(values[i+2],values[i+1],values[0]));
strip.show();
}
fieldIndex = 0; // ready to start over
}
}
}
//--------------------------------------------------------------
// 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment