I'm sorry for being such a noob.
I still don't get the point in this:
These codes are from the examples provided from the arduino package.
Master Receiver:
#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(5, 6); // request 6 bytes from slave device #2
while(Wire.available()) // slave may send less than requested
{
char c = Wire.read(); // receive a byte as character
Serial.print(c); // print the character
}
delay(500);
}
Slave sender:
#include <Wire.h>
void setup()
{
Wire.begin(5); // 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("hello "); // respond with message of 6 bytes
// as expected by master
}
WHen I copy and paste the snippets into my project it all works fine. Now, if I alter the sent data from string to single char like this:
void requestEvent()
{
Wire.write("h");
Wire.write("e");
Wire.write("l");
Wire.write("l");
Wire.write("o");
Wire.write(" ");
}
it stops working.
Can someone explain to me what's happening here? Originally I am trying to send a long in a union bytewise, that doesn't work whenever I try to transmit more than one byte at once using different lines (like, in a loop) it sends empty bytes.
If that matters I'm using Nanos on both ends.