Hardware Reference
In-Depth Information
Listing 8-2: Master Sketch (fi lename: Chapter8bMaster.ino).
1 #include <Wire.h>
2
3 #define SLAVE_ADDRESS 0x08
4 int data = 0;
5 int state = 0;
6
7 void setup()
8 {
9 pinMode(13, OUTPUT); // Internal LED
10 Serial.begin(9600);
11 Wire.begin(); // Initialize as I2C master
12 }
13
14 void loop()
15 {
16 Wire.beginTransmission(SLAVE_ADDRESS); // Prepare message to slave
17 Wire.write(1); // Send one byte, LED ON
18 Wire.endTransmission(); // End message, transmit
19 digitalWrite(13, HIGH); // Turn the LED on
20
21 delay(10); // Give the slave time to react
22 printLight(); // What is the slave's status?
23
24 delay(1000);
25
26 Wire.beginTransmission(SLAVE_ADDRESS); // Prepare message to slave
27 Wire.write(0); // Send one byte, LED OFF
28 Wire.endTransmission(); // End message, transmit
29 digitalWrite(13, LOW); // Turn the LED off
30
31 delay(10); // Give the slave time to react
32 printLight(); // What is the slave's status?
33
34 delay(200);
35 }
36
37 void printLight()
38 {
39 Wire.requestFrom(SLAVE_ADDRESS, 1); // Request 1 byte from slave
40
41 data = Wire.read(); // Receive a byte af data
42 switch (data)
43 {
44 case 0:
45 Serial.println("LED is OFF");
46 break;
47 case 1:
continues
Search WWH ::




Custom Search