I have short wires with button attached to pin 2 and ground. However, even without touching anything, the led is toggled many times a minute. Due to that (I guess) it is reacting to my button press very very randomly. It looks like the constant charge on the wires are interfering the actual button press and acting like antenna. I've tried different Arduino UNO boards, same results, so the board is not faulty.
int pin = 9;
volatile boolean state = LOW;
void setup()
{
attachInterrupt(0, blink, CHANGE);
pinMode(pin, OUTPUT);
}
void loop()
{
digitalWrite(pin, state);
delay(1000);// for debug purposes
}
void blink()
{
if (state ==LOW)
{
state=HIGH;
}
else
{
state=LOW;
}
}
Will appreciate if anyone can recommend hardware solution to this.