Is it legal to use pinMode() inside void loop()?

Example of what I'm asking:

void setup()
{
    pinMode(5,OUTPUT);
    pinMode(7,INPUT);
}

void loop()
{
     // some code here

    pinMode(5,INPUT);
    pinMode(7,OUTPUT);

    // some code here
}

I am trying to create an IC tester (00, 02, 04, 08, 32, 86, and 266) using Arduino Uno. The algorithm goes something like this:

  1. Apply HIGH and LOW to pins 14 and 7 of the IC in question
  2. Read the logic level of the other 12 pins (corresponding Arduino pins are INPUT_PULLUP to avoid floating)
  3. From the response, determine the gate layout
  4. Apply 00, 01, 10, and 11 to the input pins of the IC to test it and determine its designation

From step 2, I need to declare the pins as input. Sure, no problem until I reach step 4. Then, I need to send a voltage level from (some of) the Arduino pins I have already declared as input.

So yeah, I'm wondering if you can use pinMode() on the fly. Thanks!

Yes

.

All of the functions can be called whenever you want to. However note that calling them at global scope (eg. in a constructor of an object at global scope) is not advised.

1 Like

LarryD:
Yes

Oh. That eases worries. Thanks. I was playing scenarios in my head already.

Our instructor told us that we can't (as opposed to shouldn't) use pinMode() inside void loop() - that for some reason, the pinMode() method only belongs inside void setup().

So that's not the case? I am worried that I might damage my board somehow.

I'm sorry, I'm afraid I'm not following. Can you please give a simple example in code and expound as to why? Thanks!

1 Like

berds:
So yeah, I'm wondering if you can use pinMode() on the fly. Thanks!

Yes no problem. The only reason why we normally put pinMode() in setup is that in the majority of cases a pin remains either an input or an output for the duration of the program, so it's only necessary to do pinMode once.

There's no reason however that the pinMode cannot be changed on the fly, it's a perfectly valid thing to do. :slight_smile:

1 Like

Our instructor told us that we can't (as opposed to shouldn't) use pinMode() inside void loop() - that for some reason, the pinMode() method only belongs inside void setup().

Time to educate the educator.
As him/her about charlieplexing.

1 Like

Wow. Alright! This is great. Seems like I'm gonna learn a lot here. Thanks, people! I really appreciate the help. :smiley:

1 Like

Here a pin is used in both input and output mode.
http://forum.arduino.cc/index.php?topic=439838.msg3030824#new

.

berds:
Our instructor told us that we can't (as opposed to shouldn't) use pinMode() inside void loop() - that for some reason, the pinMode() method only belongs inside void setup().

Always ask for a reason, and don't accept something stupid like "just because". If this person is supposed to be instructing you, make them explain things properly.

1 Like

Maybe the instructor means you "can't" in the sense that s/he would mark down the assignment if you did. They may be trying to teach you to do sensible things (like configuring pins in setup).

What everyone else said. You can, but putting pinMode in loop() when it only needs to set in once in setup() will get adverse comment here. It's untidy.

That is a ridiculous restriction, especially in light of berds's project which cannot know at compile time which pins need to be outputs, so it must pinMode in loop.

.

Jiggy-Ninja:
That is a ridiculous restriction, especially in light of berds's project which cannot know at compile time which pins need to be outputs, so it must pinMode in loop.

there is a really simple reason for the lecturer to mark down the pinmode setup being incorrect if done in the loop.

the lecturer may be trying to "force" the class to research a way into limit input ports while reading large chunks of data.

if i was adding that restriction as a mentor, my hope would be that students would figure out a way to store the reading in a shift register and implement code to read from the shift register.

i agree 100% with the

Jiggy-Ninja:
"Always ask for a reason, and don't accept something stupid like "just because". If this person is supposed to be instructing you, make them explain things properly."

comment.

PS
i understand thread resurrection is not ideal in forums, however,
1 - some one else learning 'may' come across this and end up being marked down in their assignment because of Jiggy-Ninja's comment

2 - my comment includes the additional information that the use of a shift register is an alternative solution that could be usefull for anyone reading this thread in future.