From: Andrea Manni Date: Mon, 8 Jun 2015 15:48:54 +0000 (+0200) Subject: serial + i2c X-Git-Url: http://git.piffa.net/web?p=sketchbook_andrea;a=commitdiff_plain;h=ed1b9da5056321699fb9b693142df42befccca43 serial + i2c --- diff --git a/basic/serial_debug/debug_1_preprocessor_simple/debug_1_preprocessor_simple.ino b/basic/serial_debug/debug_1_preprocessor_simple/debug_1_preprocessor_simple.ino new file mode 100644 index 0000000..0461985 --- /dev/null +++ b/basic/serial_debug/debug_1_preprocessor_simple/debug_1_preprocessor_simple.ino @@ -0,0 +1,46 @@ +/* + 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 + + +// 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) +#ifdef DEBUG + Serial.println("High"); +#endif + + delay(breve); // wait for a second + digitalWrite(led, LOW); // turn the LED off by making the voltage LOW +#ifdef DEBUG + Serial.println("Low"); +#endif + delay(breve); // wait for a second +} + + + + diff --git a/basic/serial_debug/debug_2_preprocessor_advanced/debug_2_preprocessor_advanced.ino b/basic/serial_debug/debug_2_preprocessor_advanced/debug_2_preprocessor_advanced.ino new file mode 100644 index 0000000..2f28259 --- /dev/null +++ b/basic/serial_debug/debug_2_preprocessor_advanced/debug_2_preprocessor_advanced.ino @@ -0,0 +1,51 @@ +/* + 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 +/* A volte il codice di debug e' complicato e deve sostituire parte del codice +notmalmente usato. In questo modo potete specificare parte di codice +da eseguire in modalita' non-debug differente da quello di debug */ + #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 +} + + diff --git a/serial/analog_rx_1/analog_rx_1.ino b/serial/analog_rx_1/analog_rx_1.ino new file mode 100644 index 0000000..bc3de98 --- /dev/null +++ b/serial/analog_rx_1/analog_rx_1.ino @@ -0,0 +1,34 @@ +/* + Analog comm: RX + + 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 + la pressione del bottone della prima. + + Schema: http://lab.piffa.net/schemi/analog_io_bb.png + */ + +// Seconda scheda: output +int led = 13; // Questa scheda ha spolo l'output +int RX = 3 ; // Pin di ricezione + +void setup() { + // initialize the digital pin as an output. + pinMode(led, OUTPUT); // Il PIN e' attivato come output + pinMode(RX, INPUT); // Il PIN e' attivato come output +} + +// the loop routine runs over and over again forever: +void loop() { + if (digitalRead(RX) == 1) { // Verifica se il PIN input e' +5v + digitalWrite(led, HIGH); + delay(50); + } + else if (digitalRead(RX) == 0) { // Alterativa: se non e' +5v + digitalWrite(led, LOW); + delay(50); + } +} + diff --git a/serial/analog_tx_1/analog_tx_1.ino b/serial/analog_tx_1/analog_tx_1.ino new file mode 100644 index 0000000..9cb5498 --- /dev/null +++ b/serial/analog_tx_1/analog_tx_1.ino @@ -0,0 +1,41 @@ +/* + Analog comm: TX + + Comunicazione seriale 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 + la pressione del bottone della prima. + + Schema: http://lab.piffa.net/schemi/analog_io_bb.png + */ + +// Prima scheda: input +int led = 13; // Debug +int input = 2; // Questa e' la scheda con un input +int TX = 3 ; // Pin di trasmissione + +void setup() { + // initialize the digital pin as an output. + pinMode(led, OUTPUT); // Il PIN e' attivato come output per DEBUG + pinMode(TX, OUTPUT); // Il PIN e' attivato come output + pinMode(input, INPUT); // Il PIN e' attivato come output +} + +// 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); // Debug + digitalWrite(TX, HIGH); + delay(50); + } + else { // Alterativa: se non e' +5v + digitalWrite(led, LOW); + digitalWrite(TX, LOW); + delay(50); + } +} + + + + diff --git a/serial/comm_rx/comm_rx.ino b/serial/comm_rx/comm_rx.ino deleted file mode 100644 index eea5540..0000000 --- a/serial/comm_rx/comm_rx.ino +++ /dev/null @@ -1,32 +0,0 @@ -/* - Analog comm: RX - - 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 - la pressione del bottone della prima. - */ - -// Seconda scheda: output -int led = 13; // Questa scheda ha spolo l'output -int RX = 3 ; // Pin di ricezione - -void setup() { - // initialize the digital pin as an output. - pinMode(led, OUTPUT); // Il PIN e' attivato come output - pinMode(RX, INPUT); // Il PIN e' attivato come output -} - -// the loop routine runs over and over again forever: -void loop() { - if (digitalRead(RX) == 1) { // Verifica se il PIN input e' +5v - digitalWrite(led, HIGH); - delay(50); - } - else if (digitalRead(RX) == 0) { // Alterativa: se non e' +5v - digitalWrite(led, LOW); - delay(50); - } -} - diff --git a/serial/comm_tx/comm_tx.ino b/serial/comm_tx/comm_tx.ino deleted file mode 100644 index 2afb4e7..0000000 --- a/serial/comm_tx/comm_tx.ino +++ /dev/null @@ -1,39 +0,0 @@ -/* - Analog comm: TX - - Comunicazione seriale 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 - la pressione del bottone della prima. - */ - -// Prima scheda: input -int led = 13; // Debug -int input = 2; // Questa e' la scheda con un input -int TX = 3 ; // Pin di trasmissione - -void setup() { - // initialize the digital pin as an output. - pinMode(led, OUTPUT); // Il PIN e' attivato come output per DEBUG - pinMode(TX, OUTPUT); // Il PIN e' attivato come output - pinMode(input, INPUT); // Il PIN e' attivato come output -} - -// 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); // Debug - digitalWrite(TX, HIGH); - delay(50); - } - else { // Alterativa: se non e' +5v - digitalWrite(led, LOW); - digitalWrite(TX, LOW); - delay(50); - } -} - - - - diff --git a/serial/debug_1_preprocessor_simple/debug_1_preprocessor_simple.ino b/serial/debug_1_preprocessor_simple/debug_1_preprocessor_simple.ino deleted file mode 100644 index 0461985..0000000 --- a/serial/debug_1_preprocessor_simple/debug_1_preprocessor_simple.ino +++ /dev/null @@ -1,46 +0,0 @@ -/* - 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 - - -// 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) -#ifdef DEBUG - Serial.println("High"); -#endif - - delay(breve); // wait for a second - digitalWrite(led, LOW); // turn the LED off by making the voltage LOW -#ifdef DEBUG - Serial.println("Low"); -#endif - delay(breve); // wait for a second -} - - - - diff --git a/serial/debug_2_preprocessor_advanced/debug_2_preprocessor_advanced.ino b/serial/debug_2_preprocessor_advanced/debug_2_preprocessor_advanced.ino deleted file mode 100644 index 2f28259..0000000 --- a/serial/debug_2_preprocessor_advanced/debug_2_preprocessor_advanced.ino +++ /dev/null @@ -1,51 +0,0 @@ -/* - 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 -/* A volte il codice di debug e' complicato e deve sostituire parte del codice -notmalmente usato. In questo modo potete specificare parte di codice -da eseguire in modalita' non-debug differente da quello di debug */ - #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 -} - - diff --git a/serial/i2c/master_2_led/master_2_led.ino b/serial/i2c/master_2_led/master_2_led.ino new file mode 100644 index 0000000..f2ecdf8 --- /dev/null +++ b/serial/i2c/master_2_led/master_2_led.ino @@ -0,0 +1,74 @@ +/* + I2C comm: Master + + Comunicazione I2C tra due schede Arduino. + La scheda master ha un bottone come input e + comunica con altre schede che monta un LED come output. + Il led della seconda si accende quando rileva + la pressione del bottone della prima. + + Questo script implementa il debounce con Millis(), + */ + +#include + +int led = 13; +int input = 2; // Questa e' la scheda con un input +int statoAttuale; // variable for reading the pin status +int ultimoStato; // variable to hold the last button state +long ultimoCambio = 0; // Momento in cui e' stato attivato il PIN input +long debounceDelay = 100; // Tempo di debounce + +// 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_PULLUP); // Il PIN e' attivato come output + + // Serial.begin(9600); // Attiviamo la seriale per debug + Wire.begin(); // join i2c bus (address optional for master) +} + + +void loop() { + statoAttuale = digitalRead(input); // Legge lo stato del bottone + + if (statoAttuale == LOW && statoAttuale != ultimoStato && millis() - ultimoCambio > debounceDelay ) { + // Trasmissione I2C + Wire.beginTransmission(2); // Invia il messaggio al device slave con id 2 + Wire.write("1"); + Wire.endTransmission(); // stop transmitting + + // Gestone stato + ultimoCambio = millis() ; // Registra il tempo attuale + } + ultimoStato = statoAttuale; // save the new state in our variable +} + +/* Domande + + 1. Implementare piu' bottoni per controllare piu' schede + 2. Implementare un messaggio di feedback dallo slave al master per uk cambio di stato del led + + Links: + + + + + + + + + + + Risposte in fondo: + 1. Vedere i links proposti: .write scrive un byte mentre + .print manda un codice ASCII + 2. 49 + 3. No, dato solo il cavo dalla TX alla RX . + Provare a fare un script con comunicazione bilaterale + 4. No perche' non ci sarebbe sincronia tra i segnali, + vedere gli esercizi su I2C + */ + + diff --git a/serial/i2c/master_3_feedback/master_3_feedback.ino b/serial/i2c/master_3_feedback/master_3_feedback.ino new file mode 100644 index 0000000..877ffd4 --- /dev/null +++ b/serial/i2c/master_3_feedback/master_3_feedback.ino @@ -0,0 +1,81 @@ +/* + I2C comm: Master + + Comunicazione I2C tra due schede Arduino. + La scheda master ha un bottone come input e + comunica con altre schede che monta un LED come output. + Il led della seconda si accende quando rileva + la pressione del bottone della prima. + + Questo script implementa il debounce con Millis() + + Schema: http://lab.piffa.net/schemi/i2c_bb.jpg + */ + +#include + +int led = 13; +int input = 2; // Questa e' la scheda con un input +int statoAttuale; // variable for reading the pin status +int ultimoStato; // variable to hold the last button state +long ultimoCambio = 0; // Momento in cui e' stato attivato il PIN input +long debounceDelay = 100; // Tempo di debounce + +// 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_PULLUP); // Il PIN e' attivato come output + + Serial.begin(9600); // Attiviamo la seriale per debug + Serial.flush(); + Serial.println("Master Debug:"); + + // Serial.begin(9600); // Attiviamo la seriale per debug + Wire.begin(); // join i2c bus (address optional for master) +} + + +void loop() { + statoAttuale = digitalRead(input); // Legge lo stato del bottone + + if (statoAttuale == LOW && statoAttuale != ultimoStato && millis() - ultimoCambio > debounceDelay ) { + // Trasmissione I2C + Wire.beginTransmission(2); // Invia il messaggio al device slave con id 2 + Wire.write("1"); + Wire.endTransmission(); // stop transmitting + + // Gestone stato + ultimoCambio = millis() ; // Registra il tempo attuale + + + // Request per lo stato del LED + delay(100); + Wire.requestFrom(2,1); + while(Wire.available()) // slave may send less than requested + { + char statoRemoto = Wire.read(); // receive a byte as character + Serial.print("Stato del LED sul device 2: "); + Serial.println(statoRemoto,DEC); // print the character + } + } + ultimoStato = statoAttuale; // save the new state in our variable + + + +} + +/* Domande + + 1. Implementare piu' bottoni per controllare piu' schede + 2. Isolare (per quanto possibile) la gestione di un BOTTONE-LED in una funzione + (es multitasking/BlinkWithoutDelay_3_funzione ) + 3. Costruire una classi (programmazione ad oggetti) per l'oggetto BOTTONE-LED + (es: multitasking/BlinkWithoutDelay_6_class/ ) + + */ + + + + + diff --git a/serial/i2c/master_writer_1/master_writer_1.ino b/serial/i2c/master_writer_1/master_writer_1.ino new file mode 100644 index 0000000..4692b52 --- /dev/null +++ b/serial/i2c/master_writer_1/master_writer_1.ino @@ -0,0 +1,37 @@ +/* Wire Master Writer + +Invio dati via I2C + + Schema: http://lab.piffa.net/schemi/i2c_bb.jpg + This example code is in the public domain. + +*/ + + +#include + +void setup() +{ + Wire.begin(); // join i2c bus (address optional for master) +} + +byte x = 0; + +void loop() +{ + // Wire.beginTransmission(4); // transmit to device #4 + // Wire.write("DAto inviato dal master"); // sends five bytes + // + // Wire.endTransmission(); // stop transmitting + // + // + // delay(500); + Wire.beginTransmission(4); // transmit to device #4 + Wire.write("Tora Tora!"); // sends five bytes + // sends one byte + Wire.endTransmission(); // stop transmitting + + + delay(500); +} + diff --git a/serial/i2c/slave_2_state/slave_2_state.ino b/serial/i2c/slave_2_state/slave_2_state.ino new file mode 100644 index 0000000..53a3ed9 --- /dev/null +++ b/serial/i2c/slave_2_state/slave_2_state.ino @@ -0,0 +1,70 @@ +/* + I2C comm: Slave LED + + Comunicazione I2C tra due schede Arduino. + La scheda master ha un bottone come input e + comunica con altre schede che monta un LED come output. + Il led della seconda si accende quando rileva + la pressione del bottone della prima. + + Schema: http://lab.piffa.net/schemi/i2c_bb.jpg + */ + +#include +// slave +// PIN 0 = RX +int led = 13; // Questa scheda ha spolo l'output +int ledState = 0; // stato attuale del LED +int incomingByte; // Dato ricevuto via seriale + + +// 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 + + Serial.begin(9600); // Attiviamo la seriale per debug + Serial.flush(); + Serial.println("Slave / RX Debug:"); + + Wire.begin(2); // join i2c bus (address optional for master) + Wire.onReceive(receiveEvent); // Evento in ricezione +} + + +void loop() { + digitalWrite(led, ledState); // Aggiorna lo stato del LED + delay(200); +} + +/// Funzioni +void receiveEvent(int howMany) +{ + Serial.print("Lo slave ha ricevuto il seguente messaggio: "); + while( Wire.available()) // loop through all but the last + { + incomingByte = Wire.read(); // receive byte as a character + Serial.print(incomingByte, DEC); // print the character + + if (incomingByte == 49) { // Verifica se viene ricevuto il dato 1 + ledState = !ledState ; // Cambia lo stato del LED + } + Serial.println(""); + } +} + +/* Domande + + 1. Implementare piu' bottoni per controllare piu' schede + 2. Implementare un messaggio di feedback dallo slave al master per uk cambio di stato del led + + Links: + + */ + + + + + + + diff --git a/serial/i2c/slave_3_feedback/slave_3_feedback.ino b/serial/i2c/slave_3_feedback/slave_3_feedback.ino new file mode 100644 index 0000000..6d01e58 --- /dev/null +++ b/serial/i2c/slave_3_feedback/slave_3_feedback.ino @@ -0,0 +1,81 @@ +/* + I2C comm: Slave LED + + Comunicazione I2C tra due schede Arduino. + La scheda master ha un bottone come input e + comunica con altre schede che monta un LED come output. + Il led della seconda si accende quando rileva + la pressione del bottone della prima. + + Schema: http://lab.piffa.net/schemi/i2c_bb.jpg + */ + +#include +// slave +// PIN 0 = RX +int led = 13; // Questa scheda ha spolo l'output +int ledState = 0; // stato attuale del LED +int incomingByte; // Dato ricevuto via seriale + + +// 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 + + Serial.begin(9600); // Attiviamo la seriale per debug + Serial.flush(); + Serial.println("Slave / RX Debug:"); + + Wire.begin(2); // join i2c bus (address optional for master) + Wire.onReceive(receiveEvent); // Evento in ricezione dati + Wire.onRequest(requestCallback); // Evento su richiesta +} + + +void loop() { + digitalWrite(led, ledState); // Aggiorna lo stato del LED + delay(200); +} + +/// Funzioni +void receiveEvent(int howMany) +{ + Serial.print("Lo slave ha ricevuto il seguente messaggio: "); + while( Wire.available()) // loop through all but the last + { + incomingByte = Wire.read(); // receive byte as a character + Serial.print(incomingByte, DEC); // print the character + + if (incomingByte == 49) { // Verifica se viene ricevuto il dato 1 + ledState = !ledState ; // Cambia lo stato del LED + } + Serial.println(""); + } +} + +void requestCallback() +{ + // Rimanda lo stato del LED + Serial.println("Comunicato lo stato del LED a MASTER"); // Debug + + Wire.write(ledState); +} + +/* Domande + + 1. Implementare piu' bottoni per controllare piu' schede + 2. Isolare (per quanto possibile) la gestione di un BOTTONE-LED in una funzione + (es multitasking/BlinkWithoutDelay_3_funzione ) + 3. Costruire una classi (programmazione ad oggetti) per l'oggetto BOTTONE-LED + (es: multitasking/BlinkWithoutDelay_6_class/ ) + + */ + + + + + + + + diff --git a/serial/i2c/slave_receiver_1/slave_receiver_1.ino b/serial/i2c/slave_receiver_1/slave_receiver_1.ino new file mode 100644 index 0000000..34040d6 --- /dev/null +++ b/serial/i2c/slave_receiver_1/slave_receiver_1.ino @@ -0,0 +1,45 @@ +/* Wire Slave Receiver + +Ricezine di una stringa di testo via I2C + + This example code is in the public domain. + + Schema: http://lab.piffa.net/schemi/i2c_bb.jpg +*/ + +char input ; + +#include + +void setup() +{ + Wire.begin(4); // join i2c bus with address #4 + Wire.onReceive(receiveEvent); // register event + + // Debug seriale + Serial.begin(9600); // start serial for output + Serial.println("Slave / RX Debug:"); + Serial.flush(); +} + +void loop() +{ + + delay(100); +// Nel Loop non succede niente, tutta l'azione e nella funzione receiveEvent() +// Innescata dall'evento Wire.onReceive +} + +// function that executes whenever data is received from master +// this function is registered as an event, see setup() +void receiveEvent(int howMany) +{ + Serial.print("Lo slave ha ricevuto il seguente messaggio: "); + while( Wire.available()) // loop through all but the last + { + input = Wire.read(); // receive byte as a character + Serial.print(input); // print the character + } // print the integer + Serial.println(""); +} + diff --git a/serial/serial_2_comm_rx_2_led/serial_2_comm_rx_2_led.ino b/serial/serial_2_comm_rx_2_led/serial_2_comm_rx_2_led.ino deleted file mode 100644 index 286e489..0000000 --- a/serial/serial_2_comm_rx_2_led/serial_2_comm_rx_2_led.ino +++ /dev/null @@ -1,46 +0,0 @@ -/* - Serial comm: RX due output - - Comunicazione seriale tra due schede arduino. - La prima scheda ha due botton1 come input e - comunica con un altra scheda che monta due LED come output. - Il led della seconda si accende quando rileva - la pressione del bottone della prima. - - La RX deve distinguere l'input ricevuto. - */ - -// Seconda scheda: output -// PIN 0 = RX -int led = 13; // Questa scheda ha spolo l'output -int red = 12; // Questa scheda ha spolo l'output -int incomingByte ; -// 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(red, OUTPUT); // Il PIN e' attivato come output - Serial.begin(9600); // Attiviamo la seriale -} - -// the loop routine runs over and over again forever: -void loop() { - if (Serial.available() > 0) { - // read the incoming byte: - incomingByte = Serial.read(); - - if (incomingByte == 2) { // Verifica se il PIN input e' +5v - digitalWrite(led, HIGH); - delay(50); - digitalWrite(led, LOW); - } - else if (incomingByte == 3) { // Alterativa: se non e' +5v - digitalWrite(red, HIGH); - delay(50); - digitalWrite(red, LOW); - } - } -} - - - diff --git a/serial/serial_2_comm_tx_2_input/serial_2_comm_tx_2_input.ino b/serial/serial_2_comm_tx_2_input/serial_2_comm_tx_2_input.ino deleted file mode 100644 index e33835f..0000000 --- a/serial/serial_2_comm_tx_2_input/serial_2_comm_tx_2_input.ino +++ /dev/null @@ -1,44 +0,0 @@ -/* - Serial comm: TX due bottoni - - Comunicazione seriale tra due schede arduino. - La prima scheda ha due bottoni come input e - comunica con un altra scheda che monta due LED come output. - Il led della seconda si accende quando rileva - la pressione del bottone della prima. - - La TX deve mandare due segnali diversi. - */ - -// Prima scheda: input -// PIN 1 = TX -int led = 13; -int input = 2; // Questa e' la scheda con un input -int inputRed = 3; // Questa e' la scheda con un input - -// 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); // Attiviamo la 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.write(2); // Indicatore per accendere il LED 2 - delay(50); - } - if (digitalRead(inputRed) == HIGH) { // Verifica se il PIN input e' +5v - digitalWrite(led, HIGH); - Serial.write(3); // Idicatore per accendere il LED 3 - delay(50); - } - digitalWrite(led, LOW); -} - - - diff --git a/serial/serial_2_rx/serial_2_rx.ino b/serial/serial_2_rx/serial_2_rx.ino new file mode 100644 index 0000000..4f9feec --- /dev/null +++ b/serial/serial_2_rx/serial_2_rx.ino @@ -0,0 +1,84 @@ +/* + Serial comm: RX + + Comunicazione seriale 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 + la pressione del bottone della prima. + + Scema: http://lab.piffa.net/schemi/serial_common_bb.png + http://lab.piffa.net/schemi/serial_common_schem.png + */ + +// Seconda scheda: output +// PIN 0 = RX +int led = 13; // Questa scheda ha spolo l'output + +// 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 + + Serial.begin(9600); // Attiviamo la seriale +} + +// the loop routine runs over and over again forever: +void loop() { + if (Serial.read() == 1) { // Verifica se il PIN input e' +5v + digitalWrite(led, HIGH); + delay(50); + } + else if (Serial.read() == 0) { // Alterativa: se non e' +5v + digitalWrite(led, LOW); + delay(50); + } +} + +/* Domande +Una connessione analogica permette di passare un solo tipo di segnale +con eventuale modulazione (8bit in output da PWM e 10bit di scansione +come analog input). + +1. Quanti tipi di dati permette di trasmettere la seriale? +2 Comandare un LED RGB via PWM via seriale (da una Arduino o da un PC). +2.1 Come si comportano le latenze? +2.2 C'e' perdita di segnale? +3. Rifare lo sketch utilizzando una STATE MACHINE: quando il + il LED viene ACCESO / SPENTO alternativamente alla pressione + del bottone. + - Dove dovremo implementare il DEBOUNCE? + - Dove implementare la gestione dello STATO? + +Risposte in fondo: + + + + + + + + + + + + + + + + + +1. Qualunque tipo di dati, anche lunghi flussi di dati +2. Si dovra' trasmettere un segnale da 0-255 e sulla ricevente + interpretarlo con http://www.arduino.cc/en/Reference/ParseInt +2.1 Tra buffer di trasmissione, timeout e conversione dei valori + ci sara' una latenza nel trasferimento dei dati, + a differenza di una connessione analigica che e' instantanea + ma soggetta a degradazione del segnale. +2.2 Non c'e' degradazione o perdita del segnale essendo una +trasmissione di dati digitale (fin tanto che non si deteriora +la connessione). +3. Vedere lo sketch sucessivo, basandosi su gli sketch della + state machine per il bottne: basic/buttons/button_state_4_state_and_condition/ + +*/ diff --git a/serial/serial_2_tx/serial_2_tx.ino b/serial/serial_2_tx/serial_2_tx.ino new file mode 100644 index 0000000..08a59c0 --- /dev/null +++ b/serial/serial_2_tx/serial_2_tx.ino @@ -0,0 +1,89 @@ +/* + Serial comm: TX + + Comunicazione seriale 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 + la pressione del bottone della prima. + + Scema: http://lab.piffa.net/schemi/serial_common_bb.png + http://lab.piffa.net/schemi/serial_common_schem.png + */ + +// Prima scheda: input +// PIN 1 = TX +int led = 13; +int input = 2; // Questa e' la scheda con un input + +// 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_PULLUP); // Il PIN e' attivato come output + + Serial.begin(9600); // Attiviamo la seriale +} + +// the loop routine runs over and over again forever: +void loop() { + if (digitalRead(input) == LOW) { // Verifica se il bottone e' premuto + digitalWrite(led, HIGH); + Serial.write(1); + delay(50); + } + else { // Alterativa: se non e' +5v + digitalWrite(led, LOW); + Serial.write(0); + delay(50); + } +} + +/* Domande +Una connessione analogica permette di passare un solo tipo di segnale +con eventuale modulazione (8bit in output da PWM e 10bit di scansione +come analog input). + +1. Quanti tipi di dati permette di trasmettere la seriale? +2 Comandare un LED RGB via PWM via seriale (da una Arduino o da un PC). +2.1 Come si comportano le latenze? +2.2 C'e' perdita di segnale? +3. Rifare lo sketch utilizzando una STATE MACHINE: quando il + il LED viene ACCESO / SPENTO alternativamente alla pressione + del bottone. + - Dove dovremo implementare il DEBOUNCE? + - Dove implementare la gestione dello STATO? + +Risposte in fondo: + + + + + + + + + + + + + + + + + +1. Qualunque tipo di dati, anche lunghi flussi di dati +2. Si dovra' trasmettere un segnale da 0-255 e sulla ricevente + interpretarlo con http://www.arduino.cc/en/Reference/ParseInt +2.1 Tra buffer di trasmissione, timeout e conversione dei valori + ci sara' una latenza nel trasferimento dei dati, + a differenza di una connessione analigica che e' instantanea + ma soggetta a degradazione del segnale. +2.2 Non c'e' degradazione o perdita del segnale essendo una +trasmissione di dati digitale (fin tanto che non si deteriora +la connessione). +3. Vedere lo sketch sucessivo, basandosi su gli sketch della + state machine per il bottne: basic/buttons/button_state_4_state_and_condition/ + +*// + diff --git a/serial/serial_3_rx_state/serial_3_rx_state.ino b/serial/serial_3_rx_state/serial_3_rx_state.ino new file mode 100644 index 0000000..6422c40 --- /dev/null +++ b/serial/serial_3_rx_state/serial_3_rx_state.ino @@ -0,0 +1,77 @@ +/* + Serial comm: RX + + Comunicazione seriale 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 + la pressione del bottone della prima. + + Scema: http://lab.piffa.net/schemi/serial_common_bb.png + http://lab.piffa.net/schemi/serial_common_schem.png + */ + + +// Seconda scheda: output +// PIN 0 = RX +int led = 13; // Questa scheda ha spolo l'output +int ledState = 0; // stato attuale del LED +int incomingByte; // Dato ricevuto via seriale + + +// 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 + + Serial.begin(9600); // Attiviamo la seriale +} + +// the loop routine runs over and over again forever: +void loop() { + if (Serial.available() > 0) { +incomingByte = Serial.read(); + if (incomingByte == 1) { // Verifica se viene ricevuto il dato 1 + ledState = !ledState ; // Cambia lo stato del LED + + // debug + Serial.print("I received: "); + Serial.println(incomingByte); + } + } + digitalWrite(led, ledState); // Aggiorna lo stato del LED +} + +/* Domande + +1. Che differenza c'e' tra Serial.write() e Serial.print()? +2. Qual'e' il codice ASCII per indicare il numero decimale 1? +3. Servono entrambi i cavi per la connessione? +4. Potrei attaccare una terza arduino? + +Links: +- http://www.arduino.cc/en/Serial/Print +- http://www.arduino.cc/en/Tutorial/ASCIITable + + + + + + + + + + +Risposte in fondo: +1. Vedere i links proposti: .write scrive un byte mentre + .print manda un codice ASCII +2. 49 +3. No, dato solo il cavo dalla TX alla RX . + Provare a fare un script con comunicazione bilaterale +4. No perche' non ci sarebbe sincronia tra i segnali, + vedere gli esercizi su I2C +*/ + + + + diff --git a/serial/serial_3_tx_debounce/serial_3_tx_debounce.ino b/serial/serial_3_tx_debounce/serial_3_tx_debounce.ino new file mode 100644 index 0000000..d6f431d --- /dev/null +++ b/serial/serial_3_tx_debounce/serial_3_tx_debounce.ino @@ -0,0 +1,79 @@ +/* + Serial comm: TX + + Comunicazione seriale 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 + la pressione del bottone della prima. + + Questo script implementa il debounce con Millis(), + + Scema: http://lab.piffa.net/schemi/serial_common_bb.png + http://lab.piffa.net/schemi/serial_common_schem.png + */ + +// Prima scheda: input +// PIN 1 = TX +int led = 13; +int input = 2; // Questa e' la scheda con un input +int statoAttuale; // variable for reading the pin status +int ultimoStato; // variable to hold the last button state +long ultimoCambio = 0; // Momento in cui e' stato attivato il PIN input +long debounceDelay = 100; // Tempo di debounce + +// 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_PULLUP); // Il PIN e' attivato come output + + Serial.begin(9600); // Attiviamo la seriale +} + +// the loop routine runs over and over again forever: +void loop() { + statoAttuale = digitalRead(input); // read input value and store it in var + + if (statoAttuale == LOW && statoAttuale != ultimoStato && millis() - ultimoCambio > debounceDelay ) { + // + Serial.write(1); // Invia il dato via seriale + //Serial.write(49); // Invia il dato via seriale: 49 e' ASCII per 1 + //Serial.print(1); // Invia il dato via seriale in ASCII + + ultimoCambio = millis() ; // Registra il tempo attuale + } + ultimoStato = statoAttuale; // save the new state in our variable + Serial.flush(); +} + +/* Domande + +1. Che differenza c'e' tra Serial.write() e Serial.print()? +2. Qual'e' il codice ASCII per indicare il numero decimale 1? +3. Servono entrambi i cavi per la connessione? +4. Potrei attaccare una terza arduino? + +Links: +- http://www.arduino.cc/en/Serial/Print +- http://www.arduino.cc/en/Tutorial/ASCIITable + + + + + + + + + + +Risposte in fondo: +1. Vedere i links proposti: .write scrive un byte mentre + .print manda un codice ASCII +2. 49 +3. No, dato solo il cavo dalla TX alla RX . + Provare a fare un script con comunicazione bilaterale +4. No perche' non ci sarebbe sincronia tra i segnali, + vedere gli esercizi su I2C +*/ + diff --git a/serial/serial_4_rx_2_led/serial_4_rx_2_led.ino b/serial/serial_4_rx_2_led/serial_4_rx_2_led.ino new file mode 100644 index 0000000..286e489 --- /dev/null +++ b/serial/serial_4_rx_2_led/serial_4_rx_2_led.ino @@ -0,0 +1,46 @@ +/* + Serial comm: RX due output + + Comunicazione seriale tra due schede arduino. + La prima scheda ha due botton1 come input e + comunica con un altra scheda che monta due LED come output. + Il led della seconda si accende quando rileva + la pressione del bottone della prima. + + La RX deve distinguere l'input ricevuto. + */ + +// Seconda scheda: output +// PIN 0 = RX +int led = 13; // Questa scheda ha spolo l'output +int red = 12; // Questa scheda ha spolo l'output +int incomingByte ; +// 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(red, OUTPUT); // Il PIN e' attivato come output + Serial.begin(9600); // Attiviamo la seriale +} + +// the loop routine runs over and over again forever: +void loop() { + if (Serial.available() > 0) { + // read the incoming byte: + incomingByte = Serial.read(); + + if (incomingByte == 2) { // Verifica se il PIN input e' +5v + digitalWrite(led, HIGH); + delay(50); + digitalWrite(led, LOW); + } + else if (incomingByte == 3) { // Alterativa: se non e' +5v + digitalWrite(red, HIGH); + delay(50); + digitalWrite(red, LOW); + } + } +} + + + diff --git a/serial/serial_4_tx_2_input/serial_4_tx_2_input.ino b/serial/serial_4_tx_2_input/serial_4_tx_2_input.ino new file mode 100644 index 0000000..e33835f --- /dev/null +++ b/serial/serial_4_tx_2_input/serial_4_tx_2_input.ino @@ -0,0 +1,44 @@ +/* + Serial comm: TX due bottoni + + Comunicazione seriale tra due schede arduino. + La prima scheda ha due bottoni come input e + comunica con un altra scheda che monta due LED come output. + Il led della seconda si accende quando rileva + la pressione del bottone della prima. + + La TX deve mandare due segnali diversi. + */ + +// Prima scheda: input +// PIN 1 = TX +int led = 13; +int input = 2; // Questa e' la scheda con un input +int inputRed = 3; // Questa e' la scheda con un input + +// 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); // Attiviamo la 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.write(2); // Indicatore per accendere il LED 2 + delay(50); + } + if (digitalRead(inputRed) == HIGH) { // Verifica se il PIN input e' +5v + digitalWrite(led, HIGH); + Serial.write(3); // Idicatore per accendere il LED 3 + delay(50); + } + digitalWrite(led, LOW); +} + + + diff --git a/serial/serial_comm_rx/serial_comm_rx.ino b/serial/serial_comm_rx/serial_comm_rx.ino deleted file mode 100644 index 1dd67db..0000000 --- a/serial/serial_comm_rx/serial_comm_rx.ino +++ /dev/null @@ -1,42 +0,0 @@ -/* - Serial comm: RX - - Comunicazione seriale 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 - la pressione del bottone della prima. - */ - -// Seconda scheda: output -// PIN 0 = RX -int led = 13; // Questa scheda ha spolo l'output - -// 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 - - Serial.begin(9600); // Attiviamo la seriale -} - -// the loop routine runs over and over again forever: -void loop() { - if (Serial.read() == 1) { // Verifica se il PIN input e' +5v - digitalWrite(led, HIGH); - delay(50); - } - else if (Serial.read() == 0) { // Alterativa: se non e' +5v - digitalWrite(led, LOW); - delay(50); - } -} - -/* Domande -Una connessione analogica permette di passare un solo tipo di segnale -con eventuale modulazione (8bit in output da PWM e 10bit di scansione -come analog input). - -- Quanti tipi di dati permette di trasmettere la seriale? -- Comandare un LED RGB via PWM via seriale (da una Arduino o da un PC). -*/ diff --git a/serial/serial_comm_rx_state/serial_comm_rx_state.ino b/serial/serial_comm_rx_state/serial_comm_rx_state.ino deleted file mode 100644 index 697a39d..0000000 --- a/serial/serial_comm_rx_state/serial_comm_rx_state.ino +++ /dev/null @@ -1,58 +0,0 @@ -/* - Serial comm: RX - - Comunicazione seriale 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 - la pressione del bottone della prima. - */ - -// Seconda scheda: output -// PIN 0 = RX -int led = 13; // Questa scheda ha spolo l'output -int ledState = 0; // stato attuale del LED -int incomingByte; // Dato ricevuto via seriale - - -// 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 - - Serial.begin(9600); // Attiviamo la seriale -} - -// the loop routine runs over and over again forever: -void loop() { - if (Serial.available() > 0) { -incomingByte = Serial.read(); - if (incomingByte == 1) { // Verifica se viene ricevuto il dato 1 - ledState = !ledState ; // Cambia lo stato del LED - - // debug - Serial.print("I received: "); - Serial.println(incomingByte); - } - } - digitalWrite(led, ledState); // Aggiorna lo stato del LED -} - -/* Domande - Una connessione analogica permette di passare un solo tipo di segnale - con eventuale modulazione (8bit in output da PWM e 10bit di scansione - come analog input). - - - Quanti tipi di dati permette di trasmettere la seriale? - - Comandare un LED RGB via PWM via seriale (da una Arduino o da un PC). - - Che caratteristiche di latenza si hanno rispetto - a una connessione analogica? - - Rifare lo sketch utilizzando una STATE MACHINE: quando il - il LED viene ACCESO / SPENTO alternativamente alla pressione - del bottone. - - Dove dovremo implementare il DEBOUNCE? - - Dove implementare la gestione dello STATO? - */ - - - diff --git a/serial/serial_comm_tx/serial_comm_tx.ino b/serial/serial_comm_tx/serial_comm_tx.ino deleted file mode 100644 index e91df22..0000000 --- a/serial/serial_comm_tx/serial_comm_tx.ino +++ /dev/null @@ -1,52 +0,0 @@ -/* - Serial comm: TX - - Comunicazione seriale 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 - la pressione del bottone della prima. - */ - -// Prima scheda: input -// PIN 1 = TX -int led = 13; -int input = 2; // Questa e' la scheda con un input - -// 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_PULLUP); // Il PIN e' attivato come output - - Serial.begin(9600); // Attiviamo la seriale -} - -// the loop routine runs over and over again forever: -void loop() { - if (digitalRead(input) == LOW) { // Verifica se il bottone e' premuto - digitalWrite(led, HIGH); - Serial.write(1); - delay(50); - } - else { // Alterativa: se non e' +5v - digitalWrite(led, LOW); - Serial.write(0); - delay(50); - } -} - -/* Domande -Una connessione analogica permette di passare un solo tipo di segnale -con eventuale modulazione (8bit in output da PWM e 10bit di scansione -come analog input). - -- Quanti tipi di dati permette di trasmettere la seriale? -- Comandare un LED RGB via PWM via seriale (da una Arduino o da un PC). -- Che caratteristiche di latenza si hanno rispetto - a una connessione analogica? -- Rifare lo sketch utilizzando una STATE MACHINE: quando il - il LED viene ACCESO / SPENTO alternativamente alla pressione - del bottone. -*/ - diff --git a/serial/serial_comm_tx_debounce/serial_comm_tx_debounce.ino b/serial/serial_comm_tx_debounce/serial_comm_tx_debounce.ino deleted file mode 100644 index 08d03e3..0000000 --- a/serial/serial_comm_tx_debounce/serial_comm_tx_debounce.ino +++ /dev/null @@ -1,62 +0,0 @@ -/* - Serial comm: TX - - Comunicazione seriale 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 - la pressione del bottone della prima. - */ - -// Prima scheda: input -// PIN 1 = TX -int led = 13; -int input = 2; // Questa e' la scheda con un input -int statoAttuale; // variable for reading the pin status -int ultimoStato; // variable to hold the last button state -long ultimoCambio = 0; // Momento in cui e' stato attivato il PIN input -long debounceDelay = 100; // Tempo di debounce - -// 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_PULLUP); // Il PIN e' attivato come output - - Serial.begin(9600); // Attiviamo la seriale -} - -// the loop routine runs over and over again forever: -void loop() { - statoAttuale = digitalRead(input); // read input value and store it in var - - if (statoAttuale == LOW && statoAttuale != ultimoStato && millis() - ultimoCambio > debounceDelay ) { - // - Serial.write(1); // Invia il dato via seriale - //Serial.write(49); // Invia il dato via seriale: 49 e' ASCII per 1 - //Serial.print(1); // Invia il dato via seriale in ASCII - - ultimoCambio = millis() ; // Registra il tempo attuale - } - ultimoStato = statoAttuale; // save the new state in our variable - Serial.flush(); -} - -/* Domande -Una connessione analogica permette di passare un solo tipo di segnale -con eventuale modulazione (8bit in output da PWM e 10bit di scansione -come analog input). - -- Quanti tipi di dati permette di trasmettere la seriale? -- Comandare un LED RGB via PWM via seriale (da una Arduino o da un PC). -- Che caratteristiche di latenza si hanno rispetto - a una connessione analogica? -- Rifare lo sketch utilizzando una STATE MACHINE: quando il - il LED viene ACCESO / SPENTO alternativamente alla pressione - del bottone. -- Dove dovremo implementare il DEBOUNCE? -- Dove implementare la gestione dello STATO? - -HINT: Vedere lo script basc/buttons/debounce-2_and_contratto -*/ -