@@ -12,8 +12,11 @@ | |||
#include <Wire.h> | |||
int led = LED_BUILTIN; | |||
void setup() | |||
{ | |||
pinMode(led, OUTPUT); | |||
Wire.begin(); // join i2c bus (address optional for master) | |||
Serial.begin(9600); // start serial for output | |||
} | |||
@@ -22,6 +25,7 @@ void loop() | |||
{ | |||
Serial.print("read: "); | |||
digitalWrite(led, HIGH); // briefly flash the LED | |||
Wire.requestFrom(8, 6); // request 6 bytes from slave device #8 | |||
while(Wire.available()) { // slave may send less than requested | |||
@@ -30,5 +34,6 @@ void loop() | |||
} | |||
Serial.println(); | |||
digitalWrite(led, LOW); | |||
delay(500); | |||
} |
@@ -12,8 +12,11 @@ | |||
#include <Wire.h> | |||
int led = LED_BUILTIN; | |||
void setup() | |||
{ | |||
pinMode(led, OUTPUT); | |||
Wire.begin(); // join i2c bus (address optional for master) | |||
} | |||
@@ -21,10 +24,12 @@ byte x = 0; | |||
void loop() | |||
{ | |||
digitalWrite(led, HIGH); // briefly flash the LED | |||
Wire.beginTransmission(9); // transmit to device #9 | |||
Wire.write("x is "); // sends five bytes | |||
Wire.write(x); // sends one byte | |||
Wire.endTransmission(); // stop transmitting | |||
digitalWrite(led, LOW); | |||
x++; | |||
delay(500); |
@@ -12,8 +12,11 @@ | |||
#include <Wire.h> | |||
int led = LED_BUILTIN; | |||
void setup() | |||
{ | |||
pinMode(led, OUTPUT); | |||
Wire.begin(9); // join i2c bus with address #9 | |||
Wire.onReceive(receiveEvent); // register event | |||
Serial.begin(9600); // start serial for output | |||
@@ -28,10 +31,12 @@ void loop() | |||
// this function is registered as an event, see setup() | |||
void receiveEvent(int howMany) | |||
{ | |||
digitalWrite(led, HIGH); // briefly flash the LED | |||
while(Wire.available() > 1) { // loop through all but the last | |||
char c = Wire.read(); // receive byte as a character | |||
Serial.print(c); // print the character | |||
} | |||
int x = Wire.read(); // receive byte as an integer | |||
Serial.println(x); // print the integer | |||
digitalWrite(led, LOW); | |||
} |
@@ -12,7 +12,7 @@ | |||
#include <Wire.h> | |||
int led = BUILTIN_LED; | |||
int led = LED_BUILTIN; | |||
void setup() | |||
{ |