Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. // Example By ArduinoAll
  2. // Arduino ตัวที่ 2 ใช้รับข้อมูล ที่ Arduino ตัวที่ 1 ส่งมา
  3. #include <Wire.h>
  4. int led = 13;
  5. int x = 0;
  6. void setup() {
  7. Wire.begin(9); // เริ่มสื่อสารแบบ I2C กำหนด address คือ 9
  8. Wire.onReceive(receiveEvent); // เมื่อมีข้อมูลส่งมาให้ทำในฟังก์ชั่นนี้ register event
  9. Serial.begin(9600);
  10. pinMode(led, OUTPUT);
  11. }
  12. void loop() {
  13. delay(100);
  14. }
  15.  
  16. void receiveEvent( int bytes )
  17. {
  18. x = Wire.read(); // อ่านค่าที่รับเข้ามา
  19. Serial.println(x);
  20. if (x == 0) {
  21. digitalWrite(led, LOW);
  22. }
  23. if (x == 1) {
  24. digitalWrite(led, HIGH);
  25. }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement