]> git.piffa.net Git - arduino/blob - var_sketches/get_started_2/_2_push_button/_2_push_button.ino
first commit
[arduino] / var_sketches / get_started_2 / _2_push_button / _2_push_button.ino
1 // Example 02: Turn on LED while the button is pressed
2 const int LED = 13;
3 // the pin for the LED
4 const int BUTTON = 7; // the input pin where the
5 // pushbutton is connected
6 int val = 0;
7 // val will be used to store the state
8 // of the input pin
9 void setup() {
10   pinMode(LED, OUTPUT);  // tell Arduino LED is an output
11   pinMode(BUTTON, INPUT); // and BUTTON is an input
12 }
13 void loop(){
14   val = digitalRead(BUTTON); // read input value and store it
15   // check whether the input is HIGH (button pressed)
16   if (val == HIGH) {
17     digitalWrite(LED, HIGH); // turn LED ON
18   } 
19   else {
20     digitalWrite(LED, LOW);
21   }
22 }
23
24