I’m trying to use bitwise shift to convert two bytes into an integer. This works fine on a normal integer, but it doesn’t work if my integer is part of a typdef structure. I get erratic readings and my arduino reboots.
typedef struct {
byte slaveID;
byte cmdID;
uint16_t mV;
uint16_t temp;
float myFloat; // some float number
uint32_t mS; // milliseconds
} RemoteSensorData_t;
void loop() {
RemoteSensorData_t sensor1;
if( getData( &sensor1 ))
{ Serial.println(sensor1.temp); }
}
This works fine:
bool getData(RemoteSensorData_t* sensorInfo)
{
uint16_t temperature;
byte i2CData[PACKET_SIZE];
// removed some i2CData code for brevity
temperature = i2CData[4] << 8;
temperature |= i2CData[5];
sensorInfo->temp = temperature;
}
This doesn’t work
bool getData(RemoteSensorData_t* sensorInfo)
{
byte i2CData[PACKET_SIZE];
// removed some i2CData code for brevity
sensorInfo->temp = i2CData[4] << 8;
sensorInfo->temp |= i2CData[5];
}
Is typdef structure just not compatible with bitwise operations, or am I messing something up with pointers?