Reading and writing to a single digital pin

Thanx for all your replies :slight_smile:

@Krodal
I thought of a circuit that could work and this was what that came to my mind at that moment.Hence the internal pullup. Any other circuit to wire this on to a single pin is most welcome.
Will check out your code soon. A bit busy today. Thanx :slight_smile:

@PaulS

 int buttonState = digitalRead(pushButton);  // Read the push button pin. Usually its zero, when pressed it goes to HIGH or 1

Shouldnt it work like this? because I think the internal resistor is atleast 20k or more. (Could not find the exact value from datasheet) so it will be like when pull up is enabled. Shouldnt only a maximum of 5/3 = 1.66 V come across the 10k external pull down resistor? which I believe should not cause the digitalRead to read it as an input 1...

@UKHeliBob
The first code I posted is apparently working fine. Only the second code gives me problems.

have anything to do with the delay(2000) in the loop() function and the switching of the pinMode ?

Cant say I havent thought of this but 2sec for switching is too much I guess. Or Am I wrong. Cant seem to figure out how to resolve the issue if thats the case.

Are you so short of pins that you need to connect the switch and the LED to the same pin ?

Well I am not actually. I would like reduce the wires going to arduino and save pins if its possible. If this doesnt work out I can always go back to 2 pins for a unit but I really would like to get this one working.

@Shpaget

Why go through all the trouble with the software when you can do this?

For obvious reasons of hardware requirement.