root / arduino-1.0 / libraries / Wire / examples / slave_receiver / slave_receiver.ino @ 58d82c77
History | View | Annotate | Download (978 Bytes)
1 |
// Wire Slave Receiver |
---|---|
2 |
// by Nicholas Zambetti <http://www.zambetti.com> |
3 |
|
4 |
// Demonstrates use of the Wire library |
5 |
// Receives data as an I2C/TWI slave device |
6 |
// Refer to the "Wire Master Writer" example for use with this |
7 |
|
8 |
// Created 29 March 2006 |
9 |
|
10 |
// This example code is in the public domain. |
11 |
|
12 |
|
13 |
#include <Wire.h> |
14 |
|
15 |
void setup() |
16 |
{ |
17 |
Wire.begin(4); // join i2c bus with address #4 |
18 |
Wire.onReceive(receiveEvent); // register event |
19 |
Serial.begin(9600); // start serial for output |
20 |
} |
21 |
|
22 |
void loop() |
23 |
{ |
24 |
delay(100); |
25 |
} |
26 |
|
27 |
// function that executes whenever data is received from master |
28 |
// this function is registered as an event, see setup() |
29 |
void receiveEvent(int howMany) |
30 |
{ |
31 |
while(1 < Wire.available()) // loop through all but the last |
32 |
{ |
33 |
char c = Wire.read(); // receive byte as a character |
34 |
Serial.print(c); // print the character |
35 |
} |
36 |
int x = Wire.read(); // receive byte as an integer |
37 |
Serial.println(x); // print the integer |
38 |
} |