How to create n variables named "statepinx" with a for loop ?

Hi

I want to use GPIOs 0 to 16 as OUTPUT except GPIO number 6, 7, 8, 9 and 11.
So I use the following piece of code.

for (int i = 0; i <= 16; i++) {
    if (i != 6 && i != 7 && i != 8 && i != 9 && i != 11) {
      pinMode(i, OUTPUT);
    }
}

Then, I want to record the state of each of these GPIOs in a varaible called "statepinx" with x the number of each spcific GPIO.

for (int i = 0; i <= 16; i++) {
      if (i != 6 && i != 7 && i != 8 && i != 9 && i != 11) {
        char buf[30];
        sprintf(buf, "statepin%d", i); //at first iteration, buf = statepin0
        static bool *buf = 0; //  so I want to create the boolean variable "statepin0" but this doesn't work as the compiler thinks I want to create a new pointer !
        if ("newly_statepin_created" != digitalRead(i)) {
          "newly_statepin_created" = digitalRead(i);
          char buff[30];
          sprintf(buff, "%s has just changed to %d", newly_statepin_created, *newly_statepin_created);

}
}
}

I hope my question isn"t too confused.

You would be better served by using pin arrays:

byte pins[] = {6, 7, 8, 9, 11};
....
for (int i = 0; i < sizeof(pins)/sizeof(pins[0]); i++) {
       pinMode(pins[i], OUTPUT);
}

This:

static bool *buf = 0;

CANNOT work. Variable names do not exist at run-time - they are converted to memory addresses by the compiler/linker. What you want is an array of booleans.

Regards,
Ray L.

aarg:
You would be better served by using pin arrays:

byte pins[] = {6, 7, 8, 9, 11};

....
for (int i = 0; i < sizeof(pins)/sizeof(pins[0]); i++) {
      pinMode(pins[i], OUTPUT);
}

I don't know if my english is so bad, but the pin 6, 7, 8, 9, 11 are the pin I don't care.( I don't want to configure then as OUTPUT )
The pin number I want to use as OUTPUT are 0, 1, 2, 3, 4, 5, 10, 12, 13, 14, 15, 16.

Plus, I'm not sure your piece of code wouldn't also configure pin N°10 with the pins N° 6, 7, 8, 9, and 11 you are configuring.

RayLivingston:
This:

static bool *buf = 0;

CANNOT work. Variable names do not exist at run-time - they are converted to memory addresses by the compiler/linker. What you want is an array of booleans.

Regards,
Ray L.

Yes, you're right, the compiler is complaining ! :slight_smile:

But, is what I want to do understandable / well explained ? Then is i tpossible to do, or is there a better way to do such things ?

edit : I'm just linking the 2 answers and making something with it : I might catch something. I'll try to post the piece of code this evening. Thks for the direction /help.

0 and 1 can be used and should be used for Serial.println. I recommend to leave 0, 1 for debug information. If you need more GPIOs, you can use the "analog" pins also.

const byte pins[] = {2, 3, 4, 5, 10, 12, 13, A0, A1, A2, A3, A4, A5};

for (auto &i : pins) {
       pinMode(i, OUTPUT);
}

Only use 0 and 1 as outputs/inputs if you are very confident about your programming skills and you know already, that you will never ask a question in a forum for that sketch.

Pin assignments don't change usually, so you should make them const.

Third thing I want to point out is the Range based for loop - it makes iteration over arrays a lot easier.

do not use similar name for variable as buf an buff

Maybe something like this :


bool statepin[16];
for (int i = 0 ; i < 17 ; i++)
{
if (i > 5 && i < 10 && i != 11)
{
pinMode(i, OUTPUT);
if (statepin [ i ] != digitalRead(i))
{
char buff[30];
sprintf(buff , "statepin%d has just changed from %d to %d" , i , statepin [ i ] , digitalRead(i));
statepin [ i ] = digitalRead(i);
}
}
}
// remove space between [ i ]

I'm trying to make something with all the incoming info.

But could you explain the following piece of code please.
I'm working on ESP8266 Lolin V3 board, so advices about pin number are not relevant for me.

noiasca:
0 and 1 can be used and should be used for Serial.println. I recommend to leave 0, 1 for debug information. If you need more GPIOs, you can use the "analog" pins also.

const byte pins[] = {2, 3, 4, 5, 10, 12, 13, A0, A1, A2, A3, A4, A5};

for (auto &i : pins) {
      pinMode(i, OUTPUT);
}




Only use 0 and 1 as outputs/inputs if you are very confident about your programming skills and you know already, that you will never ask a question in a forum for that sketch.

Pin assignments don't change usually, so you should make them const.

Third thing I want to point out is the [Range based for loop](https://www.google.com/search?q=range+based+for) - it makes iteration over arrays a lot easier.

I'm using that piece of code and it seems to work pretty well :

const byte pins[] = {0, 1, 2, 3, 4, 5, 10, 12, 13, 14, 15, 16};

void setup_GPIO() {
  //const byte pins[] = {0, 1, 2, 3, 4, 5, 10, 12, 13, 14, 15, 16};
  for (auto &i : pins) {
    pinMode(i, OUTPUT);
  }
}

void check_pin_state(double now) {
  if (now - last > 100) {
    static bool statepin[17];
    for (auto &i : pins) {
      if (statepin [ i ] != digitalRead(i)) {
        char buff[50];
        sprintf(buff , "statepin%d has just changed from %d to %d" , i ,  statepin [ i ] , digitalRead(i));
        Serial.println(buff);
        client.publish("esp/ouest", buff);     //using MQTT broker for publishing the info 
        statepin [ i ] = digitalRead(i);
      }
    }
    last = now;
}
  }

One of my question is :
Why do I need to create a 17 statepin value array :

static bool statepin[17];

when I have only 12 pin value in my array, otherwise it won't work properly !

I'm working on ESP8266 Lolin V3 board, so advices about pin number are not relevant for me.

have you told before that you are using not an Arduino Uno? Where?

Why do I need to create a 17 statepin value array :

because in that range based for i will get up to 17. If you want to do something with the index of the array, use a for i < sizeof...