slave_receiver.ino 979 B

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