From 0807dd2a57e859d7fc246d8eb02692ea4d0da248 Mon Sep 17 00:00:00 2001 From: Andrea Manni Date: Mon, 9 Mar 2015 17:10:34 +0100 Subject: [PATCH] Buttons and serial --- .../Serial_hello_world/Serial_hello_world.ino | 32 ++++++++++++ basic/buttons/button_1/button_1.ino | 10 +++- .../button_2_serial_debug.ino | 50 ++++++++++++++++++ .../button_presses_counter_AND_4.ino | 14 ++--- .../buttons/button_state_3/button_state_3.ino | 29 ++++++----- .../button_state_4_state.ino | 42 +++++++++++++++ .../button_state_4_state_and_condition.ino | 41 +++++++++++++++ basic/buttons/debounce/debounce.ino | 52 +++++++++++++++++++ .../debounce_3_and_contratto.ino | 52 +++++++++++++++++++ serial/comm_rx/comm_rx.ino | 2 +- .../debug_preprocessor/debug_preprocessor.ino | 48 +++++++++++++++++ 11 files changed, 350 insertions(+), 22 deletions(-) create mode 100644 basic/Serial_hello_world/Serial_hello_world.ino create mode 100644 basic/buttons/button_2_serial_debug/button_2_serial_debug.ino create mode 100644 basic/buttons/button_state_4_state/button_state_4_state.ino create mode 100644 basic/buttons/button_state_4_state_and_condition/button_state_4_state_and_condition.ino create mode 100644 basic/buttons/debounce/debounce.ino create mode 100644 basic/buttons/debounce_3_and_contratto/debounce_3_and_contratto.ino create mode 100644 serial/debug_preprocessor/debug_preprocessor.ino diff --git a/basic/Serial_hello_world/Serial_hello_world.ino b/basic/Serial_hello_world/Serial_hello_world.ino new file mode 100644 index 0000000..846a798 --- /dev/null +++ b/basic/Serial_hello_world/Serial_hello_world.ino @@ -0,0 +1,32 @@ +/* + * Hello World! + * + * This is the Hello World! for Arduino. + * It shows how to send data to the computer trough the serial connection + */ + +void setup() +{ + Serial.begin(9600); // set up Serial library at 9600 bps + // If the Arduino transfers data at 9600 bits per second + // and you're sending 12 bytes of data, how long does it take to send over this information? + + // Try to change bond rate in the monitor + + Serial.println("Hello World!"); // prints hello with ending line break + + + Serial.print("Il mio nome e': ") // Scrive senza ritorno a capo + Serial.println("Andrea"); // Scrive con ritorno a capo + Serial.flush // Assicura che tutto il testo venga scritto + +} + +void loop() // run over and over again +{ + // Try to put the Serial.printl() statenent in here, with a delay maybe +} + + + + diff --git a/basic/buttons/button_1/button_1.ino b/basic/buttons/button_1/button_1.ino index 5d840ec..15e9abe 100644 --- a/basic/buttons/button_1/button_1.ino +++ b/basic/buttons/button_1/button_1.ino @@ -9,7 +9,7 @@ // Pin 13 has an LED connected on most Arduino boards. // give it a name: int led = 13; -int input = 8; +int input = 2; // the setup routine runs once when you press reset: void setup() { @@ -28,7 +28,13 @@ void loop() { } } +// Modifiche: +// 1. invertire il programma facendo in modo che il led si spenga +// quando il bottone e' premuto. Consoderare come ottenere lo stesso risultato +// modificando il circuito. +// 2. Modificare il programma per far brillare il led cinque volte al secondo +// quando il bottone e' premuto. -// Funzioni create dall'utente: +// Domanda: cosa succede se il jumper input non e' collegato ne al +5 ne al gound? diff --git a/basic/buttons/button_2_serial_debug/button_2_serial_debug.ino b/basic/buttons/button_2_serial_debug/button_2_serial_debug.ino new file mode 100644 index 0000000..f0b41d5 --- /dev/null +++ b/basic/buttons/button_2_serial_debug/button_2_serial_debug.ino @@ -0,0 +1,50 @@ +/* + Input serial + + + Accensione e spegnimanto di un LED utilizzando un pin come input. + + Schemi del circuito: + - http://lab.piffa.net/schemi/button_1_bb.png + - http://lab.piffa.net/schemi/button_1_schem.png + */ + +// Pin 13 has an LED connected on most Arduino boards. +// give it a name: +int led = 13; +int input = 2; + +// the setup routine runs once when you press reset: +void setup() { + // initialize the digital pin as an output. + pinMode(led, OUTPUT); // Il PIN e' attivato come output + pinMode(input, INPUT); // Il PIN e' attivato come output + + Serial.begin(9600); // Attivazione seriale +} + +// the loop routine runs over and over again forever: +void loop() { + if (digitalRead(input) == HIGH) { // Verifica se il PIN input e' +5v + digitalWrite(led, HIGH); + Serial.println("Bottone premuto: circuito chiuso"); // Debug seriale + delay(200); + } + else { // Alterativa: se non e' +5v + digitalWrite(led, LOW); + Serial.println("Bottone libero: circuito aperto"); // Debug seriale + delay(200); + } +} + +// Modifiche: +// 1. invertire il programma facendo in modo che il led si spenga +// quando il bottone e' premuto. Consoderare come ottenere lo stesso risultato +// modificando il circuito. +// 2. Modificare il programma per far brillare il led cinque volte al secondo +// quando il bottone e' premuto. + +// Domanda: cosa succede se il jumper input non e' collegato ne al +5 ne al gound? + + + 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..20d15d0 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 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(100); // 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 } diff --git a/basic/buttons/button_state_3/button_state_3.ino b/basic/buttons/button_state_3/button_state_3.ino index de29a13..2bc683b 100644 --- a/basic/buttons/button_state_3/button_state_3.ino +++ b/basic/buttons/button_state_3/button_state_3.ino @@ -1,29 +1,34 @@ /* - * Alternating switch + Stato di un bottone + + Legge lo stato di un input + */ 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 void setup() { 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 - - if (val != buttonState) { // the button state has changed! - if (val == LOW) { // check if the button is pressed - Serial.println("Button just pressed"); - } else { // the button is -not- pressed... - Serial.println("Button just released"); + statoAttuale = digitalRead(switchPin); // read input value and store it in val + // delay(20) // riduce leffetto bounce + if (statoAttuale != ultimoStato) { // the button state has changed! + if (statoAttuale == HIGH) { // check if the button is pressed + Serial.println("Bottone premuto"); + } + else { // the button is -not- pressed... + Serial.println("Bottone rilasciato"); } } - buttonState = val; // save the new state in our variable + ultimoStato = statoAttuale; // save the new state in our variable } + 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 + +} + + + diff --git a/basic/buttons/button_state_4_state_and_condition/button_state_4_state_and_condition.ino b/basic/buttons/button_state_4_state_and_condition/button_state_4_state_and_condition.ino new file mode 100644 index 0000000..42fd3a0 --- /dev/null +++ b/basic/buttons/button_state_4_state_and_condition/button_state_4_state_and_condition.ino @@ -0,0 +1,41 @@ +/* + 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 && statoAttuale == HIGH) { + // the button state has changed AND 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 + +} + + + diff --git a/basic/buttons/debounce/debounce.ino b/basic/buttons/debounce/debounce.ino new file mode 100644 index 0000000..d8b3f36 --- /dev/null +++ b/basic/buttons/debounce/debounce.ino @@ -0,0 +1,52 @@ +/* + Stato di un bottone + + Modifica lo stato di un led in base all'input di un bottone. + Viene usato un ciclo condizionale ramificato + e due variabili per confrontare il periodo di pressione del bottone. + + */ +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 = 0; // varabile per mantenere lo stato del led + +long ultimoCambio = 0; // Momento in cui e' stato attivato il PIN input +long debounceDelay = 30; // Tempo di debounce + +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 +} + +void loop(){ + int lettura = digitalRead(buttonPin); // read input value and store it in var + if (lettura != ultimoStato) { // controlla se il bottone ha cambiato stato + ultimoCambio = millis() ; // Registra il tempo attuale + } + + if ((millis() - ultimoCambio) > debounceDelay) { // controllo che il periodo di tempo sia sufficente + if (lettura != statoAttuale) { // Il bottone ha cambiato stato + statoAttuale = lettura; // Evitiamo che la precedente condizione si ripeta ad oltranza + if (statoAttuale == HIGH) { // check if the button is pressed + Serial.println("Button premuto"); + + ledStatus = !ledStatus ; // Inverte lo stato del LED + Serial.print("Stato del LED: "); // DEBUG + Serial.println(ledStatus) ; + } + } + } + ultimoStato = lettura; // save the new state in our variable + digitalWrite(led, ledStatus); // setta il led allo stato richiesto + +} + + + + + + diff --git a/basic/buttons/debounce_3_and_contratto/debounce_3_and_contratto.ino b/basic/buttons/debounce_3_and_contratto/debounce_3_and_contratto.ino new file mode 100644 index 0000000..83230a9 --- /dev/null +++ b/basic/buttons/debounce_3_and_contratto/debounce_3_and_contratto.ino @@ -0,0 +1,52 @@ +/* + 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 = 0; // varabile per mantenere lo stato del led + +long ultimoCambio = 0; // Momento in cui e' stato attivato il PIN input +long debounceDelay = 100; // Tempo di debounce + +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 +} + +void loop(){ + statoAttuale = digitalRead(buttonPin); // read input value and store it in var + + if (statoAttuale == HIGH && statoAttuale != ultimoStato && millis() - ultimoCambio > debounceDelay ) { + // + Serial.println("Button premuto"); + + ledStatus = !ledStatus ; // Inverte lo stato del LED + Serial.print("Stato del LED: "); // DEBUG + Serial.println(ledStatus) ; + ultimoCambio = millis() ; // Registra il tempo attuale + } + + // Serial.print("statoAttuale "); + // Serial.println(statoAttuale); + // Serial.println(ultimoStato); + //delay(400); + ultimoStato = statoAttuale; // save the new state in our variable + digitalWrite(led, ledStatus); // setta il led allo stato richiesto + + +} + + + + + + + + diff --git a/serial/comm_rx/comm_rx.ino b/serial/comm_rx/comm_rx.ino index 59a7d79..eea5540 100644 --- a/serial/comm_rx/comm_rx.ino +++ b/serial/comm_rx/comm_rx.ino @@ -1,7 +1,7 @@ /* Analog comm: RX - Comunicazione seriale tra due schede arduino. + Comunicazione analogica tra due schede arduino. La prima scheda ha un bottone come input e comunica con un altra scheda che monta un LED come output. Il led della seconda si accende quando rileva diff --git a/serial/debug_preprocessor/debug_preprocessor.ino b/serial/debug_preprocessor/debug_preprocessor.ino new file mode 100644 index 0000000..978b97d --- /dev/null +++ b/serial/debug_preprocessor/debug_preprocessor.ino @@ -0,0 +1,48 @@ +/* + Debug con macro per il preprocessore + + Blink v1 + + Accensione e spegnimanto di un LED utilizzando variabili + per impostare la velocita' del lampeggio. + + Turns on an LED on for one second, then off for one second, repeatedly. + + This example code is in the public domain. + */ + +// Pin 13 has an LED connected on most Arduino boards. +// give it a name: +int led = 13; +int breve = 200; // Variabile richiambile nel corso dell'esecuzione + +#define DEBUG +// Debug +#ifdef DEBUG + #define DEBUG_PRINT(x) Serial.print (x) + #define DEBUG_PRINTDEC(x) Serial.print (x, DEC) + #define DEBUG_PRINTLN(x) Serial.println (x) +#else + #define DEBUG_PRINT(x) + #define DEBUG_PRINTDEC(x) + #define DEBUG_PRINTLN(x) +#endif + +// the setup routine runs once when you press reset: +void setup() { + // initialize the digital pin as an output. + pinMode(led, OUTPUT); + Serial.begin(9600); +} + +// the loop routine runs over and over again forever: +void loop() { + digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) + DEBUG_PRINTLN("Stato HIGHT"); + delay(breve); // wait for a second + digitalWrite(led, LOW); // turn the LED off by making the voltage LOW + DEBUG_PRINTLN("Stato LOW"); + delay(breve); // wait for a second +} + + -- 2.39.2