]> git.piffa.net Git - sketchbook_andrea/blob - basic/button_presses_count_down_5/button_presses_count_down_5.ino
7e43837c9777ee7b15c85647f2b5732516824f82
[sketchbook_andrea] / basic / button_presses_count_down_5 / button_presses_count_down_5.ino
1 /*
2  *  Alternating switch
3  */
4
5 int switchPin = 2;              // switch is connected to pin 2
6 int val;                        // variable for reading the pin status
7 int buttonState;                // variable to hold the last button state
8 int buttonPresses = 10;          // Counter for the button
9
10 void setup() {
11   pinMode(switchPin, INPUT_PULLUP);    // Set the switch pin as input
12
13   Serial.begin(9600);           // Set up serial communication at 9600bps
14   buttonState = digitalRead(switchPin);   // read the initial state
15   Serial.println("Don not press the button! :P ");
16 }
17
18
19 void loop(){
20   val = digitalRead(switchPin);      // read input value and store it in val
21   delay(100);                        // Debounce
22   if ((val != buttonState) && (val == HIGH)) {     // check if the button is pressed
23     buttonPresses-- ;
24     Serial.print("Press it an other ");
25     Serial.print(buttonPresses);
26     Serial.println(" times.");
27   }
28   if (buttonPresses == 0) {
29     Serial.println("----- >  ExplOdE! <------");
30    // Serial.flush();                // Print out the whole serial buffer
31    // exit(0);                       // Exit the sketch
32   }
33   buttonState = val;                 // save the new state in our variable
34 }
35
36