I have been working on a cheerlights derivative that reads and ascii value of 48 - 58 (0 -9) and displays the colour accordingly, however it does not light up the diode at all. I tried to look at the serial monitor with the perl script off(so the serial port can be used by the ide instead) and it wont even show the Serial.println i made in the setup function. I hope to get this working and will post my final solution so others who dont want to shell out $$$ for the ethernet shield or GE io shield can have a fun christmas project too :P. Here is my code below:
/*
* CheerLights.pde
*
*/
//RGB LED pins
int ledAnalogOne[] = {3, 5, 6}; //the three pins of the first analog LED 3 = redPin, 5 = greenPin, 6 = bluePin
//These pins must be PWM
int ledAnalogTwo[] = {9, 10, 11}; //the three pins of the second analog LED 9 = redPin, 10 = greenPin, 11 = bluePin
//These pins must be PWM
// Variable Setup
long lastConnectionTime = 0;
String lastCommandString = "";
byte LastColour[] = {255, 255, 255};
const byte ORANGE[] = {83, 4, 0};
const byte YELLOW[] = {255, 255, 0};
const byte GREEN[] = {0, 255, 0};
const byte BLUE[] = {0, 0, 255};
const byte INDIGO[] = {4, 0, 19};
const byte PURPLE[] = {23, 0, 22};
const byte CYAN[] = {0, 255, 255};
const byte MAGENTA[] = {255, 0, 255};
const byte WHITE[] = {255, 255, 255};
const byte BLACK[] = {0, 0, 0};
const byte RED[] = {158, 4, 79};
//const byte PINK[] = {158, 4, 79};
//const byte* LastColour;
void setup() {
// sb = HughesyShiftBrite(10,11,12,13);
//sb.sendColour(0,0,0);
for(int i = 0; i < 3; i++){
pinMode(ledAnalogOne[i], OUTPUT);
//Set the three LED pins as outputs
pinMode(ledAnalogTwo[i], OUTPUT);
//Set the three LED pins as outputs
}
setColor(ledAnalogOne, BLACK);
//Turn off led 1
setColor(ledAnalogTwo, BLACK);
//Turn off led 2
// Serial.begin(9600);
// Setup Serial
Serial.begin(9600);
delay(100);
Serial.println('Startin Serial');
}
void loop() {
int input = Serial.read();
Serial.println('Last Colour was:');
Serial.println(lastCommandString);
switch (input) {
//case 48:
fadeToColor(ledAnalogOne, LastColour, RED, 10);
delay(3000);
for(int i = 0; i < 3; i++) {
LastColour[i] = RED[i];
}
lastCommandString = "red";
break;
case 49:
fadeToColor(ledAnalogOne, LastColour, GREEN, 10);
delay(3000);
for(int i = 0; i < 3; i++) {
LastColour[i] = GREEN[i];
}
lastCommandString = "green";
break;
case 50:
fadeToColor(ledAnalogOne, LastColour, BLUE, 10);
delay(3000);
for(int i = 0; i < 3; i++) {
LastColour[i] = BLUE[i];
}
lastCommandString = "blue";
break;
case 51:
fadeToColor(ledAnalogOne, LastColour, CYAN, 10);
delay(3000);
for(int i = 0; i < 3; i++) {
LastColour[i] = CYAN[i];
}
lastCommandString = "cyan";
break;
case 52:
fadeToColor(ledAnalogOne, LastColour, WHITE, 10);
delay(3000);
for(int i = 0; i < 3; i++) {
LastColour[i] = WHITE[i];
}
lastCommandString = "white";
break;
case 53:
//warmwhite
fadeToColor(ledAnalogOne, LastColour, INDIGO, 10);
delay(3000);
for(int i = 0; i < 3; i++) {
LastColour[i] = INDIGO[i];
}
lastCommandString = "indigo";
break;
case 54:
fadeToColor(ledAnalogOne, LastColour, PURPLE, 10);
delay(3000);
for(int i = 0; i < 3; i++) {
LastColour[i] = PURPLE[i];
}
lastCommandString = "purple";
break;
case 55:
fadeToColor(ledAnalogOne, LastColour, MAGENTA, 10);
delay(3000);
for(int i = 0; i < 3; i++) {
LastColour[i] = MAGENTA[i];
}
lastCommandString = "magenta";
break;
case 56:
fadeToColor(ledAnalogOne, LastColour, YELLOW, 10);
delay(3000);
for(int i = 0; i < 3; i++) {
LastColour[i] = YELLOW[i];
}
lastCommandString = "yellow";
break;
case 57:
fadeToColor(ledAnalogOne, LastColour, ORANGE, 10);
delay(3000);
for(int i = 0; i < 3; i++) {
LastColour[i] = ORANGE[i];
}
lastCommandString = "orange";
break;
}
}
/* Sets the color of the LED to any RGB Value led - (int array of three values defining the LEDs pins (led[0] = redPin, led[1] = greenPin, led[2] = bluePin)) color - (byte array of three values defing an RGB color to display (color[0] = new Red value, color[1] = new Green value, color[2] = new Red value*/
void setNumColor(int* led, byte* color){
for(int i = 0; i < 3; i++){ //iterate through each of the three pins (red green blue)
analogWrite(led[i], 255 - color[i]); //set the analog output value of each pin to the input value (ie led[0] (red pin) to 255- color[0] (red input color)
//we use 255 - the value because our RGB LED is common anode, this means a color is full on when we output analogWrite(pin, 0)
//and off when we output analogWrite(pin, 255).
}
}
/* A version of setColor that takes a predefined color (neccesary to allow const int pre-defined colors */
void setColor(int* led, const byte* color) {
byte tempByte[] = {color[0], color[1], color[2]};
setColor(led, tempByte);
Serial.println('Colour set');
}
/* Fades the LED from a start color to an end color at fadeSpeed
led - (int array of three values defining the LEDs pins (led[0] = redPin, led[1] = greenPin, led[2] = bluePin))
startColor - (byte array of three values defing the start RGB color (startColor[0] = start Red value, startColor[1] = start Green value, startColor[2] = start Red value
endColor - (byte array of three values defing the finished RGB color (endColor[0] = end Red value, endColor[1] = end Green value, endColor[2] = end Red value
fadeSpeed - this is the delay in milliseconds between steps, defines the speed of the fade*/
void fadeToNumColor(int* led,const byte* startColor,const byte* endColor, int fadeSpeed){
int changeRed = endColor[0] - startColor[0];
//the difference in the two colors for the red channel
int changeGreen = endColor[1] - startColor[1];
//the difference in the two colors for the green channel
int changeBlue = endColor[2] - startColor[2];
//the difference in the two colors for the blue channel
int steps = max(abs(changeRed),max(abs(changeGreen), abs(changeBlue)));
//make the number of change steps the maximum channel change
for(int i = 0 ; i < steps; i++){
//iterate for the channel with the maximum change
byte newRed = startColor[0] + (i * changeRed / steps);
//the newRed intensity dependant on the start intensity and the change determined above
byte newGreen = startColor[1] + (i * changeGreen / steps);
//the newGreen intensity
byte newBlue = startColor[2] + (i * changeBlue / steps);
//the newBlue intensity
byte newColor[] = {newRed, newGreen, newBlue};
//Define an RGB color array for the new color
setColor(led, newColor);
//Set the LED to the calculated value
delay(fadeSpeed);
//Delay fadeSpeed milliseconds before going on to the next color
}
setColor(led, endColor);
//The LED should be at the endColor but set to endColor to avoid rounding errors
}
/* A version of fadeToColor that takes predefined colors (neccesary to allow const int pre-defined colors */
void fadeToColor(int* led, const byte* startColor, const byte* endColor, int fadeSpeed) {
Serial.println('Changing colour');
byte tempByte1[] = {startColor[0], startColor[1], startColor[2]};
byte tempByte2[] = {endColor[0], endColor[1], endColor[2]};
fadeToNumColor(led, tempByte1, tempByte2, fadeSpeed);
}