]> git.piffa.net Git - sketchbook_andrea/blob - serial/serial_3_tx_debounce/serial_3_tx_debounce.ino
serial + i2c
[sketchbook_andrea] / serial / serial_3_tx_debounce / serial_3_tx_debounce.ino
1 /*
2   Serial comm: TX
3  
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.
9  
10  Questo script implementa il debounce con Millis(),
11  
12  Scema: http://lab.piffa.net/schemi/serial_common_bb.png
13         http://lab.piffa.net/schemi/serial_common_schem.png
14  */
15
16 // Prima scheda: input
17 // PIN 1 = TX
18 int led = 13;
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
24
25 // the setup routine runs once when you press reset:
26 void setup() {                
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
30   
31   Serial.begin(9600); // Attiviamo la seriale
32 }
33
34 // the loop routine runs over and over again forever:
35 void loop() {
36   statoAttuale = digitalRead(input);      // read input value and store it in var
37
38   if (statoAttuale == LOW && statoAttuale != ultimoStato && millis() - ultimoCambio > debounceDelay ) {
39     // 
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 
43     
44      ultimoCambio = millis() ;    // Registra il tempo attuale
45   }
46   ultimoStato = statoAttuale;                 // save the new state in our variable
47   Serial.flush();
48 }
49
50 /* Domande
51
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?
56
57 Links:
58 - http://www.arduino.cc/en/Serial/Print
59 - http://www.arduino.cc/en/Tutorial/ASCIITable
60
61
62
63
64
65
66
67
68
69
70 Risposte in fondo:
71 1. Vedere i links proposti: .write scrive un byte mentre
72    .print manda un codice ASCII
73 2. 49
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
78 */
79