digitalWrite() problems

Whenever I upload this sketch http://pastebin.com/PHBRdnxT It won’t turn on the led even when I press the button unless I touch the wire going into pin 7. Why is it doing this?

CDCosma:
Whenever I upload this sketch code - Pastebin.com It won't turn on the led even when I press the button unless I touch the wire going into pin 7. Why is it doing this?

You most likely have a wiring problem and also most likely require a pull-down or pull-up resistor to operate correctly. Only if you can draw out how you are wiring the switch up (or explain very carefully and explicitly) can we be sure of what you need to change.

Lefty

I am using a SPDT switch with the common pin attatched to GND on the arduino and the default on pin on the switch to pin 7. I have no resistors attatched.

CDCosma:
I am using a SPDT switch with the common pin attatched to GND on the arduino and the default on pin on the switch to pin 7. I have no resistors attatched.

Ok, you just need to enable the input pin's internal pull-up resistor. Add the following line in your setup() function after the last pinMode(inPin, INPUT); statement:

digitalWrite(inPin, HIGH);  //enables the internal pull-up resistor

Lefty

Thanks, It worked :smiley: