Arduino Project Button

Button


Breadboard



Shematic




Code


  1. const int buttonPin = 2;     // the number of the pushbutton pin
  2. const int ledPin =  13;      // the number of the LED pin

  3. // variables will change:
  4. int buttonState = 0;         // variable for reading the pushbutton status

  5. void setup() {
  6.   // initialize the LED pin as an output:
  7.   pinMode(ledPin, OUTPUT);
  8.   // initialize the pushbutton pin as an input:
  9.   pinMode(buttonPin, INPUT);
  10. }

  11. void loop() {
  12.   // read the state of the pushbutton value:
  13.   buttonState = digitalRead(buttonPin);

  14.   // check if the pushbutton is pressed.
  15.   // if it is, the buttonState is HIGH:
  16.   if (buttonState == HIGH) {
  17.     // turn LED on:
  18.     digitalWrite(ledPin, HIGH);
  19.   }
  20.   else {
  21.     // turn LED off:
  22.     digitalWrite(ledPin, LOW);
  23.   }
  24. }

Post a Comment

0 Comments