LED not turning off completely on LOW with button

I made a very simple circuit and program to turn on an LED while a button is pressed (just to test it out for another project), but the LED isn't turning off completely even when the pin should be on LOW, it just glows and gets a bit brighter when the button is pressed. It also sometimes seems, strangely, to be affected by how close I am to the circuit.
This problem has only occurred with this specific kind of program, and doesn't happen, for example, when I load the "Blink" example program.

Here's my code:

int outpin = 7;
int inpin = 12;
int val = 0;

void setup() {
pinMode(7, OUTPUT);
pinMode(12, INPUT);
}

void loop() {
val = digitalRead(inpin);
digitalWrite(outpin, val);
}

An LED and resistor are connected in series between digital pin 7 and ground, and a momentary push button is connected between +5v and digital pin 12.
I don't think the problem is with the arduino, since it happened on both of the boards I tried it with.
So, if anyone has any suggestions or can point out what I'm doing wrong, it would be greatly appreciated.
Thanks

FellowBob:
I made a very simple circuit and program to turn on an LED while a button is pressed (just to test it out for another project), but the LED isn't turning off completely even when the pin should be on LOW, it just glows and gets a bit brighter when the button is pressed. It also sometimes seems, strangely, to be affected by how close I am to the circuit.
This problem has only occurred with this specific kind of program, and doesn't happen, for example, when I load the "Blink" example program.

Here's my code:

int outpin = 7;

int inpin = 12;
int val = 0;

void setup() {
pinMode(7, OUTPUT);
pinMode(12, INPUT);
}

void loop() {
val = digitalRead(inpin);
digitalWrite(outpin, val);
}




An LED and resistor are connected in series between digital pin 7 and ground, and a momentary push button is connected between +5v and digital pin 12. 
I don't think the problem is with the arduino, since it happened on both of the boards I tried it with.
So, if anyone has any suggestions or can point out what I'm doing wrong, it would be greatly appreciated.
Thanks

When do you give your visual persistence time to recognize the LED is off??????

Paul

Add an external pull down resistor to the input pin,

or

change the button connection to GND instead of 5V, activate the internal pull up resistor and check for LOW pressed.

const byte outPin = 7;
const byte inPin = 12;

void setup() {
  pinMode(outPin, OUTPUT);
  pinMode(inPin, INPUT_PULLUP);
}

void loop() {
  digitalWrite(outpin, digitalRead(inpin) ? LOW : HIGH);
}

Paul_KD7HB:
When do you give your visual persistence time to recognize the LED is off??????

Paul

I'm not sure what you mean; the LED should be off when the button isn't pressed.

FellowBob:
a momentary push button is connected between +5v and digital pin 12.

Do you have a (10k) pull down resistor between that pin and ground.
Without that, the inpin will be 'floating' when the button is not pushed, causing the problems you see.

Better/easier to connect the button between pin and ground, and use internal pull up on the pin with code.

pinMode(12, INPUT_PULLUP); // button between pin and ground

Or better use the names you gave the pins.

pinMode(outpin, OUTPUT);
pinMode(inpin, INPUT_PULLUP);

Note that logic is now reversed. A button push turns the LED off.
You can change that by adding an exclamation mark in front of digitalRead().

val = !digitalRead(inpin);

Leo..

Whandall:
Add an external pull down resistor to the input pin,

or

change the button connection to GND instead of 5V, activate the internal pull up resistor and check for LOW pressed.

Thank you, the pull-down resistor worked.

Wawa:
Do you have a (10k) pull down resistor between that pin and ground.
Without that, the inpin will be 'floating' when the button is not pushed, causing the problems you see.

Better/easier to connect the button between pin and ground, and use internal pull up on the pin with code.

pinMode(12, INPUT_PULLUP); // button between pin and ground

Or better use the names you gave the pins.

pinMode(outpin, OUTPUT);
pinMode(inpin, INPUT_PULLUP);

Note that logic is now reversed. A button push turns the LED off.
You can change that by adding an exclamation mark in front of digitalRead().

val = !digitalRead(inpin);

Leo..

Thank you, both of your suggestions worked well.