You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.1KB

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