Input Pullup Serial

/*
Input Pullup Serial

This example demonstrates the use of pinMode(INPUT_PULLUP). It reads a
digital input on pin 2 and prints the results to the serial monitor.

The circuit:
* Momentary switch attached from pin 2 to ground
* Built-in LED on pin 13

Unlike pinMode(INPUT), there is no pull-down resistor necessary. An internal
20K-ohm resistor is pulled to 5V. This configuration causes the input to
read HIGH when the switch is open, and LOW when it is closed.

created 14 March 2012
by Scott Fitzgerald

http://www.arduino.cc/en/Tutorial/InputPullupSerial

This example code is in the public domain

*/

const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 1; // the number of the default LED pin

void setup(){
//configure pin2 as an input and enable the internal pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);

}

void loop(){
//read the pushbutton value into a variable
int sensorVal = digitalRead(buttonPin);

// Keep in mind the pullup means the pushbutton’s
// logic is inverted. It goes HIGH when it’s open,
// and LOW when it’s pressed. Turn on ledPin when the
// button’s pressed, and off when it’s not:
if (sensorVal == HIGH) {
digitalWrite(ledPin, LOW);
}
else {
digitalWrite(ledPin, HIGH);
}
}