/* button_led_serial by Robert Butera (rob@butera.org) demonstrates use of pushbutton and LED as indicator connect a 10K resistor between pin BUTTON_PIN and VCC connect a pushbutton with one terminal connected to GND, the other to BUTTON_PIN connect pin LED_PIN to a 220 ohm resistor, in series with an LED, connected to GND the long wire on the LED should be the one connected to the resistor see the DigitalReadSerial tutorial on Arduino.cc for button connections this one adds serial output to demonstrate the serial monitor open the serial monitor to read button state */ // set these to the LED and Button pin numnbers #define LED_PIN 13 #define BUTTON_PIN 2 // this is run every time the Arduino is powered up or the reset button is pressed void setup() { // this makes the designated pin a digital output (the one the LED is connected to) pinMode(LED_PIN, OUTPUT); // this makes the button pin a digital input pinMode(BUTTON_PIN, INPUT); // this sets up serial communications at 9600 bits pers second Serial.begin(9600); } // after setup executes, this loop runs forvever until powerdown or reset void loop() { int buttonstate ; // read the button -- high or low? buttonstate = digitalRead(BUTTON_PIN) ; // LOW means the button is pressed! (closed circuit) // HIGH means the button is not pressed (open circuit) // we want the LED to light up (HIGH) when the button is pressed (LOW) if (buttonstate == LOW) digitalWrite(LED_PIN, HIGH) ; else digitalWrite(LED_PIN, LOW) ; Serial.println(buttonstate) ; // write state to serial output delay(10); // minor delay for serial stability } // there are MANY ways to improve the efficiency or tightness of the above logic -- discuss! // or play with the logic // example: how to change serial monitor so it only outputs when // the button state changes (and same with LED)?