HI I am trying from Arduino CookBook 2nd edtion. Section 7.4 Adjusting the color of an LED.
[code]
/*
RGB_LEDs sketch
RGB LEDs driven from analog output ports
*/
const int redPin = 3; // choose the pin for each of the LEDs
const int greenPin = 5;
const int bluePin = 6;
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() {
// pins driven by analogWrite do not need to be declared as outputs
}
void loop() {
int brightness = 255; // 255 is maximum brightness
hueToRGB( color, brightness); // call functiion to convert hue to RGB
// write the RGB values to the pins
analogWrite(redPin, R);
analogWrite{greenPin, G);
analogWrite(bluePin, B);
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 scaleHue = (hue * 6);
// segment 0 to 5 around the color wheel
unsigned int segment = scaleHue / 256:
// position within segmentOffset = scaledHue - (segment * 256);
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 = complenment;
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 4: // magenta
R = brightness;
G = complement;
B = prev;
break;
}
}
[/code]
The code will not complied. It errors out at
Something wrong here:
analogWrite{greenPin, G);
And here:
Where is 'segmentOffset' defined?
And here:
unsigned int segment = scaleHue / 256:
And here:
B = complenment;
And here:
case 4: // blue
case 4: // magenta
Also learn to use CTRL T to format your sketches.
.
Hi LarryD I did format with control T and I used the Auto Format under the Tools. If you use the wrong punctuation it will not format correctly. With some corrections. Here is a rewrite of the code. Still have problem with defining segmentOffset
[code]
/*
RGB_LEDs sketch
RGB LEDs driven from analog output ports
*/
const int redPin = 3; // choose the pinfor each of the LEDs
const int greenPin = 5;
const int bluePin = 6;
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() {
// pins driven by analogWrite do not need to be declared as outputs
}
void loop() {
int brightness = 255; // 255 is maximum brightness
hueToRGB( color, brightness); // call functiion to convert hue to RGB
// write the RGB values to the pins
analogWrite(redPin, R);
analogWrite(greenPin, G);
analogWrite(bluePin, B);
color++; // increment the color
if (color > 255) //
color = 0;
delay(10);
}
// function to convert a olor to its Red, Green, and Blue components.
void hueToRGB(int hue, int brightness)
{
unsigned int scaleHue = (hue * 6);
// segment 0 to 5 around the color wheel
unsigned int segment = scaleHue / 256;
// position within the segment
unsigned int segmentOffset = scaledHue - (segment * 256);
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
R = brightness;
G = complement;
B = prev;
break;
}
[/code]
Make this a global variable:
unsigned int segmentOffset;
Then change this:
unsigned int segmentOffset = scaledHue - (segment * 256);
to this:
segmentOffset = scaledHue - (segment * 256);
.
I made the changes as you suggested. But I still have a problem.
[code]
/*
RGB_LEDs sketch
RGB LEDs driven from analog output ports
*/
const int redPin = 3; // choose the pinfor each of the LEDs
const int greenPin = 5;
const int bluePin = 6;
const boolean invert = true; // set true if common anode, false if common cathode
unsigned int segmentOffset;
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() {
// pins driven by analogWrite do not need to be declared as outputs
}
void loop() {
int brightness = 255; // 255 is maximum brightness
hueToRGB( color, brightness); // call functiion to convert hue to RGB
// write the RGB values to the pins
analogWrite(redPin, R);
analogWrite(greenPin, G);
analogWrite(bluePin, B);
color++; // increment the color
if (color > 255) //
color = 0;
delay(10);
}
// function to convert a olor to its Red, Green, and Blue components.
void hueToRGB(int hue, int brightness)
{
unsigned int scaleHue = (hue * 6);
// segment 0 to 5 around the color wheel
unsigned int segment = scaleHue / 256;
// position within the segment
segmentOffset = scaledHue - (segment * 256);
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
R = brightness;
G = complement;
B = prev;
break;
}
[/code]
OK that works. Now a new question how do I get nine(9) RGB LEDs to work off one board with independent colors? I have Uno and Maga.
Look at shift register examples on how to increase pin counts.
Also
Maybe look into charlieplexing, you might have luck there.
.