Help please - making a function work with variables

Hello! I'm trying to use the code below to fade from one color to another. I've managed to modify it to work with a common anode RGB that I have in my parts bin. I posted this over in the Adafruit forums too.

But I can't seem to create my own function. The function "do_fader" is what I'm trying to get to work.

If I try to pass "red, blue" for example, I can't use the variables to directly call f.fade( from, to). The IDE I'm using (Visual Micro for Visual Studio) helpfully hints that "no suitable constructor exists to convert from "char" to "rgb_color". "rgb_color" is a class which is constructed in the code.

I'm not a programmer by any means.

Any help is appreciated....

    /*
     * 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);
          }
        }

    };

    void setup()
    {
    }
    // color names for use when calling do_fader
    char red, magenta, pink, purple, green, yellow, blue, cyan, black, white;

    void loop()
    {
       fader_examples();
    do_fader(red, purple);
    delay(500);
    do_fader(purple, red);
    }

    // fade from one color to another
    void do_fader( char from, char to)
    {
      fader f (11, 10, 9); //R G B pins for fading
       /*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 );
     
     f.fade( from, to)

     // String doFade =  "f.fade( from, to)";
     //  doFade;
     }

    void fader_examples()
    {
      fader f (11, 10, 9); //R G B pins for fading
      /*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);
     
      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);
    }

You have a variable called purple:

    // color names for use when calling do_fader
    char red, magenta, pink, purple, green, yellow, blue, cyan, black, white;

and another variable called purple:

      rgb_color purple   (  75, 255,     0 );

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.

     f.fade( from, to)

The fade() method:

        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
        {

does not take a car. So, why are you passing it one?

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);
}

Since rgb_color provides no real functionality, a class is not the best choice. A struct is a better choice. There is no reason why r, g, and b (the my_ prefix is useless) can't be public members. That way, no getters are needed.

You could make the rgb_color instances const to prevent changing red from (255, 0, 0) to (0, 255, 0).