Ok, I've done all you said. It is still not working correctly. I am now going to use I2C_Anything, but I still wonder what the problem here is?
I have now:
Master:
// I2C BUS:
// Constants:
const byte slaveAddress = 8;
const byte dataCount = 24;
// Variables:
union T {byte b[8]; double d;} T;
union X {byte b[8]; double d;} X;
union Y {byte b[8]; double d;} Y;
// Functions:
void readPositions() // Requests coordinates (x and y) and the heading angle (theta) from the slave.
{
if (Wire.requestFrom(slaveAddress, dataCount) == dataCount)
{
for (byte i = 0; i < 8; i++)
{
T.b[i] = Wire.read(); Serial.print(T.b[i]); Serial.print(",");
X.b[i] = Wire.read(); Serial.print(X.b[i]); Serial.print(",");
Y.b[i] = Wire.read(); Serial.print(Y.b[i]); Serial.println();
}
Serial.println();
t = T.d; x = X.d; y = Y.d;
// Serial.print(t); Serial.print(",");
// Serial.print(x); Serial.print(",");
// Serial.print(y); Serial.println();
}
}
Slave:
// I2C BUS:
// Constants:
const byte myAddress = 8;
const byte dataCount = 24; // [bytes] Max:32
// Variables:
union T {byte b[8]; double d;} T;
union X {byte b[8]; double d;} X;
union Y {byte b[8]; double d;} Y;
byte data[dataCount];
// Functions:
void writePositions() // The function that executes whenever data is requested by master.
{
T.d = t; X.d = x; Y.d = y;
for (byte i = 0; i < 8; i++)
{
byte j = i * 3;
data[j] = T.b[i]; Serial.print(data[j]); Serial.print(",");
data[j + 1] = X.b[i]; Serial.print(data[j + 1]); Serial.print(",");
data[j + 2] = Y.b[i]; Serial.print(data[j + 2]); Serial.println();
}
Serial.println();
Wire.write(data, sizeof data);
// Serial.print(t); Serial.print(",");
// Serial.print(x); Serial.print(",");
// Serial.print(y); Serial.println();
}
Outputs are:
Slave:
249,17,126
186,201,27
131,71,189
155,55,202
120,75,69
31,153,120
25,165,245
64,191,62
Master:
64,249,17
126,186,201
27,131,71
189,155,55
202,120,75
69,31,153
120,25,165
245,64,255