PaulS:
You have a variable called purple:
....and another variable called purple:
....That is a REALLY bad idea. The first variable called purple is never assigned a value, so it is pointless to pass it to a function.
Thank you! Now after you have got me to thinking about the declaration of the variables, I realize that I had tried to declare the fader "f" and the colors in the wrong places when I was experimenting. I tried to declare them in setup() and also above the code that creates the classes. I moved them to just above the setup() and beneath the code creating the classes and I now can call the fader function in the main loop or other functions.
So now this is working:
/*
* LedBrightness sketch
* controls the brightness of LEDs on "analog" (PWM) output ports.
* http://stackoverflow.com/questions/15803986/fading-arduino-rgb-led-from-one-color-to-the-other
*/
class rgb_color {
private:
int my_r;
int my_g;
int my_b;
public:
rgb_color (int red, int green, int blue)
:
my_r(red),
my_g(green),
my_b(blue)
{
}
int r() const {return my_r;}
int b() const {return my_b;}
int g() const {return my_g;}
};
/*instances of fader can fade between two colors*/
class fader {
private:
int r_pin;
int g_pin;
int b_pin;
public:
/* construct the fader for the pins to manipulate.
* make sure these are pins that support Pulse
* width modulation (PWM), these are the digital pins
* denoted with a tilde(~) common are ~3, ~5, ~6, ~9, ~10
* and ~11 but check this on your type of arduino.
*/
fader( int red_pin, int green_pin, int blue_pin)
:
r_pin(red_pin),
g_pin(green_pin),
b_pin(blue_pin)
{
}
/*fade from rgb_in to rgb_out*/
void fade( const rgb_color& in,
const rgb_color& out,
unsigned n_steps = 256, //default take 256 steps
unsigned time = 10) //wait 10 ms per step
{
int red_diff = out.r() - in.r();
int green_diff = out.g() - in.g();
int blue_diff = out.b() - in.b();
for ( unsigned i = 0; i < n_steps; ++i){
/* output is the color that is actually written to the pins
* and output nicely fades from in to out.
*/
rgb_color output ( in.r() + i * red_diff / n_steps,
in.g() + i * green_diff / n_steps,
in.b() + i * blue_diff/ n_steps);
/*put the analog pins to the proper output.*/
analogWrite( r_pin, output.r() );
analogWrite( g_pin, output.g() );
analogWrite( b_pin, output.b() );
delay(time);
}
}
};
//R G B pins for fading
fader f (11, 10, 9);
// color names for use when calling do_fader
/*colors for common cathode RGB LED*/
/*rgb_color yellow( 250, 105, 0 );
rgb_color orange( 250, 40, 0 );
rgb_color red ( 255, 0, 0 );
rgb_color blue ( 10, 10, 255 );
rgb_color pink ( 255, 0, 100 );
rgb_color purple( 200, 0, 255 );
rgb_color green ( 0, 255, 0 );
rgb_color white ( 255, 255, 255 );*/
/*colors for common anode RGB LED*/
rgb_color red ( 0, 255, 255 );
rgb_color magenta ( 0, 255, 0 );
rgb_color pink ( 0, 255, 100 );
rgb_color purple ( 75, 255, 0 );
rgb_color green ( 255, 0, 255 );
rgb_color yellow ( 0, 50, 255 );
rgb_color blue ( 255, 255, 0 );
rgb_color cyan ( 255, 0, 0 );
rgb_color white ( 0, 0, 0 );
rgb_color black ( 255, 255, 255 );
// one of the LEDs has too bright a green/too dim a red, so yellow has to be fine tuned
// rgb_color yellow(0, 0, 255);
void setup()
{
}
void loop()
{
int wait = 1000;
f.fade( black, red);
delay(wait);
f.fade( red, blue);
delay(wait);
f.fade( blue, green);
delay(wait);
f.fade( green, black);
delay(wait);
// fader_examples();
}
void fader_examples()
{
int wait = 1000;
// examples
f.fade( black, red);
delay(wait);
f.fade( red, magenta);
delay(wait);
f.fade( magenta, pink);
delay(wait);
f.fade( magenta, green);
delay(wait);
f.fade( green, yellow);
delay(wait);
f.fade( yellow, blue);
delay(wait);
f.fade( blue, cyan);
delay(wait);
f.fade( cyan, white);
delay(wait);
f.fade( white, black);
delay(wait * 2);
}