Skip to content

Instantly share code, notes, and snippets.

@tomekr
Created August 30, 2015 03:54
Show Gist options
  • Save tomekr/4c78c794c33c0f1ea55f to your computer and use it in GitHub Desktop.
Save tomekr/4c78c794c33c0f1ea55f to your computer and use it in GitHub Desktop.
#include "LPD8806.h"
#include "SPI.h"
#include "RGBConverter.h"
// Simple test for 160 (5 meters) of LPD8806-based RGB LED strip
// Not compatible with Trinket/Gemma due to limited RAM
/*****************************************************************************/
// Number of RGB LEDs in strand:
int nLEDs = 4;
// Chose 2 pins for output; can be any valid output pins:
int clockPin = 2;
int dataPin = 3;
int dataPin2 = 4;
int dataPin3 = 5;
int dataPin4 = 7;
int dataPin5 = 8;
// 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(nLEDs, dataPin, clockPin);
LPD8806 strip2 = LPD8806(nLEDs, dataPin2, clockPin);
LPD8806 strip3 = LPD8806(nLEDs, dataPin3, clockPin);
LPD8806 strip4 = LPD8806(nLEDs, dataPin4, clockPin);
LPD8806 strip5 = LPD8806(nLEDs, dataPin5, clockPin);
// You can optionally use hardware SPI for faster writes, just leave out
// the data and clock pin parameters. But this does limit use to very
// specific pins on the Arduino. For "classic" Arduinos (Uno, Duemilanove,
// etc.), data = pin 11, clock = pin 13. For Arduino Mega, data = pin 51,
// clock = pin 52. For 32u4 Breakout Board+ and Teensy, data = pin B2,
// clock = pin B1. For Leonardo, this can ONLY be done on the ICSP pins.
//LPD8806 strip = LPD8806(nLEDs);
String inputString = ""; // a string to hold incoming data
int input_val;
boolean stringComplete = false; // whether the string is complete
void setup() {
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
// Start up the LED strip
strip.begin();
strip2.begin();
strip3.begin();
strip4.begin();
strip5.begin();
// Update the strip, to start they are all 'off'
strip.show();
strip2.show();
strip3.show();
strip4.show();
strip5.show();
}
void loop() {
if (stringComplete) {
Serial.println(inputString);
input_val = inputString.toInt();
// clear the string:
inputString = "";
stringComplete = false;
}
colorChase(strip.Color(127, input_val, 0), 100); // Red
// colorChase(strip.Color( 0,25, 0), 100); // Green
// colorChase(strip.Color( 0, 0,25), 100); // Blue
// colorChase(strip.Color(25,25,25), 100); // White
}
// Chase one dot down the full strip. Good for testing purposes.
void colorChase(uint32_t c, uint8_t wait) {
int i;
// Start by turning all pixels off:
for(i=0; i<strip.numPixels(); i++) strip.setPixelColor(i, 0);
// Then display one pixel at a time:
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c); // Set new pixel 'on'
strip.show(); // Refresh LED states
strip.setPixelColor(i, 0); // Erase pixel, but don't refresh!
//delay(wait);
strip2.setPixelColor(i, c); // Set new pixel 'on'
strip2.show(); // Refresh LED states
strip2.setPixelColor(i, 0); // Erase pixel, but don't refresh!
strip3.setPixelColor(i, c); // Set new pixel 'on'
strip3.show(); // Refresh LED states
strip3.setPixelColor(i, 0); // Erase pixel, but don't refresh!
strip4.setPixelColor(i, c); // Set new pixel 'on'
strip4.show(); // Refresh LED states
strip4.setPixelColor(i, 0); // Erase pixel, but don't refresh!
strip5.setPixelColor(i, c); // Set new pixel 'on'
strip5.show(); // Refresh LED states
strip5.setPixelColor(i, 0); // Erase pixel, but don't refresh!
delay(wait);
}
strip.show(); // Refresh to turn off last pixel
strip2.show();
strip3.show();
strip4.show();
strip5.show();
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '$') {
stringComplete = true;
}
else {
// add it to the inputString:
inputString += inChar;
}
}
}
//void parseMessage(String msg, double result[6][2]) {
// msg = "2400|10.7|1300|9.7|1300|9.7|1300|9.7|1300|9.7|1300|9.7$"
//
// int pipeDelimiter = msg.indexOf('|');
// String frequency = msg.substring(0, pipeDelimeter);
//
// do
// {
// delay(50); // wait for sensors to stabilize
// x = readSensors(); // check the sensors
//
// } while (msg.indexOf('|') != -1);
//
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment