Found some time to try wrap my brain around the code. I made some headway but one thing has be a bit confused. The return value from the pressure temperature conversion is not correct. I am not sure if it is because of a number(s) being rounded.
I setup a couple Pots to emulate the pressure.
#define SEGMENT_POINTS 78
const int temperaturePin = 1; //pin used for temperature input
const int pressurePin = 0; //pin used for pressure transducer input
int pres, high = 0, low = 1023; //defines the high and low levels of the sensor
//pressure_temp_lookup is the "table", [_] is the number of rows, SEGMENT_POINTS ????? <-- SEGMENT_POINTS is the number of points you have to approximate the curve.
//PROGMEM puts data into flash memory, pgm calls it back <-- Correct
//Top row is what is used to return the lower rows (use the top row for pressure) <-- Correct
// You can add some defines here that specify what each row of the table
// represents.
#define PRESSURE 0
#define REF_1_TEMP 1 //507
#define REF_2_TEMP 2
float pressure_temp_lookup[3][SEGMENT_POINTS] PROGMEM =
{
{-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135, 140},
{-60, -58, -57, -55, -54, -53, -50, -48, -46, -44, -41, -39, -38, -36, -34, -32, -31, -29, -27, -26, -24, -23, -21, -20, -19, -17, -16, -15, -14, -12, -11, -9, -8, -7, -6, -4, -3, -2, -1, 0, 1 , 2, 3, 4 , 4, 6, 8, 10, 11, 13, 15, 16, 18, 19, 21, 22, 24, 25, 26, 28, 29, 30, 32, 33, 34, 37, 40, 43, 45, 48, 51, 53, 55, 58, 60, 62, 64},
{0, 1, 6, 12, 20, 26}
};
// Add a new function parameter that tells which refrigerant to look up called ref_index
float lookup_temp_from_pressure(uint8_t ref_index, float pressure)
{
// Search table for appropriate value.
uint8_t index = 0;
while( pgm_read_float(&pressure_temp_lookup[PRESSURE][index])<=pressure && index < SEGMENT_POINTS )
index++;
// If index is zero, pressure is smaller than our table range
if( index==0 )
{
return map_f( pressure,
pgm_read_float(&pressure_temp_lookup[PRESSURE][0]), // x1
pgm_read_float(&pressure_temp_lookup[PRESSURE][1]), // X2
pgm_read_float(&pressure_temp_lookup[ref_index][0]), // Y1
pgm_read_float(&pressure_temp_lookup[ref_index][1]) );// Y2
}
// If index is maxed out, pressure is larger than our range.
else if( index==SEGMENT_POINTS )
{
return map_f( pressure,
pgm_read_float(&pressure_temp_lookup[PRESSURE][SEGMENT_POINTS-2]), // x1
pgm_read_float(&pressure_temp_lookup[PRESSURE][SEGMENT_POINTS-1]), // X2
pgm_read_float(&pressure_temp_lookup[ref_index][SEGMENT_POINTS-2]), // Y1
pgm_read_float(&pressure_temp_lookup[ref_index][SEGMENT_POINTS-1]) );// Y2
}
// index is between 0 and max, just right
else
{
return map_f( pressure,
pgm_read_float(&pressure_temp_lookup[PRESSURE][index-1]), // x1
pgm_read_float(&pressure_temp_lookup[PRESSURE][index]), // X2
pgm_read_float(&pressure_temp_lookup[ref_index][index-1]), // Y1
pgm_read_float(&pressure_temp_lookup[ref_index][index]) );// Y2
}
}
void setup()
{
Serial.begin( 19200 );
}
void loop()
{
float voltage, degreesC, degreesF; //these are float values that are used to calculate the actual temp from the sensors 0-5v output
voltage = getVoltage(temperaturePin); //temperature sensor voltage. (pin number defined at the top of sketch)
degreesC = (voltage - 0.5) * 100.0; //Math to get *c
degreesF = degreesC * (9.0/5.0) + 32.0; //Math to get *f
pres = analogRead(pressurePin); //returns the pressure transducer input (pin number defined at the top of sketch)
int sst;
int sh;
sst = lookup_temp_from_pressure(REF_1_TEMP, pres);
sh = degreesF - sst;
//Serial.print(" deg C: ");
//Serial.print(degreesC);
Serial.print(degreesF);
Serial.println("*F");
Serial.print(pres);
Serial.print("psi");
Serial.print(" SST: ");
Serial.println(sst);
Serial.print("SH: ");
Serial.println(sh);
Serial.println();
//Serial.println( lookup_temp_from_pressure(REF_1_TEMP, 70) );
delay(5000); // repeat once per second
}
//This section manually sets the pressure to voltage from the transducer
void manualTune()
{
pres = map(pres, 0, 1023, 0, 255); //the input is 0-1023, it is mapped to 0-255)
pres = constrain(pres, 0, 255); //prevents the pressure from being outside of these values)
}
float getVoltage (int temperaturePin)
{
// This function has one input parameter, the analog pin number to read. It returns a floating-point value, which is the true voltage on that pin (0 to 5V).
// To take in parameters, put their type and name in the
// parenthesis after the function name (see above). You can
// have multiple parameters, separated with commas.
// To return a value, put the type BEFORE the function name
// (see "float", above), and use a return() statement in your code
// to actually return the value (see below).
// all the math we need to do within this statement:
return (analogRead(temperaturePin) * 0.004882814);
// This equation converts the 0 to 1023 value that analogRead()
// returns, into a 0.0 to 5.0 value that is the true voltage
// being read at that pin.
}
float map_f(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}