Wire.requestFrom(112, 2); // request 2 bytes from slave device #112 | Wire.requestFrom(112, 2); // request 2 bytes from slave device #112 | ||||
// step 5: receive reading from sensor | // step 5: receive reading from sensor | ||||
if(2 <= Wire.available()) // if two bytes were received | |||||
{ | |||||
reading = Wire.read(); // receive high byte (overwrites previous reading) | |||||
if (Wire.available() >= 2) { // if two bytes were received | |||||
reading = Wire.read(); // receive high byte (overwrites previous reading) | |||||
reading = reading << 8; // shift high byte to be high 8 bits | reading = reading << 8; // shift high byte to be high 8 bits | ||||
reading |= Wire.read(); // receive low byte as lower 8 bits | |||||
reading |= Wire.read(); // receive low byte as lower 8 bits | |||||
Serial.println(reading); // print the reading | Serial.println(reading); // print the reading | ||||
} | } | ||||
void setup() { | void setup() { | ||||
// uncomment these to use alternate pins | |||||
//Wire.setSCL(16); | |||||
//Wire.setSDA(17); | |||||
Wire.begin(); | Wire.begin(); | ||||
Serial.begin(9600); | Serial.begin(9600); | ||||
while (!Serial); // Leonardo: wait for serial monitor | while (!Serial); // Leonardo: wait for serial monitor |
void setup() | void setup() | ||||
{ | { | ||||
Wire.begin(); // join i2c bus (address optional for master) | |||||
Serial.begin(9600); // start serial for output | |||||
Wire.begin(); // join i2c bus (address optional for master) | |||||
Serial.begin(9600); // start serial for output | |||||
} | } | ||||
void loop() | void loop() | ||||
{ | { | ||||
Wire.requestFrom(8, 6); // request 6 bytes from slave device #8 | |||||
Serial.print("read: "); | |||||
while(Wire.available()) // slave may send less than requested | |||||
{ | |||||
char c = Wire.read(); // receive a byte as character | |||||
Serial.print(c); // print the character | |||||
Wire.requestFrom(8, 6); // request 6 bytes from slave device #8 | |||||
while(Wire.available()) { // slave may send less than requested | |||||
char c = Wire.read(); // receive a byte as character | |||||
Serial.print(c); // print the character | |||||
} | } | ||||
Serial.println(); | |||||
delay(500); | delay(500); | ||||
} | } |
void loop() | void loop() | ||||
{ | { | ||||
Wire.beginTransmission(8); // transmit to device #8 | |||||
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.endTransmission(); // stop transmitting | |||||
Wire.write(x); // sends one byte | |||||
Wire.endTransmission(); // stop transmitting | |||||
x++; | x++; | ||||
delay(500); | delay(500); |
void setup() | void setup() | ||||
{ | { | ||||
Wire.begin(8); // join i2c bus with address #8 | |||||
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) | ||||
{ | { | ||||
while(1 < Wire.available()) // loop through all but the last | |||||
{ | |||||
char c = Wire.read(); // receive byte as a character | |||||
Serial.print(c); // print the character | |||||
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 | |||||
int x = Wire.read(); // receive byte as an integer | |||||
Serial.println(x); // print the integer | |||||
} | } |
#include <Wire.h> | #include <Wire.h> | ||||
int led = BUILTIN_LED; | |||||
void setup() | void setup() | ||||
{ | { | ||||
pinMode(led, OUTPUT); | |||||
Wire.begin(8); // join i2c bus with address #8 | Wire.begin(8); // join i2c bus with address #8 | ||||
Wire.onRequest(requestEvent); // register event | Wire.onRequest(requestEvent); // register event | ||||
} | } | ||||
// this function is registered as an event, see setup() | // this function is registered as an event, see setup() | ||||
void requestEvent() | void requestEvent() | ||||
{ | { | ||||
Wire.write("hello "); // respond with message of 6 bytes | |||||
// as expected by master | |||||
digitalWrite(led, HIGH); // briefly flash the LED | |||||
Wire.write("hello "); // respond with message of 6 bytes | |||||
// as expected by master | |||||
digitalWrite(led, LOW); | |||||
} | } |