Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Created February 18, 2015 15:38
Show Gist options
  • Select an option

  • Save rwaldron/616e95b7c2637ebd21c5 to your computer and use it in GitHub Desktop.

Select an option

Save rwaldron/616e95b7c2637ebd21c5 to your computer and use it in GitHub Desktop.
I2C slave: Wire.write() sending only one byte?
#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
...
*/
@soundanalogous

Copy link
Copy Markdown

actually it looks like TinyWireM only implements I2C master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment