Skip to content

Instantly share code, notes, and snippets.

@futureshocked
Created January 6, 2020 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save futureshocked/1e38afe8c860ea5107e2a4be7cfc799d to your computer and use it in GitHub Desktop.
Save futureshocked/1e38afe8c860ea5107e2a4be7cfc799d to your computer and use it in GitHub Desktop.
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