p02_ButtonControlLED.ino 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /*
  2. Demo code for Project 2 - Button Control LED
  3. Turns on and off a light emitting diode(LED) connected to digital
  4. pin 9, when pressing a pushbutton attached to pin 2.
  5. by Maker Studio
  6. http://makerstudio.cc
  7. */
  8. // constants won't change. They're used here to
  9. // set pin numbers:
  10. const int buttonPin = 2; // the number of the pushbutton pin
  11. const int ledPin = 9; // the number of the LED pin
  12. // variables will change:
  13. int buttonState = 0; // variable for reading the pushbutton status
  14. void setup() {
  15. // initialize the LED pin as an output:
  16. pinMode(ledPin, OUTPUT);
  17. // initialize the pushbutton pin as an input:
  18. pinMode(buttonPin, INPUT);
  19. }
  20. void loop(){
  21. // read the state of the pushbutton value:
  22. buttonState = digitalRead(buttonPin);
  23. // check if the pushbutton is pressed.
  24. // if it is, the buttonState is HIGH:
  25. if (buttonState == HIGH) {
  26. // turn LED on:
  27. digitalWrite(ledPin, HIGH);
  28. }
  29. else {
  30. // turn LED off:
  31. digitalWrite(ledPin, LOW);
  32. }
  33. }