// Function to read pressures and temperature from the ADS1118
void readPressuresAndTemperature(float pressures[4], float &temperature) {
float mV, mA;
// Set ADS1118 configuration
ads1118.setSamplingRate(ads1118.RATE_8SPS);
ads1118.setFullScaleRange(ads1118.FSR_2048);
// Read the temperature (in Celsius)
float tempC = ads1118.getTemperature();
temperature = tempC * 9.0 / 5.0 + 32.0; // Convert to Fahrenheit
for (int i = 0; i < 4; i++) {
// Map the loop index to the correct ADS1118 input channels
int channel;
switch (i) {
case 0: channel = ads1118.AIN_0; break;
case 1: channel = ads1118.AIN_1; break;
case 2: channel = ads1118.AIN_2; break;
case 3: channel = ads1118.AIN_3; break;
}
// Select the input channel and allow settling time
ads1118.setInputSelected(channel);
delayMicroseconds(50); // Allow settling time
// Discard the first reading and take the second
ads1118.getMilliVolts(); // Discard first reading
mV = ads1118.getMilliVolts(); // Read stabilized value
// Calculate current (mA) based on calibration values
switch (i) {
case 0: mA = mV * 0.011774056 - 0.0706443 + 0.07; break;
case 1: mA = mV * 0.011816083 - 0.000708965; break;
case 2: mA = mV * 0.011794713 - 0.002240995; break;
case 3: mA = mV * 0.011810675 - 0.002244028; break;
}
// Calculate pressure
pressures[i] = mA * 250 - 1000.0;
if (i == 0) pressures[i] -= 0.32; // Adjustment for Pressure 1
if (i == 1) pressures[i] -= 0.12; // Adjustment for Pressure 2
if (pressures[i] < 0) pressures[i] = 0.0; // Prevent negative pressure
}
}