Hi everyone!
First post - I'm a complete noob at this. I have a decent knowledge of electronics, but no Arduino or programming experience. I want to learn! I would greatly appreciate your help with my first project. I have a chain of 8 ShiftBars with Satellite LED modules, connected to an Arduino Uno via the ShiftBrite shield. I would like to:
a) be able to control the brightness of all LEDs at once with a potentiometer
b) be able to vary the RGB colors of all LEDs at once with a second potentiometer
c) be able to turn the entire array on or off with a momentary switch button.
I've tested the chain using this code posted on the Macetech site:
#define clockpin 13 // CI
#define enablepin 10 // EI
#define latchpin 9 // LI
#define datapin 11 // DI
#define NumLEDs 3
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[0];
SB_GreenCommand = LEDChannels[1];
SB_BlueCommand = LEDChannels[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);
}
Everything checks out fine; I also did an experiment with a single RGB LED, without the ShiftBars, to see if I could write the code for the two potentiometers and the button:
// Will fade an RGB LED from warm to cool
// in relation to the pot value
// LED leads connected to PWM pins
const int RED_LED_PIN = 9; //Red LED pin
const int GREEN_LED_PIN = 10; //Green LED pin
const int BLUE_LED_PIN = 11; //Blue LED pin
const int Button_Pin = 2; // Pushbutton pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the pushbutton pin as an input:
pinMode(Button_Pin, INPUT);
}
void loop() {
int color = analogRead(0);
//read the color temperature pot
//Range is 2000K to 5500K
//RGB 255,137,18 to 255,236,224
int brightness = analogRead(1);
//read the brightness pot
//Range is off to full on
int redValue = constrain(
map(color, 0, 1023, 0,255),255,255)*map(brightness, 0, 1023, 0, 255)/255;
//calculate the red Value (255-0
//over the range 0-1023)
int greenValue = constrain(
map(color, 0, 1023, 0, 255),137,236)*map(brightness, 0, 1023, 0, 255)/255;
//calculate the green value (0-255
//over 0-1023)
int blueValue = constrain(
map(color, 0, 1023, 0, 255),18,224)*map(brightness, 0, 1023, 0, 255)/255;
//calculate the blue value 0-255 over
//0-1023
// read the state of the pushbutton value
buttonState = digitalRead(Button_Pin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// Display the requested color
analogWrite(RED_LED_PIN, redValue);
analogWrite(GREEN_LED_PIN, greenValue);
analogWrite(BLUE_LED_PIN, blueValue);
}
else {
// turn LEDs off:
analogWrite(RED_LED_PIN, 0);
analogWrite(GREEN_LED_PIN, 0);
analogWrite(BLUE_LED_PIN, 0);
}
}
I did not figure out how to get the momentary button to toggle, so I could use some help there, please! Otherwise, it seems to work fine.
The million-dollar question is: how do I combine these two sketches to allow me to control the ShiftBrite chain like I did with the single RGB LED? The two approaches seem very different. Is there a better way? I'm stuck here! Thanks a bunch for your help!