X-Git-Url: http://git.piffa.net/web?a=blobdiff_plain;f=basic%2Fbuttons%2Fbutton_state_4_state%2Fbutton_state_4_state.ino;fp=basic%2Fbuttons%2Fbutton_state_4_state%2Fbutton_state_4_state.ino;h=87ceffa95d49affcf5e83c1ae993ec2564248b94;hb=0807dd2a57e859d7fc246d8eb02692ea4d0da248;hp=0000000000000000000000000000000000000000;hpb=412fa124a8e9482bb252ba40f2619f6c7880e221;p=sketchbook_andrea diff --git a/basic/buttons/button_state_4_state/button_state_4_state.ino b/basic/buttons/button_state_4_state/button_state_4_state.ino new file mode 100644 index 0000000..87ceffa --- /dev/null +++ b/basic/buttons/button_state_4_state/button_state_4_state.ino @@ -0,0 +1,42 @@ +/* + Stato di un bottone + + Legge lo stato di un input + + */ +int led = 13; +int buttonPin = 2; +int statoAttuale; // variable for reading the pin status +int ultimoStato; // variable to hold the last button state +int ledStatus; // varabile per mantenere lo stato del led + +void setup() { + pinMode(buttonPin, INPUT); // Set the switch pin as input + pinMode(led, OUTPUT); + Serial.begin(9600); // Set up serial communication at 9600bps + ultimoStato = digitalRead(buttonPin); // read the initial state + ledStatus = 0; +} + +void loop(){ + statoAttuale = digitalRead(buttonPin); // read input value and store it in var + delay(20); // riduce l'effetto bounce + if (statoAttuale != ultimoStato) { // the button state has changed! + if (statoAttuale == HIGH) { // check if the button is pressed + Serial.println("Button premuto"); + + ledStatus = !ledStatus ; // Inverte lo stato del LED + // ledStatus = 1 - ledStatus ; // Forma analoga + + Serial.print("Stato del LED: "); // DEBUG + Serial.println(ledStatus) ; + } + } + + ultimoStato = statoAttuale; // save the new state in our variable + digitalWrite(led, ledStatus); // setta il led allo stato richiesto + +} + + +