Hey guys pardon me if this is obvious. I'm wondering if I can use an output for driving a status led, and assuming I'm in a tight enough loop, can I flip to pin mode to input check if the button is down, and then restore the pin to output mode with its previous state?
This way I would get double duty with my pin right?
I'm new, and I don't even have a board yet, but I ordered one last week and I'm getting itchy.
This is doable, indeed. But remember that the button migh miss presses. And the led might flicker.
I started to write som pseudocode, but you've actually done it yourself.
tight enough loop,
log outputstate
I flip pin mode to input
check if the button is down
log result
and then restore the pin to output mode
set state?
thight loop end
Thanks AlphaBeta for the confirmation. I wasn't sure if I would have to save/restore the output state before checking for input.
I'm going to use a couple pins for alert conditions. I figure a NO switch used to pull the pin high when pushed would just turn on the led, which doesn't sound like a problem.
What's the general wisdom on button handling?
I looked at the various examples, including the arduino clock program, and everyone seems to just read a pin to see if a button is pressed. Would an electrolytic help hold the pin longer to make it easier for the loop to catch?
It's tempting to use an interrupt to detect keypresses, but I'm guessing that those might be nice to save for XBee and Ethernet processes if in fact they are needed for that.
Also be careful from a hardware perspective if you attempt this dual use. If the button was wired to +5 or ground on one end and it was still pressed down when your program switches the pin to output mode, then you would short circuit the output pin and burn it out.
So be sure to add a series resistor on one side of the switch such that a output pin would not sink or source more then 20ma or so if the switch was held down.
Lefty