]> git.piffa.net Git - sketchbook_andrea/blob - basic/buttons/button_presses_counter_AND_4/button_presses_counter_AND_4.ino
Buttons and serial
[sketchbook_andrea] / basic / buttons / button_presses_counter_AND_4 / button_presses_counter_AND_4.ino
1 /*
2  *  Alternating switch
3  */
4
5 int switchPin = 2;              // switch is connected to pin 2
6 int statoAttuale;                        // variable for reading the pin status
7 int ultimoStato;                // variable to hold the last button state
8 int buttonPresses = 0;          // 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   ultimoStato = digitalRead(switchPin);   // read the initial state
15 }
16
17
18 void loop(){
19   statoAttuale = digitalRead(switchPin);      // read input value and store it in val
20   delay(100);                        // Debounce, sort of...
21   if ((statoAttuale != ultimoStato) && (statoAttuale == HIGH)) {     // check if the button is pressed
22     buttonPresses++ ;
23     Serial.print("Button has been pressed ");
24     Serial.print(buttonPresses);
25     Serial.println(" times.");
26   }
27   ultimoStato = statoAttuale;                 // save the new state in our variable
28 }
29
30