简体   繁体   中英

Arduino/C++ cross-function variables

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM