4 Comunicazione seriale tra due schede arduino.
5 La prima scheda ha un bottone come input e
6 comunica con un altra scheda che monta un LED come output.
7 Il led della seconda si accende quando rileva
8 la pressione del bottone della prima.
10 Questo script implementa il debounce con Millis(),
12 Scema: http://lab.piffa.net/schemi/serial_common_bb.png
13 http://lab.piffa.net/schemi/serial_common_schem.png
16 // Prima scheda: input
19 int input = 2; // Questa e' la scheda con un input
20 int statoAttuale; // variable for reading the pin status
21 int ultimoStato; // variable to hold the last button state
22 long ultimoCambio = 0; // Momento in cui e' stato attivato il PIN input
23 long debounceDelay = 100; // Tempo di debounce
25 // the setup routine runs once when you press reset:
27 // initialize the digital pin as an output.
28 pinMode(led, OUTPUT); // Il PIN e' attivato come output
29 pinMode(input, INPUT_PULLUP); // Il PIN e' attivato come output
31 Serial.begin(9600); // Attiviamo la seriale
34 // the loop routine runs over and over again forever:
36 statoAttuale = digitalRead(input); // read input value and store it in var
38 if (statoAttuale == LOW && statoAttuale != ultimoStato && millis() - ultimoCambio > debounceDelay ) {
40 Serial.write(1); // Invia il dato via seriale
41 //Serial.write(49); // Invia il dato via seriale: 49 e' ASCII per 1
42 //Serial.print(1); // Invia il dato via seriale in ASCII
44 ultimoCambio = millis() ; // Registra il tempo attuale
46 ultimoStato = statoAttuale; // save the new state in our variable
52 1. Che differenza c'e' tra Serial.write() e Serial.print()?
53 2. Qual'e' il codice ASCII per indicare il numero decimale 1?
54 3. Servono entrambi i cavi per la connessione?
55 4. Potrei attaccare una terza arduino?
58 - http://www.arduino.cc/en/Serial/Print
59 - http://www.arduino.cc/en/Tutorial/ASCIITable
71 1. Vedere i links proposti: .write scrive un byte mentre
72 .print manda un codice ASCII
74 3. No, dato solo il cavo dalla TX alla RX .
75 Provare a fare un script con comunicazione bilaterale
76 4. No perche' non ci sarebbe sincronia tra i segnali,
77 vedere gli esercizi su I2C