Go Down

Topic: change pinMode? (Read 757 times) previous topic - next topic

treebykooba

Hello,
I was wondering if there's a way to change the behaviour of a pin mid program. I have a button that I want to sort of shut off while a conditional is running. If in void setup() i have, say, pinMode(button1, INPUT); and then in the if conditional i have if (oldButtonReading != newButtonReading) {.....and then while that conditional is running, I don't want the arduino to be reading anything from button1. Is this possible? Could I change pinMode to OUTPUT or something just while it's in that if ()?
Another option is that I could set up a little 5V relay or something to just break the connection but it'd be way easier to do something with the code.
thanks for any help!

retrolefty


Hello,
I was wondering if there's a way to change the behaviour of a pin mid program. I have a button that I want to sort of shut off while a conditional is running. If in void setup() i have, say, pinMode(button1, INPUT); and then in the if conditional i have if (oldButtonReading != newButtonReading) {.....and then while that conditional is running, I don't want the arduino to be reading anything from button1. Is this possible? Could I change pinMode to OUTPUT or something just while it's in that if ()?
Another option is that I could set up a little 5V relay or something to just break the connection but it'd be way easier to do something with the code.
thanks for any help!


No, you are looking for a hardware solution when the correct solution is just proper software struture and control statements.

Lefty


James C4S

Quote
I was wondering if there's a way to change the behaviour of a pin mid program.

You can call pinMode() anytime you like.  However, this doesn't achieve what you want.

Quote
(oldButtonReading != newButtonReading) {.....and then while that conditional is running, I don't want the arduino to be reading anything from button1. Is this possible? Could I change pinMode to OUTPUT or something just while it's in that if ()?

No.  If you call digitalRead() on a pin defined as OUTPUT, the result will be whatever that pin's register is current set to.

What do you mean by "while that conditional is running"?  If you are inside of the if-statement, then the if-statement won't be checked again.

Quote
Another option is that I could set up a little 5V relay or something to just break the connection but it'd be way easier to do something with the code.

That seems like a pretty expensive way to achieve something that could be done with a simple flag.  Create a variable called "pinActive."  Set it to 1 when you want to read from the pin and to 0 when you don't.  Then change your if statement to:

Code: [Select]
if ((oldButtonReading != newButtonReading) && (pinActive == 1)) {{/code]
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Go Up