Use of strings for pinMode argument

Hi,

I am a new user of Arduino boards, so far going thru the basic examples proposed on the site.

I am facing the following issue :

I have successfully run the switchCase2 example on an Arduino Uno board and I am now trying to get it to run on a ESP8266 based board.

This is the part for which I got a problem :

void setup() {

  • // initialize serial communication:*
  • Serial.begin(9600);*
  • // initialize the LED pins:*
  • for (int thisPin = 2; thisPin < 7; thisPin++) {*
  • pinMode(thisPin, OUTPUT);*
  • }*
    }

Could somebody explain me how to change the 'For loop' used in the void setup, knowing that on ESP8266 the pins are named D1, D2,D3, etc... . In other words, how do I construct a variable to place as argument 1 in pinMode function ?
So far with the different solutions I have tried, I got this error :

cannot convert 'String' to 'uint8_t {aka unsigned char}' for argument '1' to 'void pinMode(uint8_t, uint8_t)'

Thks for yr help

Philippe

The only safe way that I can think of is to create an array of them and then index it:
(untested)

byte ESPdatapins[] = {D2, D3, D4, D5, D6, D7};
...
  for (byte thisPin = 0; thisPin < sizeof(ESPdatapins[]); thisPin++) {
    pinMode(ESPdatapins[thisPin], OUTPUT);
  }

The names D2...D7 are not strings, they are preprocessor macro identifiers. They disappear in the process of compilation.

Delta_G:
What you have to understand is that D1, D2 etc are symbols standing for numbers. For each there is a number you could use. This is perfectly valid.

for(int pin = D1; pin < D3; pin++){

That assumes that they are consecutive.

Thanks very much for yr quick and clear answers, so I understand why trying to play with strings was not going in the right direction.

In the swithCase2 example, the pin are consecutive, but undertand the array would be a solution if that was not the case.

Cheers

Philippe :slight_smile:

pst51:
In the swithCase2 example, the pin are consecutive

How do you know that, exactly? You can't tell if the values are consecutive by just looking at the names.