| 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 |
Wire.begin(8); // join I2C bus with address #8 |
| 17 |
Wire.onReceive(receiveEvent); // register event |
| 18 |
Serial.begin(9600); // start serial for output |
| 19 |
} |
| 20 |
|
| 21 |
void loop() { |
| 22 |
delay(100); |
| 23 |
} |
| 24 |
|
| 25 |
// function that executes whenever data is received from master |
| 26 |
// this function is registered as an event, see setup() |
| 27 |
void receiveEvent(int howMany) { |
| 28 |
while (1 < Wire.available()) { // loop through all but the last |
| 29 |
char c = Wire.read(); // receive byte as a character |
| 30 |
Serial.print(c); // print the character |
| 31 |
} |
| 32 |
int x = Wire.read(); // receive byte as an integer |
| 33 |
Serial.println(x); // print the integer |
| 34 |
} |