Skip to content

Instantly share code, notes, and snippets.

@henrybear327
Forked from rwaldron/i2c_backpack_master.ino
Last active August 29, 2015 14:22
Show Gist options
  • Save henrybear327/751376b1edfaecfd92a5 to your computer and use it in GitHub Desktop.
Save henrybear327/751376b1edfaecfd92a5 to your computer and use it in GitHub Desktop.
#include <Wire.h>
byte i2cdata[4];
void setup() {
Wire.begin();
Serial.begin(9600);
}
void loop() {
Wire.requestFrom(4, 4);
int a = Wire.available();
Serial.print("Available: ");
Serial.println(a);
// Both of these produce the same result
// for (int i = 0; i < a; i++) {
// Serial.print(Wire.read());
// Serial.print(" ");
// }
while (Wire.available()) {
Serial.print(Wire.read());
Serial.print(" ");
}
Serial.println();
memset(&i2cdata, 0, 4);
delay(500);
}
#include <Wire.h>
int address = 4;
void setup() {
Wire.begin(address);
Wire.onRequest(onRequest);
}
void loop() {}
void onRequest() {
byte buffer[4] = {1, 2, 3, 4};
Wire.write(buffer, 4);
}
/*
When this sketch is running on the slave, the output of the master is:
Available: 4
1 2 3 4
...
*/
#include <Wire.h>
int address = 4;
void setup() {
Wire.begin(address);
Wire.onRequest(onRequest);
}
void loop() {}
void onRequest() {
for (int i = 1; i < 5; i++) {
Wire.write(i);
}
}
/*
When this sketch is running on the slave, the output of the master is:
Available: 4
4 255 255 255
...
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment