Digital write loop using combined string as pin number


I have program which the output pin on hardware is not arranged in order. So I’m thinking is it possible to define
the unorganized pins, to reflect my nozzle numberings output. And run a loop to declare it as a and output pin? Rather that declaring it one by one, manually.

pinMode(13, OUTPUT);
pinMode(8, OUTPUT);
pinMode(12, OUTPUT); and so on.

Here’s my code :

// as output pin that we want to control is not in order, we define back to sort the number start with word ‘SOL’ follow by running number of solenoids that we have installed.

#define SOL1 13; // give an example I want to define ‘SOL1’ as pin led output 13
#define SOL2 8;
#define SOL3 12;
#define SOL4 3;
#define SOL5 4;
#define SOL6 7;

void setup (){
Serial.begin(9600); //initializing the serial

for(int i=0; i < 5 ; i++) { // loop for 5 times
String outp = “SOL” + String(i); // combine SOL and i, so it will loop and form SOL1,SOL2,SOL3
and so on, and point back to reference pin
Serial.println (outp); //try to debug and see the result at serial.
pinMode(outp, OUTPUT); // set output pin. I’m stuck at this point. Purpose is, we want to declare
//pin 13,8,12,3,4 and 7 as output by only calling the define.


void loop(){
// something goes here

When I compile, I get an error. “cannot convert String to uint8_t”.
I know that we can’t directly assign string at the output pin, so I’ve tried to convert the string to char array by using :

char charBuf[7];
FINALSOL.toCharArray(charBuf, 7);

But still no success.

What you need is a lookup table in an array. It'll be much quicker than diddling with Strings.

double posting. - removed

If you want to do show text on a display, you can use strings. But not in this situation.

// define the 6 pins. solPins[0] is pin 13 and so on.
int solPins[] = { 13, 8, 12, 3, 4, 7 };


  for( int i=0; i<6; i++)
    pinMode( solPins[i], OUTPUT);
int STSOL[16] = {15,14,21,20,19,18,17,16,50,52,38,40,42,44,46,38};

  for (int xy=0; xy<=15; xy++) {
  pinMode(STSOL[xy], INPUT);  // set input pin
  for (int xu=0; xu<=15; xu++) {
  pinMode(SOLA[xu], OUTPUT);  // set as output pin
  pinMode(SOLB[xu], OUTPUT);  // set as output pin

Thank you.

You can replace xy and xu with i. It makes no difference to the compiler, but as is, it will surprise people a little reading your code.

Using i as a loop counter is only a convention but if you don't it tends to make people look for a reason why. If it were x or y or u, it wouldn't matter so much, but xy suggests that the name has some extra meaning and as far as I can see from your snippet, it doesn't.