inventrkits

button

Jan 22nd, 2020
639
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // constants won't change. They're used here to
  2. // set pin numbers:
  3. const int buttonPin = 2;     // the number of the pushbutton pin
  4. const int ledPin =  13;      // the number of the LED pin
  5.  
  6. // variables will change:
  7. int buttonState = 0;         // variable for reading the pushbutton status
  8.  
  9. void setup() {
  10.   // initialize the LED pin as an output:
  11.   pinMode(ledPin, OUTPUT);
  12.   // initialize the pushbutton pin as an input:
  13.   pinMode(buttonPin, INPUT);
  14. }
  15.  
  16. void loop() {
  17.   // read the state of the pushbutton value:
  18.   buttonState = digitalRead(buttonPin);
  19.  
  20.   // check if the pushbutton is pressed.
  21.   // if it is, the buttonState is HIGH:
  22.   if (buttonState == HIGH) {
  23.     // turn LED on:
  24.     digitalWrite(ledPin, HIGH);
  25.   } else {
  26.     // turn LED off:
  27.     digitalWrite(ledPin, LOW);
  28.   }
  29. }
Add Comment
Please, Sign In to add comment