Advertisement
3rdWard_Arduino

class2_button

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