Min menu

Pages

project 12:PushbuttonControlled LED

PushbuttonControlled LED

In this project, you’ll add a pushbutton switch to an LED circuit to control when the LED is lit.

/* by DojoDave modified 30 Aug 2011 by Tom Igoe This example code is in the public domain. http://www.arduino.cc/en/Tutorial/Button */

 const int buttonPin = 2; // Pin connected to pushbutton const int ledPin = 13; // Pin connected to LED int buttonState = 0; // Give pushbutton a value void setup() { pinMode(ledPin, OUTPUT); // Set LED pin as output pinMode(buttonPin, INPUT); // Set pushbutton pin as input } void loop() { buttonState = digitalRead(buttonPin); // Read input from pin 2 if (buttonState == HIGH) { // If pushbutton is pressed, set as HIGH digitalWrite(ledPin, HIGH); // Turn on LED } else { digitalWrite(ledPin, LOW); // Otherwise, turn off LED } }



reaction: