ok thank you for your explanation!
I did not want to throw just 400 lines of code "over the fence". Please keep in mind I am still learning so I know the code can be done in a way more efficient way. But that will come later.
Anyway here is my complete code: I am sorry I had to remove a few lines because of the fact I exceeded 9000 characters:
byte ChipIDByte = 0;
byte MSBpressData = 0;
byte MidPressData = 0;
byte SensorStatus = 0;
byte ErrorStatus = 0;
byte OSRRegData = 0;
byte FifoConfigStatus = 0;
byte PwrCtrlStatus = 0;
long Data1 = 0;
long Data2 = 0;
long Data3 = 0;
long Data4 = 0;
long Data5 = 0;
long Data6 = 0;
/*
Bytes to store pressure and temperature calibration data
*/
byte NVM_PAR_T1_1 = 0;
byte NVM_PAR_T1_2 = 0;
byte NVM_PAR_T2_1 = 0;
byte NVM_PAR_T2_2 = 0;
byte NVM_PAR_T3 = 0;
byte NVM_PAR_P1_1 = 0;
byte NVM_PAR_P1_2 = 0;
byte NVM_PAR_P2_1 = 0;
byte NVM_PAR_P2_2 = 0;
byte NVM_PAR_P3 = 0;
byte NVM_PAR_P4 = 0;
byte NVM_PAR_P5_1 = 0;
byte NVM_PAR_P5_2 = 0;
byte NVM_PAR_P6_1 = 0;
byte NVM_PAR_P6_2 = 0;
byte NVM_PAR_P7 = 0;
byte NVM_PAR_P8 = 0;
byte NVM_PAR_P9_1 = 0;
byte NVM_PAR_P9_2 = 0;
byte NVM_PAR_P10 = 0;
byte NVM_PAR_P11 = 0;
unsigned int CombinedData = 0;
//unsigned int NVM_PAR_T1_COMBINED = 0;
//unsigned int NVM_PAR_T2_COMBINED = 0;
float NVM_PAR_T1_COMBINED = 0.0;
float NVM_PAR_T2_COMBINED = 0.0;
//float NVM_PAR_T3 = 0.0;
/*
Combined bytes of pressure calibration data
*/
unsigned int NVM_PAR_P1_COMBINED = 0;
unsigned int NVM_PAR_P2_COMBINED = 0;
unsigned int NVM_PAR_P5_COMBINED = 0;
unsigned int NVM_PAR_P6_COMBINED = 0;
unsigned int NVM_PAR_P9_COMBINED = 0;
float Parameter_T1;
float Parameter_T2;
float Parameter_T3;
float Parameter_P1;
float Parameter_P2;
float Parameter_P3;
float Parameter_P4;
float Parameter_P5;
float Parameter_P6;
float Parameter_P7;
float Parameter_P8;
float Parameter_P9;
float Parameter_P10;
float Parameter_P11;
float T_lin;
long CombinedRawData;
long UncompPress;
long CombinedPressData;
void setup() { // put your setup code here, to run once:
Wire.begin();//initiate the Wire library
Serial.begin(9600); //initiate serial comm with Arduino
delay(100); // time to setup everything
Wire.beginTransmission(BaroSensAdd);// send the sensor Address
Wire.write(PwrCtrlReg);// send the register to write to
Wire.write(NormalMode);// send the value to the register
Wire.endTransmission();// stop transmission
delay(100);// give sensor time to process
// Wire.beginTransmission(BaroSensAdd);
// Wire.write(OSR_REG);// send the register to write to
// Wire.write(0x52);// send the value to the register
// Wire.endTransmission();// stop transmission
delay(100);// give sensor time to process
GetTempCalibration();
GetPresCalibration();
CalibrateTemp();
CalibratePress();
delay(5000);
}
void loop() {// put your main code here, to run repeatedly:
/*
* The first transmission of data gets temperature
*/
Wire.beginTransmission(BaroSensAdd);// start sending a mess70)age to the sensor
Wire.write(TempDataReg);//try to read out the chip ID
Wire.endTransmission();//send the end of transmission
Wire.requestFrom(BaroSensAdd, 3);// request 1 byte from the BMP388
if (Wire.available() <= 3) {//if the byte is available
Data1 = Wire.read();//get byte 1
Data2 = Wire.read();//get byte 2
Data3 = Wire.read();//get byte 3
Serial.println("Byte 1");// print text
Serial.println(Data1, HEX); // show byte 1
Serial.println("Byte 2");// print text
Serial.println(Data2, HEX);// show byte 2
Serial.println("Byte 3");// print text
Serial.println(Data3, HEX);//show byte 3
long CombinedData = (Data3 << 16) | (Data2 << 8) | (Data1); //
Serial.println("Combined Data: ");
Serial.println(CombinedData, 10);
CombinedRawData = CombinedData;
Serial.println("Combined data double");
Serial.println(CombinedRawData);
CompensateTemp();
delay(100);
}
/*
* This transmisison wil get pressure data
*/
Wire.beginTransmission(BaroSensAdd);// start sending a mess70)age to the sensor
Wire.write(DataReg0);//try to read out the chip ID
Wire.endTransmission();//send the end of transmission
Wire.requestFrom(BaroSensAdd, 3);// request 1 byte from the BMP388
if (Wire.available() <= 3) {
Serial.println("pressure data");
Data4 = Wire.read();//get byte 1
Data5 = Wire.read();//get byte 2
Data6 = Wire.read();//get byte 3
Serial.println("Byte 1");// print text
Serial.println(Data4, HEX); // show byte 1
Serial.println("Byte 2");// print text
Serial.println(Data5, HEX);// show byte 2
Serial.println("Byte 3");// print text
Serial.println(Data6, HEX);//show byte 3
long CombinedPress = (Data6 << 16) | (Data5 << 8) | (Data4); //
Serial.println(CombinedPress);
CombinedPressData = CombinedPress;
CompensatePress();
delay(5000);//delay
}
}
void CalibrateTemp() {
Parameter_T1 = (float)NVM_PAR_T1_COMBINED / powf(2.0f, -8.0f);
Parameter_T2 = (float)NVM_PAR_T2_COMBINED / powf(2.0f, 30.0f);
Parameter_T3 = (float)NVM_PAR_T3 / powf(2.0f, 48.0f);
Serial.println ("Trim Parameters Temp");
Serial.println (Parameter_T1);
Serial.println (Parameter_T2, 10);
Serial.println (Parameter_T3, 15);
}
/*
* Calculations to compensate temperature
*/
void CompensateTemp() {
float PartialData1 = (float)CombinedRawData - Parameter_T1;
float PartialData2 = PartialData1 * Parameter_T2;
float Temperature = PartialData2 + (PartialData1 * PartialData1) * Parameter_T3;
T_lin = Temperature;
Serial.println("Temperature");
Serial.println(Temperature);
}
/*
* This function will calculate calibration coefficients
*/
void CalibratePress() {
Parameter_P1 = ((float)NVM_PAR_P1_COMBINED - powf(2.0f, 14.0f)) / powf(2.0f, 20.0f);
Parameter_P2 = ((float)NVM_PAR_P2_COMBINED - powf(2.0f, 14.0f)) / powf(2.0f, 29.0f);
Parameter_P3 = (float)NVM_PAR_P3 / powf(2.0f, 32.0f);
Parameter_P4 = (float)NVM_PAR_P4 / powf(2.0f, 37.0f);
Parameter_P5 = (float)NVM_PAR_P5_COMBINED / powf(2.0f, -3.0f);
Parameter_P6 = (float)NVM_PAR_P6_COMBINED / powf(2.0f, 6.0f);
Parameter_P7 = (float)NVM_PAR_P7 / powf(2.0f, 8.0f);
Parameter_P8 = (float)NVM_PAR_P8 / powf(2.0f, 15.0f);
Parameter_P9 = (float)NVM_PAR_P9_COMBINED / powf(2.0f, 48.0f);
Parameter_P10 = (float)NVM_PAR_P10 / powf(2.0f, 48.0f);
Parameter_P11 = (float)NVM_PAR_P11 / powf(2.0f, 65.0f);
Serial.println ("Trim Parameters Press");
Serial.println (Parameter_P1, 15);
Serial.println (Parameter_P2, 15);
Serial.println (Parameter_P3, 15);
Serial.println (Parameter_P4, 15);
Serial.println (Parameter_P5, 15);
Serial.println (Parameter_P6, 15);
Serial.println (Parameter_P7, 15);
Serial.println (Parameter_P8, 15);
Serial.println (Parameter_P9, 15);
Serial.println (Parameter_P10, 15);
Serial.println (Parameter_P11, 18);
}
/*
* This function is based on the formulast out of the datasheet
* and will calibrate pressure based on the Temperature
*/
void CompensatePress() {
float PartialData_P1 = Parameter_P6 * T_lin;
float PartialData_P2 = Parameter_P7 * T_lin * T_lin;
float PartialData_P3 = Parameter_P8 * T_lin * T_lin * T_lin;
float Partial_out_1 = Parameter_P5 + PartialData_P1 + PartialData_P2 + PartialData_P3;
PartialData_P1 = Parameter_P2 * T_lin;
PartialData_P2 = Parameter_P3 * T_lin * T_lin;
PartialData_P3 = Parameter_P4 * T_lin * T_lin * T_lin;
float Partial_out_2 = (float)CombinedPressData * (Parameter_P1 + PartialData_P1 + PartialData_P2 + PartialData_P3);;
PartialData_P1 = (float)CombinedPressData * (float)CombinedPressData;
PartialData_P2 = Parameter_P9 + Parameter_P10 * T_lin;
PartialData_P3 = PartialData_P1 * PartialData_P2;
float PartialData_P4 = PartialData_P3 + (float)CombinedPressData * (float)CombinedPressData * CombinedPressData * Parameter_P11;
float Pressure_2 = Partial_out_1 + Partial_out_2 + PartialData_P4;
}