Why the simple code is running very slow?

void setup() {
pinMode(13, OUTPUT);
pinMode(2, INPUT);

              }

void loop() {
//  Serial.begin(9600);
    int a = digitalRead(2);
    
if (a == HIGH)
  digitalWrite(13, HIGH);

else
  digitalWrite(13, LOW);
//Serial.println(a);

            }

when pin 2 goes HIGH, led on 13 glows immediately but when it goes low, led is taking too much time(~3 sec.) to go off what should i do?

what should i do?

Describe how you’ve wired everything (post a hand drawn schematic, not a Fritzing diagram, or clear photo).

Try putting a 1K resistor from pin 2 to ground. Maybe it is taking a long time to go HIGH to LOW.

There is nothing wrong with the code. The problem must be in the wiring connections.

...R

Robin2:
There is nothing wrong with the code. The problem must be in the wiring connections.

…R

jremington:
Describe how you’ve wired everything (post a hand drawn schematic, not a Fritzing diagram, or clear photo).

wtf is this now?? code not changed

Look, you do not have a PULLUP resistor on your input. Use one. Either enable the internal PULLUP on the input or use a 10k PULLUP (or pulldown) connected to the pin.

See: https://learn.sparkfun.com/tutorials/pull-up-resistors/what-is-a-pull-up-resistor

Here is the weird bug I found :confused: Issue: https://drive.google.com/file/d/0B2z8xHe3t8dnT0gxa19kaHFpV2M/view?usp=sharing

  void setup() {
pinMode(13, OUTPUT);
pinMode(2, INPUT);

              }

void loop() {
//  Serial.begin(9600);
    int a = digitalRead(2);
    
if (a == HIGH)
  digitalWrite(13, HIGH);

else
  digitalWrite(13, LOW);
//Serial.println(a);

            }

I think it is issue in board

(deleted)

electroscientistX: wtf is this now?? code not changed

Who said it was changed ?

...R

My assumption is that you have a switch connected between Pin 2 and Vcc.

So when switch is actuated, Pin 2 goes high. But when the switch is then released... why would pin 2 stop being high? That pin is configured as an input, so there's nothing to cause the voltage on the pin to go back to logical-0. The only reason it ever changes is that the pin and wire going to the switch will act as an antenna, picking up local electrical noise. You can see this effect very clearly if you use the analog read serial example with nothing connected to it.

And that's why you need a pulldown on pin 2 - i normally default 10k for pullups and pulldowns, but anything from 1k to 100k will work for simple cases.

The other approach is to invert the switch, so it connects pin 2 to gnd, and then set the pin to INPUT_PULLUP, which uses the chip's internal pullup resistors (it doesn't have internal pulldowns)

Threads merged. What's the point of posing the question again (in completely the wrong forum section) when it's already been answered here?