4 Comunicazione I2C tra due schede Arduino.
5 La scheda master ha un bottone come input e
6 comunica con altre schede 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 Schema: http://lab.piffa.net/schemi/i2c_bb.jpg
18 int input = 2; // Questa e' la scheda con un input
19 int statoAttuale; // variable for reading the pin status
20 int ultimoStato; // variable to hold the last button state
21 long ultimoCambio = 0; // Momento in cui e' stato attivato il PIN input
22 long debounceDelay = 100; // Tempo di debounce
24 // the setup routine runs once when you press reset:
26 // initialize the digital pin as an output.
27 pinMode(led, OUTPUT); // Il PIN e' attivato come output
28 pinMode(input, INPUT_PULLUP); // Il PIN e' attivato come output
30 Serial.begin(9600); // Attiviamo la seriale per debug
32 Serial.println("Master Debug:");
34 // Serial.begin(9600); // Attiviamo la seriale per debug
35 Wire.begin(); // join i2c bus (address optional for master)
40 statoAttuale = digitalRead(input); // Legge lo stato del bottone
42 if (statoAttuale == LOW && statoAttuale != ultimoStato && millis() - ultimoCambio > debounceDelay ) {
44 Wire.beginTransmission(2); // Invia il messaggio al device slave con id 2
46 Wire.endTransmission(); // stop transmitting
49 ultimoCambio = millis() ; // Registra il tempo attuale
52 // Request per lo stato del LED
54 Wire.requestFrom(2,1);
55 while(Wire.available()) // slave may send less than requested
57 char statoRemoto = Wire.read(); // receive a byte as character
58 Serial.print("Stato del LED sul device 2: ");
59 Serial.println(statoRemoto,DEC); // print the character
62 ultimoStato = statoAttuale; // save the new state in our variable
70 1. Implementare piu' bottoni per controllare piu' schede
71 2. Isolare (per quanto possibile) la gestione di un BOTTONE-LED in una funzione
72 (es multitasking/BlinkWithoutDelay_3_funzione )
73 3. Costruire una classi (programmazione ad oggetti) per l'oggetto BOTTONE-LED
74 (es: multitasking/BlinkWithoutDelay_6_class/ )