1

I'm trying to create an Arduino library in which I need to declare and initialize three variables, (redpin_ledID, greenpin_ledID, bluepin_ledID) in one function (rgbInitiate) in such a way that another function (rgbMixer) can use them without the first function calling the second.

(Please note that this code is contained within an Arduino library.)

int rgbInitiate(int ledID, int redpin, int greenpin, int bluepin)
{   
    int redpin_ledID;
    int greenpin_ledID;
    int bluepin_ledID;
    redpin_ledID = redpin;
    greenpin_ledID = greenpin;
    bluepin_ledID = bluepin;
    pinMode(redpin_ledID, OUTPUT);
    pinMode(greenpin_ledID, OUTPUT);
    pinMode(bluepin_ledID, OUTPUT);
}

void rgbMixer(int ledID, int redvalue, int greenvalue, int bluevalue)
{   
    analogWrite(redpin_ledID, redvalue);
    analogWrite(greenpin_ledID, greenvalue);
    analogWrite(bluepin_ledID, bluevalue);
}
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
endrega
  • 25
  • 3
  • The scope of your int variables (redpin_ledID) are within the function rgbInitiate(). You most likely need to either declare them as global variables or pass them as arguments to rgbMixer(). – WebF0x Feb 08 '14 at 05:11
  • If there's no need for these to ever change then you should consider making the constant via `#define` instead. – Ignacio Vazquez-Abrams Feb 08 '14 at 15:39

2 Answers2

0

You are calling the first function from your main file which means those variables must exist in the main file. You don't actually change them in the rgbInitiate(), so sample code in your main file would looks like this:

int r = 2;
int g = 3;
int b = 4;
libname.rgbInitiate(r,g,b);

then you pass those variables to the next function from your main file libname.rgbMixer( r,g,b,ledID, redvalue, greenvalue, bluevalue)

Also, your first function (as it is written) can be simplified greatly

int rgbInitiate(int r, int g, int b)
{
    pinMode(r, OUTPUT);
    pinMode(g, OUTPUT);
    pinMode(b, OUTPUT);
}
Norbles
  • 217
  • 2
  • 8
  • That's not exactly what I had in mind, but I can see that I'll have to adjust my expectations being that I'm now fairly confident that the way I had originally intended for it to work is not a viable option. Thanks for the help. =) – endrega Feb 08 '14 at 19:12
  • You want to make a class (I actually didn't know Arduino classes are called libraries, so I looked into it a little more). Here's another answer: (http://stackoverflow.com/questions/1735990/using-classes-with-the-arduino) It seems there are no good ways to make proper classes. – Norbles Feb 09 '14 at 06:40
  • Classes are not Libraries – jdr5ca Feb 09 '14 at 08:59
  • Yes, but libraries are 'classes'--at least that is how it is presented on all the Arduino tutorials. (this is only in the scope of the Arduino IDE, not of all programming or all Arduino) – Norbles Feb 10 '14 at 05:53
  • A library is not a class. Many libraries _contain_ one class, which might mislead you to think they are the same. A library is a container. A library may contain any number of classes along with plain functions. – jdr5ca Feb 11 '14 at 03:47
  • Something people mistake is that the Arduino IDE changes things. The Arduino IDE **bundles** a standard compiler and tool chain and provides easy controls from a GUI. The compiler operates on standard C++. Arduino code is plain C++. What has been done is a good wrapping of the tool chain that allows people a great out of the box experience. – jdr5ca Feb 11 '14 at 03:57
0

You where close, you just need to move the pin variable definitions out of the function. That will make them have a permanent lifetime. Your library probably does not need any module other than the .CPP file they are defined in to use the variables 'redpin', etc. The addition of 'static' below means that these variable will not be visible to other code modules. That means if some application by chance uses a variable 'redpin' then it will not interfere with your code.

// somefile.cpp
static int redpin;
static int greenpin;
static int bluepin;

int rgbInitiate(int redpin, int greenpin, int bluepin) {   
  redpin = redpin;
  greenpin = greenpin;
  bluepin = bluepin;
  ...
}

void rgbMixer(int redvalue, int greenvalue, int bluevalue) {   
  analogWrite(redpin, redvalue);
  analogWrite(greenpin, greenvalue);
  analogWrite(bluepin, bluevalue);
}

You showed no use of ledID. Are you thinking there are more than one LED? C++ will not construct variable names on the fly. If you are going to have N LED's then the above would use an array.

static int redpin[4];
...

int rgbInitiate(int ledID, int redpin, int greenpin, int bluepin) {   
   redpin[ledID] = redpin;
   ...
}

void rgbMixer(int ledID, int redvalue, int greenvalue, int bluevalue) {   
  analogWrite(redpin[ledID], redvalue);
  ...
}
jdr5ca
  • 2,809
  • 14
  • 25
  • Thank you so much! I've been bashing my head in for the last month trying to figure that out. And after all that the answer was a few arrays. – endrega Feb 10 '14 at 17:42