Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This sketch reads the state of a button (pressed or unpressed) and turns on an LED when the button is pressed.
/*
Pushbutton sketch a switch connected to pin 2 lights the LED on pin 13
*/
const int ledPin = 13; // choose the pin for the LED
const int inputPin = 2; // choose the input pin (for a
// pushbutton)
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare pushbutton as input
}
void loop(){
int val = digitalRead(inputPin); // read input value
if (val == HIGH)
{
digitalWrite(ledPin,HIGH);
} else
{
digitalWrite(ledPin,LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment