RGB ShiftBar fade (ShiftBrite)

Hello. I’m new here. I’m setting up controllable lights for some pumpkins and have two of these cool ShiftBar boards and a Satellite modules from macetech. I’m only running one of these for now for testing and I have something close to what I want, but I am getting unexpected flashes from other LEDs (channels) when trying to fade only one color. Here is my slightly modified code from macetech blog.

int datapin = 13; // DI
int latchpin = 12; // LI
int enablepin = 11; // EI
int clockpin = 10; // CI

int fade = 3;

unsigned long SB_CommandPacket;
int SB_CommandMode;
int SB_BlueCommand;
int SB_RedCommand;
int SB_GreenCommand;

void setup() {
pinMode(datapin, OUTPUT);
pinMode(latchpin, OUTPUT);
pinMode(enablepin, OUTPUT);
pinMode(clockpin, OUTPUT);

Serial.begin(9600);

digitalWrite(latchpin, LOW);
digitalWrite(enablepin, LOW);

}

void SB_SendPacket() {
SB_CommandPacket = SB_CommandMode & B11;
SB_CommandPacket = (SB_CommandPacket << 10) | (SB_BlueCommand & 1023);
SB_CommandPacket = (SB_CommandPacket << 10) | (SB_RedCommand & 1023);
SB_CommandPacket = (SB_CommandPacket << 10) | (SB_GreenCommand & 1023);

shiftOut(datapin, clockpin, MSBFIRST, SB_CommandPacket >> 24);
shiftOut(datapin, clockpin, MSBFIRST, SB_CommandPacket >> 16);
shiftOut(datapin, clockpin, MSBFIRST, SB_CommandPacket >> 8);
shiftOut(datapin, clockpin, MSBFIRST, SB_CommandPacket);

delay(1); // adjustment may be necessary depending on chain length
digitalWrite(latchpin,HIGH); // latch data into registers
delay(1); // adjustment may be necessary depending on chain length
digitalWrite(latchpin,LOW);
}

void loop() {

SB_CommandMode = B01; // Write to current control registers
SB_RedCommand = 0; // Full current
SB_GreenCommand = 0; // Full current
SB_BlueCommand = 127; // Full current
SB_SendPacket();

SB_CommandMode = B00; // Write to PWM control registers
//SB_RedCommand = 0; // Maximum red
//SB_GreenCommand = 0; // Minimum green
SB_BlueCommand = fade; // Minimum blue
SB_SendPacket();

if(fade < 1004){
fade = fade + 20;
}

Serial.println(fade);

delay(50);

}

I am getting the blue to fade nicely from 3 - 1023, but the red and green channels flash a little every time it cycles through the loop. Any ideas what I might be doing wrong here? (I will want the red and green to not turn on at some point, just not on its own will.)

Thanks, Patrick

Here is my setup.

Arduino setup - duemilanove - arduino 0016 - power from 7.5v, 700mA wall wart (measures 10.3volts when its all running)

ShiftBar header connections - CI, EL, LI, DI are going to digital pins 10, 11, 12, 13 on arduino respectively - Gnd and V+ are connected to the 7.5v rail on a breadboard

ShiftBar screw terminals (from left->right respectively) +, -, +, B, R, G connects to... 7.5v+ rail on breadboard, Gnd rail on breadboard, V+ on Satellite, Blue on Satellite, Red on Satellite, Green on Satellite

Replying on my phone right now so I can't paste code...but you should be leaving the current settings alone after you set them to 127 or whatever works. Also, you need to set the PWM registers to zero instead of commenting out those lines.

I'll try to find sone example code for you later tonight...

I tried those hints out and still getting the “chattering” of red and green lights. Here is the updated code with a standard pinout and some ghetto conditionals for continuous fade up / fade down.

int datapin = 11; // DI
int latchpin = 9; // LI
int enablepin = 10; // EI
int clockpin = 13; // CI

int fade = 3;
boolean runOnce = true;
boolean up = true;

unsigned long SB_CommandPacket;
int SB_CommandMode;
int SB_BlueCommand;
int SB_RedCommand;
int SB_GreenCommand;

void setup() {
pinMode(datapin, OUTPUT);
pinMode(latchpin, OUTPUT);
pinMode(enablepin, OUTPUT);
pinMode(clockpin, OUTPUT);

Serial.begin(9600);

digitalWrite(latchpin, LOW);
digitalWrite(enablepin, LOW);

}

void SB_SendPacket() {
SB_CommandPacket = SB_CommandMode & B11;
SB_CommandPacket = (SB_CommandPacket << 10) | (SB_BlueCommand & 1023);
SB_CommandPacket = (SB_CommandPacket << 10) | (SB_RedCommand & 1023);
SB_CommandPacket = (SB_CommandPacket << 10) | (SB_GreenCommand & 1023);

shiftOut(datapin, clockpin, MSBFIRST, SB_CommandPacket >> 24);
shiftOut(datapin, clockpin, MSBFIRST, SB_CommandPacket >> 16);
shiftOut(datapin, clockpin, MSBFIRST, SB_CommandPacket >> 8);
shiftOut(datapin, clockpin, MSBFIRST, SB_CommandPacket);

delay(1); // adjustment may be necessary depending on chain length
digitalWrite(latchpin,HIGH); // latch data into registers
delay(1); // adjustment may be necessary depending on chain length
digitalWrite(latchpin,LOW);
}

void loop() {

if(runOnce){
SB_CommandMode = B01; // Write to current control registers
SB_RedCommand = 0; // Full current
SB_GreenCommand = 0; // Full current
SB_BlueCommand = 127; // Full current
SB_SendPacket();
}

SB_CommandMode = B00; // Write to PWM control registers
SB_RedCommand = 0; // Maximum red
SB_GreenCommand = 0; // Minimum green
SB_BlueCommand = fade; // Minimum blue
SB_SendPacket();

if(fade < 1004 && up){
fade = fade + 20;
}

if(fade > 3 && !up){
fade = fade - 20;
}

if(fade == 1023){
up = false;
}
if(fade == 3){
up = true;
}

Serial.println(fade);
runOnce = false;

delay(50);

}

Thanks for you help. Patrick.

Can you try running the following code? Also, I’m a little suspicious of your power supply. It just sounds like a transformer instead of a regulated switching supply. Please also make sure that all the grounds involved are tied together.

/* Ports and Pins
 
 Direct port access is much faster than digitalWrite.
 You must match the correct port and pin as shown in the table below.
 
 Arduino Pin        Port        Pin
 13 (SCK)           PORTB       5
 12 (MISO)          PORTB       4
 11 (MOSI)          PORTB       3
 10 (SS)            PORTB       2
 9                  PORTB       1
 8                  PORTB       0
 7                  PORTD       7
 6                  PORTD       6
 5                  PORTD       5
 4                  PORTD       4
 3                  PORTD       3
 2                  PORTD       2
 1 (TX)             PORTD       1
 0 (RX)             PORTD       0
 A5 (Analog)        PORTC       5
 A4 (Analog)        PORTC       4
 A3 (Analog)        PORTC       3
 A2 (Analog)        PORTC       2
 A1 (Analog)        PORTC       1
 A0 (Analog)        PORTC       0
 
 */

// Defines for use with Arduino functions
#define clockpin   13 // CL
#define enablepin  10 // BL
#define latchpin    9 // XL
#define datapin    11 // SI

// Defines for direct port access
#define CLKPORT PORTB
#define ENAPORT PORTB
#define LATPORT PORTB
#define DATPORT PORTB
#define CLKPIN  5
#define ENAPIN  2
#define LATPIN  1
#define DATPIN  3

// Variables for communication
unsigned long SB_CommandPacket;
int SB_CommandMode;
int SB_BlueCommand;
int SB_RedCommand;
int SB_GreenCommand;

// Define number of ShiftBrite modules
#define NumLEDs 2

// Create LED value storage array
int LEDChannels[NumLEDs][3] = {0};

// Set pins to outputs and initial states
void setup() {
  pinMode(datapin, OUTPUT);
  pinMode(latchpin, OUTPUT);
  pinMode(enablepin, OUTPUT);
  pinMode(clockpin, OUTPUT);
  digitalWrite(latchpin, LOW);
  digitalWrite(enablepin, LOW);
  SPCR = (1<<SPE)|(1<<MSTR)|(0<<SPR1)|(0<<SPR0);
}

// Load values into SPI register
void SB_SendPacket() {
 
    if (SB_CommandMode == B01) {
     SB_RedCommand = 127;
     SB_GreenCommand = 110;
     SB_BlueCommand = 110;
    }
 
    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)));
 
}

// Latch values into PWM registers
void SB_Latch() {

  delayMicroseconds(1);
  LATPORT += (1 << LATPIN);
  //ENAPORT += (1 << ENAPIN);
  delayMicroseconds(1);
  //ENAPORT &= ~(1 << ENAPIN);
  LATPORT &= ~(1 << LATPIN);

}

// Send all array values to chain
void WriteLEDArray() {

  SB_CommandMode = B00; // Write to PWM control registers

  for (int i = 0; i<NumLEDs; i++) {
    SB_RedCommand = LEDChannels[i][0] & 1023;
    SB_GreenCommand = LEDChannels[i][1] & 1023;
    SB_BlueCommand = LEDChannels[i][2] & 1023;
    SB_SendPacket();
  }

  SB_Latch();

  SB_CommandMode = B01; // Write to current control registers

  for (int z = 0; z < NumLEDs; z++) SB_SendPacket();   

  SB_Latch();

}

// Fade between two colors at specified interval
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() {

  fadeall(20,0,0,0,1023,0,0);
  fadeall(20,1023,0,0,0,1023,0);
  fadeall(20,0,1023,0,0,0,1023);
  fadeall(20,0,0,1023,1023,0,0);
  fadeall(20,1023,0,0,0,0,0);
  
  delay(500);
  fadeall(60,0,0,0,0,0,1023);
  delay(500);
  fadeall(60,0,0,1023,0,0,0);  
  delay(500);
  
}

Tried this (copy paste), and all channels are on what seems like full power, and twitching very slightly. Nothing is fading. My power source is an extra linksys wall wart transformer. Everything is grounded together. I'm going to pick up a computer power supply tomorrow if I can find one, and try that. I'll keep you posted.

Thanks! Patrick

Sorry, forgot to enable SPI in that. I edited the previous code, just copy and paste it again. This time I actually tested it. :)

I'm only using one ShiftBar, so I changed "#define NumLEDs 2" to "#define NumLEDs 1" , but I don't think this should make a difference.

Nothing fading. Green is really bright, blue is a little more than half brightness and red is slightly on.

So I just hooked up my other ShiftBar out of curiosity, and it works like a charm. I guess I might have fried the other one. :-/

Thanks for the code though! It's really smooth.

Hi again,

I’m having another strange problem with the ShiftBars. I’m using two new ShiftBars now and when I run your code from above the fading works great as long as the Arduino is plugged into my computer over USB or I touch the USB plug when it’s not plugged in. When it’s not plugged in or being held, the lights just go kind of crazy and there is lots a flickering.

My power supply is now a switching PS and I’ve got 12v going to the Arduino and 12v to the ShiftBars.

The only thing I’m suspicious of now are the power cables going to the ShiftBars. They are breadboard jumpers ( http://www.makershed.com/ProductDetails.asp?ProductCode=MKSEEED3 ) and they are going through 2 breadboards before they hit the ShiftBar. Would that cause this problem?

I've used those jumpers before, without problems, for a few ShiftBars running maximum current. Not an optimal solution but it should be okay. Odd that it works when you touch the USB plug. Are all the grounds involved connected together?

If you could push your whole setup together enough to fit into a nice well lit focused photo, I could look around and see if anything appears out of place.

So I think I might have found the culprit. When any of the trim pots on the ShiftBar are set to max while running the fade script, all the LED's flip out and flicker to white very rapidly and not together. Fading becomes smooth and normal once I turn down the trim pots. When set to around 3/4 of a turn, things are fine! Maybe its a current thing?

I tried the ShiftBar I thought I fried the other day, and now it works like a charm. It was probably never broken.

I'm not running any more ShiftBar power through the little jumpers, but directly to the power rails on a breadboard. The breadboard also powers the Arduino, so yes, everything is common.

My power supply is rated at 4amps for 12v and 5v. Nothing is plugged into the 5v side. It's a PS out of an older Sun server.

Here is a video of the setup. http://www.youtube.com/watch?v=mVhjo_7Pfrw

Yes, 3/4 of a turn is about 100mA, which is what the arrays should be running. All the way up and it's going to put about 150mA through each channel, which is too much. Wouldn't really explain the flickering, though...

How many ShiftBars can you chain together? Can you do 25? That would be something like 7.5A..... correct?!?!?! I'd probably fry something trying to rig that up...

I'm looking for a solution to do a 5x5 RGB "disco floor" coffee table...

Yep, 25 is no problem…we’ve done 32, and other customers have done over 100. You just need to make sure to connect power frequently using the extra power terminals on the ShiftBar, since the communication cables couldn’t carry 7.5A as you say. You’d probably want to connect them one at a time and test, being very careful not to reverse any connectors.

What LED voltage are you thinking of using? If it’s 12V, this supply would work fine: http://www.mpja.com/prodinfo.asp?number=16013+PS

Awesome, thanks for the info...

Would you hook it up like the guy with the pumpkins (Power terminal to power terminal all the way down the line?) Also, what kind/gauge of wire would you use to carry that load???

I'm having a hard time conceptualizing what amount of current would be on each leg of the circuit supplying power to each ShiftBar as you go down the line... :-/

In your opinion, could you use the ShiftBar do create something like this:

http://www.youtube.com/watch?v=zCA79Du-WqY&feature=related

... but with Full Color... I'm just wondering about the brightness and such... I'd have to get the right kind of material for the top to make it work with whatever the brightness and dispersion is...