X-Git-Url: http://git.piffa.net/web?a=blobdiff_plain;f=basic%2Fbuttons%2Fbutton_presses_counter_AND_4%2Fbutton_presses_counter_AND_4.ino;h=dac48cb93faeba7be4a071cee38123c0ae1cb400;hb=d19cffa69bc2d538de8b73f854f5c3163e231f21;hp=6f5c373890ce10bc90596d028ef1ef41484728d1;hpb=6a0496fdc97f180319df0a3270d1a78920e7ec32;p=sketchbook_andrea diff --git a/basic/buttons/button_presses_counter_AND_4/button_presses_counter_AND_4.ino b/basic/buttons/button_presses_counter_AND_4/button_presses_counter_AND_4.ino index 6f5c373..dac48cb 100644 --- a/basic/buttons/button_presses_counter_AND_4/button_presses_counter_AND_4.ino +++ b/basic/buttons/button_presses_counter_AND_4/button_presses_counter_AND_4.ino @@ -3,28 +3,28 @@ */ int switchPin = 2; // switch is connected to pin 2 -int val; // variable for reading the pin status -int buttonState; // variable to hold the last button state +int statoAttuale; // variable for reading the pin status +int ultimoStato; // variable to hold the last button state int buttonPresses = 0; // Counter for the button void setup() { - pinMode(switchPin, INPUT_PULLUP); // Set the switch pin as input + pinMode(switchPin, INPUT); // Set the switch pin as input Serial.begin(9600); // Set up serial communication at 9600bps - buttonState = digitalRead(switchPin); // read the initial state + ultimoStato = digitalRead(switchPin); // read the initial state } void loop(){ - val = digitalRead(switchPin); // read input value and store it in val - delay(100); // Debounce - if ((val != buttonState) && (val == HIGH)) { // check if the button is pressed + statoAttuale = digitalRead(switchPin); // read input value and store it in val + delay(20); // Debounce, sort of... + if ((statoAttuale != ultimoStato) && (statoAttuale == HIGH)) { // check if the button is pressed buttonPresses++ ; Serial.print("Button has been pressed "); Serial.print(buttonPresses); Serial.println(" times."); } - buttonState = val; // save the new state in our variable + ultimoStato = statoAttuale; // save the new state in our variable }