Hey,
See if this helps you at all.
I am reading 2 floats from the slave.
Master Arduino has this code somewhere in the loop, triggered as you see fit:
byte data[8];
float RPM;
float DRPS;
void loop()
{
Wire.beginTransmission(2);
Wire.requestFrom(2, 8); // request 8 bytes from slave device #2
if(Wire.available())
{
int i = 0;
while(Wire.available()) // slave may send less than requested
{
data[i] = Wire.receive(); // receive a byte as character
i = i + 1;
}
//A union datatypes makes the byte and float elements share the same piece of memory, which enables conversion from a byte array to a float possible
union RPM_tag {byte RPM_b[4]; float RPM_fval;} RPM_Union;
RPM_Union.RPM_b[0] = data[0];
RPM_Union.RPM_b[1] = data[1];
RPM_Union.RPM_b[2] = data[2];
RPM_Union.RPM_b[3] = data[3];
RPM = RPM_Union.RPM_fval * 60;
union DRPS_tag {byte DRPS_b[4]; float DRPS_fval;} DRPS_Union; //DRPS = Drum Revs per Second
DRPS_Union.DRPS_b[0] = data[4];
DRPS_Union.DRPS_b[1] = data[5];
DRPS_Union.DRPS_b[2] = data[6];
DRPS_Union.DRPS_b[3] = data[7];
DRPS = DRPS_Union.DRPS_fval;
}
Wire.endTransmission();
}
First float in this case is RPM, second is Drum Revs Per Second (DRPS). So make these whatever you like.
Slave Arduino has this code:
volatile byte* INPUT1FloatPtr;
volatile byte* INPUT2FloatPtr;
int Address = 2; //This slave is address number 2
void setup()
{
Wire.begin(Address);
Wire.onRequest(requestEvent); // register event
}
void loop()
{
lastINPUT1 = 12345.56 //your code here
lastINPUT2 = 1234.12 //your code here
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
byte* Data;
INPUT1FloatPtr = (byte*) &lastINPUT1;
INPUT2FloatPtr = (byte*) &lastINPUT2;
Data[0] = INPUT1FloatPtr[0];
Data[1] = INPUT1FloatPtr[1];
Data[2] = INPUT1FloatPtr[2];
Data[3] = INPUT1FloatPtr[3];
Data[4] = INPUT2FloatPtr[0];
Data[5] = INPUT2FloatPtr[1];
Data[6] = INPUT2FloatPtr[2];
Data[7] = INPUT2FloatPtr[3];
Wire.send(Data,8);
}
So in the above code, the first float I am sending is lastINPUT1, the second is lastINPUT2. They are both bundled up into a byte array called Data, and sent over Wire to the master.
The master then reads in each byte, and outputs the two floats.
Is that of any help?
Cheers
James