Hi,
I am using the basic code from:
Master:
#include <Wire.h>
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
void loop() {
Wire.requestFrom(2, 1); // request 1 byte from slave device #2
while (Wire.available()) { // slave may send less than requested
char c = Wire.read(); // receive a byte as character
Serial.println(c); // print the character
}
delay(500);
}
Slave:
#include <Wire.h>
void setup() {
Wire.begin(2); // join i2c bus with address #2
Wire.onRequest(requestEvent); // register event
}
void loop() {
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
Wire.write("a"); // respond with message of 1 bytes
// as expected by master
}
Currently I am trying to make it work with 1 nano only. A4 is connected to A4, A5 to A5 and gnd to gnd.
It seems like the master code cannot go in the "while (Wire.available())" and if I remove this line it prints "ÿ". If I print a test line in the while it never appears. Also, if I print a test line in the slave "void requestEvent()" it never appears (I called Serial.begin(9600); when I tested).
Are my connection wrong or did I use the code in a bad way?
Thanks.