]> git.piffa.net Git - sketchbook_andrea/blob - serial/serial_comm_tx_debounce/serial_comm_tx_debounce.ino
08d03e38c9dceb9960877d2b6939ad47b7a12e4b
[sketchbook_andrea] / serial / serial_comm_tx_debounce / serial_comm_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
11 // Prima scheda: input
12 // PIN 1 = TX
13 int led = 13;
14 int input = 2;  // Questa e' la scheda con un input           
15 int statoAttuale;                        // variable for reading the pin status
16 int ultimoStato;                // variable to hold the last button state
17 long ultimoCambio = 0;  // Momento in cui e' stato attivato il PIN input
18 long debounceDelay = 100;    // Tempo di debounce
19
20 // the setup routine runs once when you press reset:
21 void setup() {                
22   // initialize the digital pin as an output.
23   pinMode(led, OUTPUT);       // Il PIN e' attivato come output
24   pinMode(input, INPUT_PULLUP);        // Il PIN e' attivato come output
25   
26   Serial.begin(9600); // Attiviamo la seriale
27 }
28
29 // the loop routine runs over and over again forever:
30 void loop() {
31   statoAttuale = digitalRead(input);      // read input value and store it in var
32
33   if (statoAttuale == LOW && statoAttuale != ultimoStato && millis() - ultimoCambio > debounceDelay ) {
34     // 
35     Serial.write(1); // Invia il dato via seriale
36     //Serial.write(49); // Invia il dato via seriale: 49 e' ASCII per 1
37     //Serial.print(1); // Invia il dato via seriale in ASCII 
38     
39      ultimoCambio = millis() ;    // Registra il tempo attuale
40   }
41   ultimoStato = statoAttuale;                 // save the new state in our variable
42   Serial.flush();
43 }
44
45 /* Domande
46 Una connessione analogica permette di passare un solo tipo di segnale
47 con eventuale modulazione (8bit in output da PWM e 10bit di scansione 
48 come analog input).
49
50 - Quanti tipi di dati permette di trasmettere la seriale?
51 - Comandare un LED RGB via PWM via seriale (da una Arduino o da un PC).
52 - Che caratteristiche di latenza si hanno rispetto 
53   a una connessione analogica?
54 - Rifare lo sketch utilizzando una STATE MACHINE: quando il
55   il LED viene ACCESO / SPENTO alternativamente alla pressione
56   del bottone.
57 - Dove dovremo implementare il DEBOUNCE?
58 - Dove implementare la gestione dello STATO?
59
60 HINT: Vedere lo script basc/buttons/debounce-2_and_contratto
61 */
62