How do I write this rgb function

Hello all, I am trying to write program that controls two RGB outputs (6 wires). Be that as it may, I do not have enough a/d pins to support both LEDs. My friend has been helping me with the code so far but it will not compile in arduino. My goal is to have two function calls that look like this: RGB(redvalue,greenvalue,bluevalue). I do not necessarily need to pwm these values (though I would like to learn). The function can be like this: RBG(HIGH,LOW,HIGH)... Here's what I have so far:

/*****************************************
*       Initialize Pins & variables      *
*****************************************/

struct RGB{
    byte red;
    byte green;
    byte blue;
  };

void initPins(RGB &temp);  
void setPiston(RGB &temp, byte s1, byte s2, byte s3);
RGB pistonOne = {2, 3, 4};
RGB pistonTwo = {5, 6, 7};



/*****************************************
*          Setup                         *
*****************************************/

void setup()
{
  initPins(pistonOne);
  initPins(pistonTwo);
}

  

/*****************************************
*          Main Loop                     *
*****************************************/

void loop()
{  
  setPiston(pistonOne, HIGH, HIGH, HIGH);
}

/******************************************
*         Funtion Definitions             *
******************************************/
void initPins(RGB &temp)
{
  pinMode(temp.red, OUTPUT);
  pinMode(temp.green, OUTPUT);
  pinMode(temp.blue, OUTPUT);
}

void setPiston(RGB &temp, byte s1, byte s2, byte s3)
{
  digitalWrite(temp.red, s1);
  digitalWrite(temp.green, s2);
  digitalWrite(temp.blue, s3);
}

Here are my errors: HD_HowItWorks_1_GG:11: error: variable or field 'initPins' declared void HD_HowItWorks_1_GG:11: error: 'RGB' was not declared in this scope HD_HowItWorks_1_GG:11: error: 'temp' was not declared in this scope HD_HowItWorks_1_GG:12: error: variable or field 'setPiston' declared void HD_HowItWorks_1_GG:12: error: 'RGB' was not declared in this scope HD_HowItWorks_1_GG:12: error: 'temp' was not declared in this scope HD_HowItWorks_1_GG:12: error: expected primary-expression before 's1' HD_HowItWorks_1_GG:12: error: expected primary-expression before 's2' HD_HowItWorks_1_GG:12: error: expected primary-expression before 's3'

This compiles, but is untested...

/*****************************************
*       Initialize Pins & variables      *
*****************************************/

typedef struct RGB
{
  byte red;
  byte green;
  byte blue;
};

RGB pistonOne = {2, 3, 4};
RGB pistonTwo = {5, 6, 7};

/*****************************************
*          Setup                         *
*****************************************/

void setup()
{
  initPins(pistonOne);
  initPins(pistonTwo);
}

/*****************************************
*          Main Loop                     *
*****************************************/

void loop()
{  
  setPiston(pistonOne, HIGH, HIGH, HIGH);
}

/******************************************
*         Function Definitions            *
*******************************************/

void initPins(struct RGB temp)
{
  pinMode(temp.red, OUTPUT);
  pinMode(temp.green, OUTPUT);
  pinMode(temp.blue, OUTPUT);
}

void setPiston(struct RGB temp, byte s1, byte s2, byte s3)
{
  digitalWrite(temp.red, s1);
  digitalWrite(temp.green, s2);
  digitalWrite(temp.blue, s3);
}

Hello,
the problem is that you cannot define classes or struct inside the sketch, because of the way Arduino IDE pre-processes your file before compilation. You can create a new file rgb.h in the same directory of your arduino sketch containing the struct definition only:

#include <arduino.h>

struct RGB
{
    byte red;
    byte green;
    byte blue;
  };

add it to the project (a new tab will appear in the IDE), then include it in your sketch:

#include "rgb.h"

void initPins(RGB &temp);  
void setPiston(RGB &temp, byte s1, byte s2, byte s3);
RGB pistonOne = {2, 3, 4};
RGB pistonTwo = {5, 6, 7};

...

OP: In your code, RGB is not a type. The type is struct RGB.

dsw00d's approach works, but I prefer to separate the struct definition from the typedef statement.

struct rgb
{
   byte red, green, blue;
};

typedef struct rgb RGB;

Great It works! I noticed that I did not have to do the ampersand thing and I had to use typedef. Also, I did not specify "struct" in my function definitions. I will make not of this next time. Thanks for your help dxw00d !! :D

Ea123,

My friend was confused as to why it wouldn't compile. Now everything makes sense! My goal in the end is to write cleaner code so I will try your method in the future. thanks!

Now I am confused: I've tried to compile the code posted by dxw00d and it does not work on Arduino 1.0 environment. Moreover in C++ language the following statement:

struct RGB { byte r, g, b; }

defines a new type named RGB, so you can declare variables ( RGB pistonOne; ) or use it in function parameters. The typedef version instead is correct in C language.

It compiles fine in 1.0 for me.

It compiles fine in 1.0 for me.

Sorry, i did't notice that you removed the two function prototypes! By removing them it compiles also without the typedef in struct RGB. In fact I think that the IDE adds automatically the functions prototypes before the beginning of the sketch.