Can a arduino pin mode be changed mid program?

Hi, I'm working on a project that requires a reading to be taken from a pin (digital) and then that pin needs to be supplying power. Can I change the pinmode from input to output mid program, or do I need transistors to switch it between different pins instead?

sort by: active | newest | oldest
Hedayat.sa1 year ago

pinMode(14, INPUT);

pinMode(14, OUTPUT);

Of course it can, look at the example->sensors->ping sketch :)

How fast can this be achieved? would it take 1ms or less, or are we talking 100ms?

Will the output damage the device your reading from? Are you at a point where your out of available pins so one has to run double duties?
David97 (author)  mpilchfamily4 years ago
No I have a sensor that when a ball bearing falls over certain pins it can tell the direction. However there is a point that it can fall across 2 input lines which means that no reading would be taken. So I thought the easiest way to fix it would be to if there was no power supplied to that pin, it would power it to see what other ones the ball had fallen across.
As long as the external circuits connected to the pins don't interfere with each other, you can change any of the settings at any point in the program. Do you have a circuit for what you are trying to do?