Pages: [1]   Go Down
Author Topic: change pinMode?  (Read 697 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 1
Posts: 142
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17293
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6146
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
if ((oldButtonReading != newButtonReading) && (pinActive == 1)) {{/code]
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Pages: [1]   Go Up
Jump to: