Skip to content

Instantly share code, notes, and snippets.

@lovettbarron
Last active December 11, 2015 10:08
Show Gist options
  • Save lovettbarron/4584635 to your computer and use it in GitHub Desktop.
Save lovettbarron/4584635 to your computer and use it in GitHub Desktop.
Having a hell of a time with this arduino sketch. It's for this installation: http://www.andrewlb.com/2013/01/firesite-debug-view/ I think I'm getting there though. It is currently not quite working, but kind of working. I had misunderstood how the LPD8806 chip worked, switched over to only using the PWM pins. I'm going to have to split the sign…
#include "LPD8806.h"
#include "SPI.h"
// Constants
const int NUMLIGHTS = 6; // Number of lights per arm
const int NUMARMS = 3; // Number of arms per light
const int LEDPERARM = 20; // Number of lights
const int serialPin = 52;
const int testPin = 53;
bool connected;
int dataPins[NUMLIGHTS/**NUMARMS*/] = {
// 22,24,26, // Light 0
// 28,30,32, // Light 1
// 34,36,38, // Light 2
// 40,42,44, // Light 3
// 46,48,50, // Light 4
// 4,6,8 // Light 5
2,
4,
6,
8,
10,
12
};
int clockPins[NUMLIGHTS/**NUMARMS*/] = {
// 23,25,27, // Light 0
// 29,31,33, // Light 1
// 35,37,39, // Light 2
// 41,43,45, // Light 3
// 47,49,51, // Light 4
// 5,7,9 // Light 5
3,
5,
7,
9,
11,
13
};
// Light struct
typedef struct {
int id; // The id must match that of the OF program
int pwr; // 0-127 val, derived from OF float val
LPD8806 strip[NUMARMS]; // The three strips
} Light;
Light Lights[NUMLIGHTS]; // Declare lights array
// Serial communication
String inString = "";
int curLight = 0;
//int curArm = 0;
int curPwr = 0;
void setup() {
// Setup all lights and arms
pinMode(testPin,OUTPUT);
pinMode(serialPin,OUTPUT);
for(int i=0;i<NUMLIGHTS;i++) {
// Set up the Lights with the appropriate objects
Lights[i].id = i;
for(int a=0;a<NUMARMS;a++) {
Lights[i].strip[a] = LPD8806(LEDPERARM, dataPins[i+a], clockPins[i+a]);
Lights[i].strip[a].begin();
Lights[i].strip[a].show();
testStrip(&Lights[i].strip[a]);
clearStrip(&Lights[i].strip[a]);
}
}
// Start serial communication
Serial.begin(9600); connected = false;
digitalWrite(testPin, HIGH);
delay(500);
}
void testStrip(LPD8806 * strip) {
int i;
for(i=0; i<strip->numPixels(); i++) {
strip->setPixelColor(i, 0);
}
strip->show();
// Then display one pixel at a time:
for(i=0; i<strip->numPixels(); i++) {
strip->setPixelColor(i, getColor(127) ); // Set new pixel 'on'
strip->show(); // Refresh LED states
strip->setPixelColor(i, 0); // Erase pixel, but don't refresh!
delay(25);
}
strip->show(); // Refresh to turn off last pixel
}
void clearStrip(LPD8806 * strip) {
int i;
for (i=0; i < strip->numPixels(); i++) {
strip->setPixelColor(i, getColor(0));
strip->show();
delay(10);
}
}
void loop() {
if(connected) {
digitalWrite(serialPin,HIGH);
digitalWrite(testPin, LOW);
for(int i=0;i<NUMLIGHTS;i++) {
render(i);
}
digitalWrite(testPin, HIGH);
} else {
digitalWrite(serialPin,LOW);
digitalWrite(testPin, HIGH);
for(int i=0;i<NUMLIGHTS;i++) {
//Lights[i].pwr = 127;
// render(i);
for( int a=0;a<NUMARMS;a++) {
testStrip(&Lights[i].strip[a]);
}
}
digitalWrite(testPin, LOW);
}
}
void render(uint32_t lightId) {
int maxPower, minPower, ptr, ledStr, pwr;
for(int a=0;a<NUMARMS;a++) {
// Turn on
for(int i=0;i<LEDPERARM;i++) {
Lights[lightId].strip[a].setPixelColor(i,0);
maxPower = i / LEDPERARM;
minPower = 127 - (maxPower * (127-Lights[lightId].pwr));
// ptr = i + ( a * LEDPERARM );
pwr = Lights[lightId].pwr;
if(pwr > 0)
Lights[lightId].strip[a].setPixelColor(i,getColor(pwr));
// Lights[lightId].strip[a].show();
// Lights[lightId].strip[a].setPixelColor(i,127);
}
Lights[lightId].strip[a].show();
delay(10);
// Turn all off
}
//for(int a=0;a<NUMARMS;a++) {
// for(int i=0;i<LEDPERARM;i++) {
// Lights[lightId].strip[a].setPixelColor(i,0);
// }
// Lights[lightId].strip[a].show();
//}
// holders for 10ms
}
// Derived from the LPD8806 lib
uint32_t getColor(int pwr) {
byte r,g,b;
r = (byte)1-(pwr/2);
b = (byte)(pwr/2);
g = (byte)0;
/*
r = 127;
b = 60;
g = 0; */
return ((uint32_t)(g | 0x80) << 16) |
((uint32_t)(r | 0x80) << 8) |
b | 0x80 ;
}
void parseSerial() {
// Read serial input:
int inChar;
while(Serial.available()) {
inChar = Serial.read();
// Collecting the datas.
if (isDigit(inChar)) {
inString += (char)inChar;
}
// Iterating through the lights
if (inChar == ',') {
Lights[curLight].pwr = inString.toInt();
inString = "";
curLight++;
}
// Last light
if (inChar == '\n') {
Lights[NUMLIGHTS-1].pwr = inString.toInt();
inString = "";
curLight = 0;
}
Serial.println(curLight);
Serial.println(Lights[curLight].pwr);
}
}
void serialEvent() {
connected = true;
parseSerial();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment