I'm trying out various things with my new Arduino Mega to get acquainted with it. I've hooked up a pushbutton to pin 31 and sent the value of the pin periodically to the Arduino serial monitor. When the switch is open, instead of sending all 1's or all 0's, it sends alternating 1's and 0's. When I close the switch, it sends all 0's. Why not read all 1's when the switch is open?
Here's the test code I'm running:
int digitalSensor = 31;
int digitalValue = 0;
void setup()
{
Serial.begin( 115200 );
pinMode( digitalSensor, INPUT );
}
void loop()
{
digitalValue = digitalRead( digitalSensor );
Serial.print( "digital value = " );
Serial.println( digitalValue, DEC );
delay( 200 );
}
The reason for this is probably obvious to you guys, but to me it's a mystery. Can you please enlighten me?
Thanks,
Dave