Go Down

Topic: LPD8806 code and sanity check (Read 757 times) previous topic - next topic

Hey!
So, I have six lighting fixtures with 3 LPD8806 based LED strips each attached to them.
Because of physical limitations of the fixtures, they are connected in parellel, and I can't chain the strips. Each strip has 20 LEDs on it. They aren't the adafruit LEDs, but a roll I ordered from alibaba.
I'm running everything off of an arduino mega, and don't have any additional circuitry, just basically direct connect from arduino to appropriate LED.

Currently, I'm getting just a string of gibberish LEDs. Really hoping to get a sanity check, as I get demos working previously, and I don't understand what's going wrong now The installation is going up in two days, kinda panicked.  Thanks in advance!

To explain the code a bit:
It sets up all the different strips, and runs a test (currently not working).
The strips should be constrained to a particular colour set based off the the individual power values from the Light struct, so between 0-127 red and 0-60 yellow, creating a range from deep red to orange (it's for a fire light)
But ya, currently nothing is happening.


Here's my code (gist here: https://gist.github.com/4584635)
Code: [Select]

#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 testPin = 53;

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 inChar;
int curLight = 0;
//int curArm = 0;
int curPwr = 0;

void setup() {
   // Setup all lights and arms
   pinMode(testPin,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);
   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(Serial.available() == 0) {
       digitalWrite(testPin, LOW);
       for(int i=0;i<NUMLIGHTS;i++) {
           render(i);
       }
       digitalWrite(testPin, HIGH);
   } else {
       digitalWrite(testPin, HIGH);
       for(int i=0;i<NUMLIGHTS;i++) {
           testStrip(&Lights[i].strip[0]);
       }        
       digitalWrite(testPin, LOW);
   }
}                  
                                       
void render(uint32_t lightId) {
   int maxPower, minPower, ptr, ledStr, pwr;
   for(int a=0;a<NUMARMS;a++) {
       for(int i=0;i<LEDPERARM;i++) {
           maxPower = i / LEDPERARM;
           minPower = 127 - (maxPower * (127-Lights[lightId].pwr));
         //  ptr = i + ( a * LEDPERARM );
           pwr = (byte)Lights[lightId].pwr;
           Lights[lightId].strip[a].setPixelColor(i,getColor(pwr));
       }
       Lights[lightId].strip[a].show();
   }
   
   delay(10); // holders for 10ms
}

// Derived from the LPD8806 lib
uint32_t getColor(byte pwr) {    
   byte r,g,b;
   r = (byte)(1 + (.5 * pwr));
   g = (byte)(.5 * pwr);
   b = (byte)0;
   
   return ((uint32_t)(g | 0x80) << 16) |
   ((uint32_t)(r | 0x80) <<  8) |
   b | 0x80 ;
}
                                       
void parseSerial() {
   // Read serial input:
   if (Serial.available() > 0) {
       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;
   }  
}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy