2

I am working on arduino IDE and esp8266 nodemcu. As i am going through the IDE I saw that there is a structure of void loop() and void setup() and all examples in the internet included defining the pinModes within the void setup() block. I want to make a program where through mqtt or http the user will give input as to what type of device he/she wants to attach to the esp8266, so based on that i will have to set, INPUT/OUTPUT state on the following pins.

Now my initial idea was :

void setup()
{
    connectwifi();
}

void loop()
{
   call_some_function();
}

where call_some_function() will recieve the requirements from user over http. For example he wants a light on pin3, so pinMode(pin3,OUTPUT);

now what I wanted is that, call the method - call_some_function() whenever the user needs to add an extra device to it, which means initally when we wanted to add 4 lights to esp8266, we will write

pinMode(pin1,OUTPUT);
pinMode(pin2,OUTPUT);
pinMode(pin3,OUTPUT);
pinMode(pin4,OUTPUT);

But now when I have a user defined input, my code will become something like this,

call_some_function()
{
     pin=(user gave me the pin no);
     state=(based on the device the server will send the state i.e IN/OUT);
     pinMode(pin,state);
}

Now my question is , calling this function 4 times and writing pinMode() function 4 times will result in same thing or separate? Also is this doable, like can i write like this? I will be calling this function from void loop() instead if void setup(), since I continuously listen for user input, Cause I cannot find any other way of making user specific input

2
  • you can call pinMode anytime
    – Juraj
    Jul 12, 2018 at 13:59
  • Can u make ur comment as "answer ur question", cuz if furthur discussion turns out to be an answer then i can mark it as an Answer.
    – Wan Street
    Jul 12, 2018 at 14:05

2 Answers 2

2

Now my question is , calling this function 4 times and writing pinMode() function 4 times will result in same thing or separate? Also is this doable, like can i write like this?

you can call pinMode anytime

And to add something more to pinMode. It is not just some preparation of the pin. It changes things for the connected circuit. Setting to INPUT lets the attached circuit rule. If there is a pull-up circuit the state will be HIGH, if pull-down LOW. INPUT_PULLUP holds the state HIGH but lets the attached circuit to pull it down. pinMode OUTPUT sets the pin LOW.

In two cases for now I use setting a pin to OUTPUT only for the time I want to control the attached circuit and the I set it back to INPUT. In first case I reset an esp8266 with EN pin connected by setting the pin LOW, then I let the external pull-up hold the EN pin HIGH. Second case is amplifier module for the tone() function. I set the pin to INPUT after tone() otherwise it catches noise.

0
2

pinMode() is usually called from setup() because in most cases, the pin mode remains the same throughout the run. Sometimes there is a reason to change it during the run, such as in your example, and you can call pinMode() at whenever is the appropriate time to do that.

1
  • Ok, ty.. Juaraj gave the correct answer first so I will mark his answer.
    – Wan Street
    Jul 12, 2018 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.