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
16k31 gold badges73 silver badges89 bronze badges
asked Feb 8, 2014 at 5:00
2
  • 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(). Commented Feb 8, 2014 at 5:11
  • If there's no need for these to ever change then you should consider making the constant via #define instead. Commented Feb 8, 2014 at 15:39

2 Answers 2

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);
 ...
}
answered Feb 9, 2014 at 9:14

1 Comment

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.
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);
}
answered Feb 8, 2014 at 15:33

6 Comments

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. =)
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: (stackoverflow.com/questions/1735990/…) It seems there are no good ways to make proper classes.
Classes are not Libraries
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)
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.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.