Button doesn't work if pressed for a long time


Basically I am running a very simple example code, for turning off the arduino led while a button is pressed. I want the led to turn off whenever the button is pressed, even if I am pressing the button for 1 minute, i want the led to be off the entire minute.
But what happens is if I press the button for more than maybe 2 seconds, the led just turns back on even if I am still pressing the button.
Here is the code, it's just a simple example code, but I just don't get why the led turns back on when pressed for a little bit longer.

Thank you in advance

int ledPin = 13; 
int inPin = 2;   
int val = 0;     

void setup() {
  pinMode(ledPin, OUTPUT); 
  pinMode(inPin, INPUT);    

void loop(){
  val = digitalRead(inPin);  
  if (val == HIGH) {        
    digitalWrite(ledPin, LOW); 
  } else {
    digitalWrite(ledPin, HIGH); 

Show your wiring. You're not using the internal pull-up resistors. Do you have external pull-up or pull-down resistors? Or do you have a floating input?

You seem to expect the button to read HIGH when pressed which is backwards, but doable if you use an external pull-down resistor. It's much easier to wire the button the normal way so it reads LOW when pressed so you can use the internal pull-up resistors and don't need the external components.

Yeah, sorry about that, forgot to say, but I am using an external resistor to pull down the value.

Show your wiring.

Did you not understand? Draw a picture and post it. You'd be amazed at how many people swear up and down they wired it right and don't have the first clue.

How to post pictures

here it is

What size is your resistor?


Any help?

you have int inPin = 2; yet your diagram shows that you are connected to D3.