]> git.piffa.net Git - sketchbook_andrea/blob - basic/buttons/button_presses_count_down_5/button_presses_count_down_5.ino
PWM: errata and states
[sketchbook_andrea] / basic / buttons / button_presses_count_down_5 / button_presses_count_down_5.ino
1 /*
2  *  Contatore di input
3
4     Il programma rileva la pressione del bottone e
5     riduce un contatore ad ogni pressione,
6     passando dallo stato iniziale a un secondo stato finale
7     dopo 10 iterazioni.
8  */
9
10 int switchPin = 2;              // switch is connected to pin 2
11 int val;                        // variable for reading the pin status
12 int buttonState;                // variable to hold the last button state
13 int buttonPresses = 10;         // Counter for the button
14
15 void setup() {
16   pinMode(switchPin, INPUT_PULLUP);    // Set the switch pin as input
17
18   Serial.begin(9600);           // Set up serial communication at 9600bps
19   buttonState = digitalRead(switchPin);   // read the initial state
20   Serial.println("Don not press the button! :P ");
21 }
22
23
24 void loop(){
25   val = digitalRead(switchPin);      // read input value and store it in val
26   delay(100);                        // Debounce
27   if ((val != buttonState) && (val == HIGH)) {     // check if the button is pressed
28     buttonPresses-- ;
29     Serial.print("Press it an other ");
30     Serial.print(buttonPresses);
31     Serial.println(" times.");
32   }
33   if (buttonPresses == 0) {
34     Serial.println("----- >  ExplOdE! <------");
35    // Serial.flush();                // Print out the whole serial buffer
36    // exit(0);                       // Exit the sketch
37   }
38   buttonState = val;                 // save the new state in our variable
39 }
40
41