Advertisement
ccarman602

Lesson-2-Button

Nov 25th, 2021 (edited)
636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Button to turn ON/OFF LED
  2. //Constants won't change. They're used here to set pin numbers:
  3. const int buttonPin = 6;     // the number of the pushbutton pin
  4. const int ledPin =  4;      // 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. If it is, the buttonState is HIGH:
  21.   if (buttonState == HIGH) {
  22.     // turn LED on:
  23.     digitalWrite(ledPin, HIGH);
  24.   } else {
  25.     // turn LED off:
  26.     digitalWrite(ledPin, LOW);
  27.   }
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement