Skip to content

Instantly share code, notes, and snippets.

@plttn
Created February 24, 2017 02:04
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 plttn/c7af1ba481914288c1b848c4d248c5fe to your computer and use it in GitHub Desktop.
Save plttn/c7af1ba481914288c1b848c4d248c5fe to your computer and use it in GitHub Desktop.
// First we'll set up constants for the pin numbers.
// This will make it easier to follow the code below.
const int button1Pin = 2; // pushbutton 1 pin
const int ledPin = 13; // LED pin
void setup()
{
// Set up the pushbutton pins to be an input:
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
// Set up the LED pin to be an output:
pinMode(ledPin, OUTPUT);
}
void loop()
{
int button1State; // variables to hold the pushbutton states
// Since a pushbutton has only two states (pushed or not pushed),
// we've run them into digital inputs. To read an input, we'll
// use the digitalRead() function. This function takes one
// parameter, the pin number, and returns either HIGH (5V)
// or LOW (GND).
// Here we'll read the current pushbutton states into
// two variables:
button1State = digitalRead(button1Pin);
// Remember that if the button is being pressed, it will be
// connected to GND. If the button is not being pressed,
// the pullup resistor will connect it to 5 Volts.
// So the state will be LOW when it is being pressed,
// and HIGH when it is not being pressed.
// digitalWrite() takes two parameters, a digital pin to control, and a
// state of either HIGH or LOW.
// Using the knowledge you have of the button state and the LED pin
// use logical statements to make the LED turn on when you hold the button
if (button1State == HIGH) {
digitalWrite(ledPin, LOW);
} else {
digitalWrite(ledPin, HIGH);
}
// hint: you'll need to write a function call that both turns the LED on
// and turns the led off based on whether or not the button is being
// pushed
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment