My programs dont work right. If i delete a few variables from the struct i get the data as expected. if i add any more than 5 floats to the struct the received data is nan.
my master is mega2560 and the slave is a arduino UNO
Master,
struct testStruct {
float T1 = 0;
float T2 = 0;
float T3 = 0;
float T4 = 0;
float T5 = 0;
float T6 = 0;
float T7 = 0;
float T8 = 0;
float T9 = 0;
};
testStruct ts1;
char messageBuffer[sizeof(ts1)];
unsigned long now = 0;
int state = 0;
#include <Wire.h>
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(115200); // start serial for output
}
void loop() {
Wire.requestFrom(2, sizeof(ts1)); // request data from slave device #2
while (Wire.available())
{
Wire.readBytes(messageBuffer , sizeof(ts1));
// / strncpy (verifyStart, (char*)messageBuffer, 6);//6 bytes
memcpy(&ts1, & messageBuffer , sizeof(ts1));
return;
}
delay(100);
}
Slave,
struct testStruct {
float T1 = 0;
float T2 = 0;
float T3 = 0;
float T4 = 0;
float T5 = 0;
float T6 = 0;
float T7 = 0;
float T8 = 0;
float T9 = 0;
};
testStruct ts1;
uint8_t message[sizeof(ts1)];
#include <Wire.h>
void setup()
{
Wire.begin(2); // join i2c bus with address #2
Wire.onRequest(requestEvent); // register event
Serial.begin(9600); // start serial for output
}
void loop()
{
// delay(100);
}
void requestEvent()
{
Wire.write((uint8_t*)&ts1, sizeof(ts1));
}
Why if i make more than 5 variables in the struct is the data not received right?
If i just use 1 float variable in the struct the data seems to show up to the master correctly. but not if i use more than 5.
Is this because of the way i use sizeof()