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