Hope this helps. Not my code but a cut n paste from online.
Why do we need the resistor R1? R1 guarantees that the Arduino’s digital input pin 7 is
connected to a constant voltage of +5V whenever the push button is not pressed. If the
push button is pressed, the signal on pin 7 drops to ground (GND), at the same time the
Arduino’s +5V power is connected to GND, we avoid a shorted circuit by limiting the
current that can flow from +5V to GND with a resistor (1 - 10 KΩ
. Also, if there was no
connection from pin 7 to +5V at all, the input pin would be “floating” whenever the
pushbutton is not pressed. T
his means that it is connected neither to GND nor to +5V,
picking up electrostatic noise leading to a false triggering of the input.
2. Open the Arduino software and write the following code:
/* Basic Digital Read
* ------------------
*
* turns on and off a light emitting diode(LED) connected to digital
* pin 13 (onboard LED, when pressing a pushbutton attached to pin 7.
* It illustrates the concept of Active-Low, which consists in
* connecting buttons using a 1K to 10K pull-up resistor.
*
* Created 1 December 2005
* copyleft 2005 DojoDave <http://www.0j0.org>
*
Arduino - HomePage
*
*/
int ledPin = 13; // choose the pin for the LED
int inPin = 7; // choose the input pin (for a pushbutton)
Winkler, Arduino workshop, sensors, p.5
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT); // declare pushbutton as input
}
void loop(){
val = digitalRead(inPin); // read input value
if (val == HIGH) { // check if the input is HIGH
// i.e. button released
digitalWrite(ledPin, LOW); // turn LED OFF
} else {
digitalWrite(ledPin, HIGH); // turn LED ON
}
}