Go Down

Topic: Fading Colors with a Shiftbrite (Read 2956 times) previous topic - next topic

nagmier


I have purchased some shiftbrite modules:
http://macetech.com/store/index.php?main_page=product_info&products_id=1

I am trying to get the following code to fade instead of quickly blink between the two colors... I have tried a couple diffent tactics and I've hit a brick wall. Here is a copy of the "example code" macetech provides.

Code: [Select]
#define clockpin 13 // CI
#define enablepin 10 // EI
#define latchpin 9 // LI
#define datapin 11 // DI

#define NumLEDs 2

int LEDChannels[NumLEDs][3] = {0};
int SB_CommandMode;
int SB_RedCommand;
int SB_GreenCommand;
int SB_BlueCommand;

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);


}

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 loop() {

  LEDChannels[0][0] = 1023;
  LEDChannels[0][1] = 0;
  LEDChannels[0][2] = 0;

  LEDChannels[1][0] = 0;
  LEDChannels[1][1] = 0;
  LEDChannels[1][2] = 1023;

  WriteLEDArray();
  delay(200);

  LEDChannels[0][0] = 0;
  LEDChannels[0][1] = 0;
  LEDChannels[0][2] = 1023;

  LEDChannels[1][0] = 1023;
  LEDChannels[1][1] = 0;
  LEDChannels[1][2] = 0;

  WriteLEDArray();
  delay(200);


}


Again any help would be greatly appreciated

macegr

#1
Sep 21, 2009, 08:33 am Last Edit: Sep 21, 2009, 08:34 am by macegr Reason: 1
Aaron,

I sent you an email with the following code on the 17th, maybe there is a problem with your inbound email.

You can use this function to fade between two colors in 32 steps, with a specified delay between each step:

Code: [Select]
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);
}
}
Unique RGB LED Modules and Arduino shields: http://www.macetech.com/store

nagmier

Thanks there must be a problem, I figured you would reply. I'll double check my trash can my spam filter may have grabbed it (I thought I checked it but I have been out of town) Thanks a Bunch I can't wait to test it when I get home tonight!

nagmier

Ok the code works great... on a Duemilanove but when I move to a Pro Mini the LEDs flicker.

This is the code I am using I have even taken out the "fade" just trying to get 2 LEDs to light correctly is proving difficult with the Mini Pro....

Code: [Select]
#define clockpin 13 // CI
#define enablepin 10 // EI
#define latchpin 9 // LI
#define datapin 11 // DI

#define NumLEDs 2

int LEDChannels[NumLEDs][3] = {0};
int SB_CommandMode;
int SB_RedCommand;
int SB_GreenCommand;
int SB_BlueCommand;

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);


}

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() {

  LEDChannels[0][0] = 1023;
  LEDChannels[0][1] = 0;
  LEDChannels[0][2] = 0;
 
  LEDChannels[1][0] = 1023;
  LEDChannels[1][1] = 0;
  LEDChannels[1][2] = 0;
 
  WriteLEDArray();
}


I assume there is some difference in the Mini and the Duemilanov I'm missing... Any help is very much appreciated.

macegr

Can't see any differences in the code or schematic that would cause a problem. Would you happen to have the 3.3V version of the Pro Mini? There would be a high chance that 3.3V levels would fall a hair too low to register as a logic high level on the ShiftBrite.
Unique RGB LED Modules and Arduino shields: http://www.macetech.com/store

nagmier

Sorry for the delay in reply I was out cold yesterday. I have the 5v version Its really odd why this is happening... Its really not a HUGE deal I have 2 Duemilanove's I can use its just a larger footprint than I wanted for my lamps but again its not a real issue there... I may continue to experiment with the mini pro's I have but I'll most likley just use them in a different project :D

At least its not something simple I'm missing thats my main concern.

macegr

As far as wiring is concerned, you may want to pay special attention to grounding. And you should be using a separate supply to power the ShiftBrites; you're not trying to power them from the Pro Mini regulated 5V right?
Unique RGB LED Modules and Arduino shields: http://www.macetech.com/store

nagmier

#7
Sep 28, 2009, 05:03 pm Last Edit: Sep 28, 2009, 05:38 pm by nagmier Reason: 1
No I'm not powering them from the Arduino. I am running them from a power supply purchased from your shop :) I think I received 3.3v Arduinos I'm waiting on a response from SFE to determine if there is a way to verify if they are in fact 3.3 or 5v.

The ones I received with my order were not labeled on the back :(

BTW Thanks a ton for your help already, anyone thats reading mace has been great! I highly recommend them for RGB LEDs!!!

Go Up