variable name of variable? Pointers to functions?

I am sequentially setting a number of pins on the arduino, and need help to try and write some code. Is is possible to put a variable into the name of a variable, or something like it.

The algorithm I’m shooting for is something like:
setup:
Create variables pin1, pin2, pin3 etc, set them to output.
boolean condition; // a high or low.

Loop:
for (i=1,i<7;i++){
set condition based on some criteria
digitalWrite(pini, condition);
//where i is substituted with the loop count, setting
// pin1, pin2 etc.
}

So in other words, iterate through the variable names as well as the values that they take on? Is there some smart(er) way to do this?

Thanks!

You could use an array to store the pin numbers you’re interested in:

uint8_t happyPins[] = {
   3,
   5,
   13,
   14
};
#define NUM_HAPPY_PINS (sizeof(happyPins)/sizeof(happyPins[0])

loop()
{
uint8_t i;
for (i=0;i<NUM_HAPPY_PINS;i++){
   // set condition based on some criteria
   digitalWrite(happyPins[i], condition);
}

I wish I had thought of that. Thanks!

happyPins

;D

I suppose that's opposed to sadPins?

  • Brian

uint8_t happyPins[] = { 3, 5, 13, 14};

uint8_t is a little cryptic to non-programmers, why not use the Arduino documented byte type?

byte happyPins[] = { 3, 5, 13, 14};