I'm trying to be aware of memory consumption with variables as the project I'm working on might get quite large. Would anyone like to review a bit of code I've written and comment on whether I'm using pointers correctly, the use of pointers is useful in this situation or a waste of time. To me it seemed more efficient.
Below is the function I'm calling on to get data from certain registers from an I2C device.
void I2C_ZMDTxRx(int CMD){// I2C COM "CMD" selects operation.
//local vars
byte msb, lsb;
int data;
Wire.beginTransmission(I2C_TxRxAddr);
Wire.send(CMD);
Wire.endTransmission();
delay(inttime); //I2C Process Time Delay
Wire.requestFrom(I2C_TxRxAddr,2);
if (Wire.available() <= 2){ // if two bytes were received
msb = Wire.receive(); // receive high byte
lsb = Wire.receive(); // receive low byte
//combine data
data = (msb << 8); // MSB
data |= (lsb); // LSB
//serial display -- Choose what we are printing for serial print line "data" = batch ,serial number, etc..
if (CMD == BATCH){
Serial.print("Batch: ");
Serial.println(data,DEC);
}
else if (CMD == SN){
Serial.print("Serial Number: ");
Serial.println(data,DEC);
}
else if (CMD == MEAS){
Serial.println(data,DEC);
}
else {
Serial.print("Error: No I2C Communications, Reconnect Sensor Device");
}
}
}//END I2C COM
Code with Pointers, does it make sense to use them versus the above code ??
void I2C_TxRx(int CMD){// I2C COM "CMD" selects operation.
//local vars
byte msb, lsb, *pbyte;
int data, *pdata = &data;
Wire.beginTransmission(I2C_TxRxAddr);
Wire.send(CMD);
Wire.endTransmission();
delay(inttime); //Mandatory I2C Process Time Delay
Wire.requestFrom(I2C_TxRxAddr,2);
if (Wire.available() <= 2){ // if two bytes were received
pbyte = &msb;
*pbyte = Wire.receive(); // receive high byte
pbyte = &lsb;
*pbyte = Wire.receive(); // receive low byte
//combine data
*pdata = (msb << 8); // MSB
*pdata |= (lsb); // LSB
//serial display -- Choose what we are printing for serial print line "data" = batch ,serial number, etc..
if (CMD == BATCH){
Serial.print("Batch: ");
Serial.println(data,DEC);
}
else if (CMD == SN){
Serial.print("Serial Number: ");
Serial.println(data,DEC);
}
else if (CMD == MEAS){
Serial.println(data,DEC);
}
else { //should we check for valid data data != 0
Serial.print("Error: No I2C Communications, Reconnect Sensor Device");
}
}
}//END I2C COM