Advertisement
3rdWard_Arduino

class2_buttonLED

Jun 10th, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. /*
  2.   Switch and LED
  3.  
  4.  Sends HIGH or LOW to serial monitor, when pressing a pushbutton attached to pin 2.
  5.  
  6.  
  7.  The circuit:
  8.  * led attached to pin 3 with a 100 ohm resistor to ground
  9.  * pushbutton attached to pin 2 from +5V
  10.  * 10K resistor attached to pin 2 from ground
  11.  
  12.  */
  13.  
  14. // set pin numbers:
  15. int buttonPin = 2;     // the number of the pushbutton pin
  16. int ledPin = 3;
  17.  
  18. // variables will change:
  19. int buttonState = 0;         // variable for reading the pushbutton status
  20.  
  21. void setup() {
  22.   // initialize the pushbutton pin as an input:
  23.   pinMode(buttonPin, INPUT);
  24.   // initialize the led pin as an output:
  25.   pinMode(ledPin, OUTPUT);
  26.  
  27.   // initialize the Serial communication
  28.   Serial.begin(9600);  
  29. }
  30.  
  31. void loop(){
  32.   // read the state of the pushbutton value:
  33.   buttonState = digitalRead(buttonPin);
  34.  
  35.   // check if the pushbutton is pressed.
  36.   if (buttonState == HIGH) {
  37.     // turn on LED by turning ledPin HIGH
  38.     digitalWrite(ledPin, HIGH);
  39.     // send HIGH to the serial monitor
  40.     Serial.println("HIGH");
  41.   }
  42.   else {
  43.     // turn on LED by turning ledPin LOW
  44.     digitalWrite(ledPin, LOW);
  45.     // send LOW to the serial monitor
  46.     Serial.println("LOW");
  47.   }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement