Download this to your Arduino libraries folder under a folder of the same name.
Something like: git clone https://gist.github.com/4751213.git SerialLED
// LED manipulation code | |
// Borrows code from: | |
// * Adafruit's LED library: https://github.com/adafruit/LPD8806 | |
// * Arduino example code for serial communication | |
// * astromaf's RGBLamp code: https://github.com/astromaf/MyRGBLamp | |
#include <LPD8806.h> | |
#include <SPI.h> | |
int dataPin = 2; | |
int clockPin = 3; | |
int numLEDs = 32; | |
// First parameter is the number of LEDs in the strand. The LED strips | |
// are 32 LEDs per meter but you can extend or cut the strip. Next two | |
// parameters are SPI data and clock pins: | |
LPD8806 strip = LPD8806(numLEDs, dataPin, clockPin); | |
/* | |
Serial RGB controller | |
Reads a serial input string looking for three comma-separated | |
integers with a newline at the end. Values should be between | |
0 and 255. The sketch uses those values to set the color | |
of an RGB LED attached to pins 9 - 11. | |
The circuit: | |
* Common-anode RGB LED cathodes attached to pins 9 - 11 | |
* LED anode connected to pin 13 | |
To turn on any given channel, set the pin LOW. | |
To turn off, set the pin HIGH. The higher the analogWrite level, | |
the lower the brightness. | |
created 29 Nov 2010 | |
by Tom Igoe | |
This example code is in the public domain. | |
*/ | |
String inString = ""; // string to hold input | |
int currentColor = 0; | |
int led = 0; | |
int red, green, blue = 0; | |
void setup() { | |
// Start up the LED strip | |
strip.begin(); | |
// Update the strip, to start they are all 'off' | |
strip.show(); | |
// Open serial communications and wait for port to open: | |
Serial.begin(9600); | |
// send an intro: | |
Serial.println("\n\nString to LED Address and then Int() RGB:"); | |
Serial.println(); | |
} | |
void loop() { | |
int inChar; | |
// Read serial input: | |
if (Serial.available() > 0) { | |
inChar = Serial.read(); | |
} | |
if (isDigit(inChar)) { | |
// convert the incoming byte to a char | |
// and add it to the string: | |
inString += (char)inChar; | |
} | |
// if you get a comma, convert to a number, | |
// set the appropriate color, and increment | |
// the color counter: | |
if (inChar == ',') { | |
// do something different for each value of currentColor: | |
switch (currentColor) { | |
case 0: // 0 = led | |
led = inString.toInt() - 1; | |
if (led < 0 || led >= numLEDs + 1) { | |
led = 0; | |
} | |
// clear the string for new input: | |
inString = ""; | |
break; | |
case 1: // 1 = red | |
red = inString.toInt(); | |
// clear the string for new input: | |
inString = ""; | |
break; | |
case 2: // 2 = green: | |
green = inString.toInt(); | |
// clear the string for new input: | |
inString = ""; | |
break; | |
} | |
currentColor++; | |
} | |
// if you get a newline, you know you've got | |
// the last color, i.e. blue: | |
if (inChar == '\n') { | |
blue = inString.toInt(); | |
// print the color for the LED to change | |
Serial.print("LED: "); | |
Serial.print(led); | |
Serial.print("Red: "); | |
Serial.print(red); | |
Serial.print(", Green: "); | |
Serial.print(green); | |
Serial.print(", Blue: "); | |
Serial.println(blue); | |
// Build the color from the passed numbers | |
uint32_t color = strip.Color(red, green, blue); | |
if (led == numLEDs) { | |
for(int i = 0; i < numLEDs; i++ ) { | |
strip.setPixelColor(i, color); // set one pixel | |
} | |
} | |
else { | |
// Set each pixel in the strip to the given color | |
strip.setPixelColor(led, color); // set one pixel | |
} | |
strip.show(); // refresh strip display | |
// clear the string for new input: | |
inString = ""; | |
// reset the color counter: | |
currentColor = 0; | |
} | |
} |