I am trying to figure out how to use/code a switch.
Since I don't have a switch here I just hooked a wire to the pin 2 and a wire to GND. So, to activate my "switch" I just join the two wires.
Then I have the following code:
int MySwitch = 2;
void setup()
{
Serial.begin(9600);
pinMode(MySwitch, INPUT);
}
void loop()
{
Serial.print(digitalRead(MySwitch));
}
When I press my button I get a constant 0 value, and that's OK (I am assuming that 0 is a reliable reading of pressed button). The problem is that when the button is unpressed I get crazy random 0/1 values.
How can I get a stable/constant sign of unpressed button? What am I doing wrong?
Thanks!