]> git.piffa.net Git - sketchbook_andrea/blob - serial/i2c/slave_receiver_1/slave_receiver_1.ino
34040d6954b53dabf20b1a3d0abc747b628dc374
[sketchbook_andrea] / serial / i2c / slave_receiver_1 / slave_receiver_1.ino
1 /* Wire Slave Receiver
2
3 Ricezine di una stringa di testo via I2C
4
5  This example code is in the public domain.
6  
7   Schema: http://lab.piffa.net/schemi/i2c_bb.jpg
8 */
9
10 char input ;
11
12 #include <Wire.h>
13
14 void setup()
15 {
16   Wire.begin(4);                // join i2c bus with address #4
17   Wire.onReceive(receiveEvent); // register event
18   
19   // Debug seriale
20   Serial.begin(9600);           // start serial for output
21   Serial.println("Slave / RX Debug:");
22   Serial.flush();
23 }
24
25 void loop()
26 {
27
28   delay(100);
29 // Nel Loop non succede niente, tutta l'azione e nella funzione receiveEvent()
30 // Innescata dall'evento Wire.onReceive
31 }
32
33 // function that executes whenever data is received from master
34 // this function is registered as an event, see setup()
35 void receiveEvent(int howMany)
36 {
37   Serial.print("Lo slave ha ricevuto il seguente messaggio: ");
38   while( Wire.available()) // loop through all but the last
39   {
40     input = Wire.read(); // receive byte as a character
41     Serial.print(input);         // print the character
42   }         // print the integer
43   Serial.println("");
44 }
45