Skip to content

Instantly share code, notes, and snippets.

@SamClayton
Created April 4, 2014 00:24
Show Gist options
  • Select an option

  • Save SamClayton/9965587 to your computer and use it in GitHub Desktop.

Select an option

Save SamClayton/9965587 to your computer and use it in GitHub Desktop.
Color fades with ShiftBrite and MegaBrite pixels
#define clockpin 13 // CI
#define enablepin 10 // EI
#define latchpin 9 // LI
#define datapin 11 // DI
#define NumLEDs 1
int LEDChannels[NumLEDs][3] = {0};
int SB_CommandMode;
int SB_RedCommand;
int SB_GreenCommand;
int SB_BlueCommand;
const boolean invert = true; // set true if common anode, false if common cathode
int color = 0; // a value from 0 to 255 representing the hue
int R, G, B; // the Red Green and Blue color components
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()
{
int brightness = 255; // 255 is maximum brightness
hueToRGB( color, brightness); // call function to convert hue to RGB
/*
// write the RGB values to the pins
analogWrite(redPin, R);
analogWrite(greenPin, G);
analogWrite(bluePin, B );
// write the RGB values to the second set of pins for the second LED
analogWrite(redPin2, R);
analogWrite(greenPin2, G);
analogWrite(bluePin2, B);
*/
LEDChannels[0][0] = R * 4;
LEDChannels[0][1] = G * 4;
LEDChannels[0][2] = B * 4;
/*
// white out the shiftbrite
LEDChannels[0][0] = 1023;
LEDChannels[0][1] = 1023;
LEDChannels[0][2] = 1023;
*/
WriteLEDArray();
delay(20);
color++; // increment the color
if(color > 255)
color = 0;
delay(10);
}
// function to convert a color to its Red, Green, and Blue components.
void hueToRGB( int hue, int brightness)
{
unsigned int scaledHue = (hue * 6);
unsigned int segment = scaledHue / 256; // segment 0 to 5 around the
// color wheel
unsigned int segmentOffset =
scaledHue - (segment * 256); // position within the segment
unsigned int complement = 0;
unsigned int prev = (brightness * ( 255 - segmentOffset)) / 256;
unsigned int next = (brightness * segmentOffset) / 256;
if(invert)
{
brightness = 255-brightness;
complement = 255;
prev = 255-prev;
next = 255-next;
}
switch(segment ) {
case 0: // red
R = brightness;
G = next;
B = complement;
break;
case 1: // yellow
R = prev;
G = brightness;
B = complement;
break;
case 2: // green
R = complement;
G = brightness;
B = next;
break;
case 3: // cyan
R = complement;
G = prev;
B = brightness;
break;
case 4: // blue
R = next;
G = complement;
B = brightness;
break;
case 5: // magenta
default:
R = brightness;
G = complement;
B = prev;
break;
}
}
/*
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);
LEDChannels[0][0] = 0;
LEDChannels[0][1] = 1023;
LEDChannels[0][2] = 0;
LEDChannels[1][0] = 1023;
LEDChannels[1][1] = 0;
LEDChannels[1][2] = 0;
WriteLEDArray();
delay(200);
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment