Hi Guys,
I'm having some problems with a last minute project (it goes live this weekend!) and I was hoping someone could help me out. I'm trying to control a string of 12 shiftbrites from an arduino and some input from a photocell. I got my code working perfectly with two shiftbrites. The problem is when I add any amount of additional shiftbrites to the chain, the additional ones aren't working. The arduino is powered by usb and the chain is powered by a 6v 1.5A wall adapter that also grounded to the arduino. The shiftbrites are connected to each other with 24" 6pin jumper harness from pololu (Pololu - 6x1 F-F 24" Cable for ShiftBrites and ShiftBars). I also tried a 9v 650mA and a 5v 1A wall adapters with the same results. I also tried smaller chains of 3 and 4. Is there something I'm missing to get the additional ones working?
For reference, here's my code:
#define clockpin 13 // DI
#define enablepin 10 // LI
#define latchpin 9 // EI
#define datapin 11 // CI
#define NumLEDs 12
int LEDChannels[NumLEDs][3] = {0};
int SB_CommandMode;
int SB_RedCommand;
int SB_GreenCommand;
int SB_BlueCommand;
int count = 0;
int sensorPin = 0;
int sensorValue = 0;
int curR = random(1023);
int curG = random(1023);
int curB = random(1023);
// threshold values for interaction
int user1sensorVal = 700;
int user2sensorVal = 300;
void setup() {
pinMode(datapin, OUTPUT);
pinMode(latchpin, OUTPUT);
pinMode(enablepin, OUTPUT);
pinMode(clockpin, OUTPUT);
SPCR = (1<<SPE)|(1<<MSTR)|(0<<SPR1)|(0<<SPR0);
digitalWrite(latchpin, LOW);
digitalWrite(enablepin, LOW);
//Serial.begin(19200);
//Serial.println("Module ready");
}
void SB_SendPacket() {
if (SB_CommandMode == B01) {
SB_RedCommand = 120;
SB_GreenCommand = 100;
SB_BlueCommand = 100;
}
SPDR = SB_CommandMode << 6 | SB_BlueCommand>>4;
while(!(SPSR & (1<<SPIF)));
SPDR = SB_BlueCommand<<4 | SB_RedCommand>>6;
while(!(SPSR & (1<<SPIF)));
SPDR = SB_RedCommand << 2 | SB_GreenCommand>>8;
while(!(SPSR & (1<<SPIF)));
SPDR = SB_GreenCommand;
while(!(SPSR & (1<<SPIF)));
}
void WriteLEDArray() {
SB_CommandMode = B00; // Write to PWM control registers
for (int h = 0;h<NumLEDs;h++) {
SB_RedCommand = LEDChannels[h][0];
SB_GreenCommand = LEDChannels[h][1];
SB_BlueCommand = LEDChannels[h][2];
SB_SendPacket();
}
delayMicroseconds(15);
digitalWrite(latchpin,HIGH); // latch data into registers
delayMicroseconds(15);
digitalWrite(latchpin,LOW);
SB_CommandMode = B01; // Write to current control registers
for (int z = 0; z < NumLEDs; z++) SB_SendPacket();
delayMicroseconds(15);
digitalWrite(latchpin,HIGH); // latch data into registers
delayMicroseconds(15);
digitalWrite(latchpin,LOW);
}
void fadeall(int rate, int fromred, int fromgreen, int fromblue, int tored, int togreen, int toblue) {
for (int i = 0; i < 33; i++) {
for (int j1 = 0; j1 < NumLEDs; j1++) {
LEDChannels[j1][0] = (fromred * (32 - i) + tored * i)/32;
LEDChannels[j1][1] = (fromgreen * (32 - i) + togreen * i)/32;
LEDChannels[j1][2] = (fromblue * (32 - i) + toblue * i)/32;
}
WriteLEDArray();
delay(rate);
}
}
void loop() {
// read photocell value
sensorValue = analogRead(sensorPin);
//Serial.println(sensorValue);
if (sensorValue<user2sensorVal) {
// pulse red when user blocks all light
fadeall(50, 1023, 0, 0, 0, 0, 0);
fadeall(50, 0, 0, 0, 1023, 0, 0);
} else if(sensorValue<user1sensorVal){
// set chain to blue when shadow is detected
for (int i = 0; i < NumLEDs; i++) {
LEDChannels[i][0] = 0;
LEDChannels[i][1] = 0;
LEDChannels[i][2] = 1023;
}
WriteLEDArray();
delay(200);
} else {
// randomly fade colors on default
int tempR = random(1023);
int tempG = random(1023);
int tempB = random(1023);
fadeall(50, curR, curG, curB, tempR, tempG, tempB);
curR = tempR;
curG = tempG;
curB = tempB;
}
}
Thanks in advance!