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.

34 lines
522 B

  1. #include <Wire.h>
  2. #define SLAVE_ADDRESS 0x04
  3. void setup()
  4. {
  5. Serial.begin(115200); // start serial for output
  6. Wire.begin(SLAVE_ADDRESS);
  7. Wire.onReceive(receiveData);
  8. Wire.onRequest(sendData);
  9. }
  10. int val,flag=0;
  11. void loop()
  12. {
  13. if(flag==1)
  14. {
  15. Serial.print(val);
  16. flag=0;
  17. }
  18. }
  19. void receiveData(int byteCount)
  20. {
  21. while(Wire.available()>0)
  22. {
  23. val=Wire.read();
  24. flag=1;
  25. }
  26. }
  27. // callback for sending data
  28. void sendData()
  29. {
  30. char a = Serial.read();
  31. if(a > 0){
  32. Wire.write(a);
  33. }
  34. }