Code:
////////////////////////////////////////////////////////////
Raw_ADC = analogRead(Lo_Side_Pressure_Transducer_pin);//read Suction pressure
if(DEBUG){ Raw_ADC = 400;}
Suct_PSI = convert_raw_sensor_to_PSI( Raw_ADC, Sensor_150);
lcd.clear();
lcd.home (); // go home
lcd.print(F("SUCT_PSI="));
lcd.print(Suct_PSI);
delay(10000);
if (Suct_PSI == -1) //is Suct_PSI out of bounds
{
lcd.clear();
lcd.home (); // go home
lcd.print(F("RAW_PSI_NG"));
lcd.setCursor(0,1);
lcd.print(F("HANG_FOREVER_3"));
do{Y = Y;} while (Y == Y); //hang forever
}
else
{ //else in bounds
Saturated_Temp = calc_R290_Saturated_Temp(Suct_PSI, Sensor_150);
lcd.clear();
lcd.home (); // go home
lcd.print(F("SAT_T="));
lcd.print(Saturated_Temp);
lcd.print(F("*F"));
delay(10000);
if (Saturated_Temp == -1) //is Suct_PSI out of bounds
{
lcd.clear();
lcd.home (); // go home
lcd.print(F("SUCT_PSI_NG"));
lcd.setCursor(0,1);
lcd.print(F("HANG_FOREVER_4"));
do{Y = Y;} while (Y == Y); //hang forever
}
else
{
Suct_T = (Read_10K_NTC (Suct_Temp_sensor_pin)); //read Suction Temp sensor
if(DEBUG){ Suct_T = 50;}
lcd.clear();
lcd.home (); // go home
lcd.print(F("SUCT_T="));
lcd.print(Suct_T);
lcd.print(F("*F"));
delay(10000);
Suct_Superheat = Suct_T - Saturated_Temp; //calc Suction Superheat
lcd.clear();
lcd.home (); // go home
lcd.print(F("SUPRHEAT="));
lcd.print(Suct_Superheat);
lcd.print(F("*F"));
delay(10000);
}
} //end else in bounds
} //end forever loop
float mapfloat(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;
}
float convert_raw_sensor_to_PSI(float Raw_ADC, int Which_sensor)
{
//analogRead results .5v = 102, 1.3v = 266, 4.5v = 921
#define Low_Limit 266.0 //ADC of 1.3v
#define Hi_Limit 921.0 //ADC of 4.5v
#define PSI_150_min 30.0
#define PSI_300_min 60.0
#define PSI_150_max 150.0
#define PSI_300_max 300.0
float PSI, min, max;
if ((Raw_ADC >= Low_Limit) && (Raw_ADC <= Hi_Limit)) //Raw_ADC Bounds check
{
// in bounds, continue
if (Which_sensor == Sensor_150)
{
min = PSI_150_min; max = PSI_150_max;
}
else //else it's Sensor_300
{
min = PSI_300_min; max = PSI_300_max;
}
PSI = mapfloat(Raw_ADC, Low_Limit, Hi_Limit, min, max);
return (PSI);
} //end if in bounds
else {return (-1);} // error return, out of bounds
} //end of convert_raw_sensor_to_PSI
float calc_R290_Saturated_Temp(float PSI, int Which_sensor)
{
//Array element [0] contains Saturation_Temp *F for 30PSI,
//Array element [1] contains Saturation_Temp *F for 40PSI,...................>
//Array element [27] contains Saturation_Temp *F for 300PSI,
//Array element [28] contains Saturation_Temp *F for 310PSI
float R290_Saturation_Temp[] =
{7.93, 18.80, 28.42, 36.91, 44.59, 51.63, 58.13, 64.21, 69.90, 75.27,
80.35, 85.18, 89.78, 94.19, 98.41, 102.50, 106.40, 110.20, 113.80,
117.30, 120.80, 124.10, 127.30, 130.40, 133.50, 136.50, 139.40, 142.20,
145.00};
float Val, Next_val, Span, Saturation_Temp, decimals, work, low, hi;
int I;
if (Which_sensor == Sensor_150){ low = 30.0; hi = 150.0; }
else{ /*Sensor_300*/ low = 60.0; hi = 300.0; }
if ((PSI >= low) && (PSI <= hi)) //Bounds check
{
// in bounds, continue
//convert PSI to array index
work = ((PSI - 30.0) / 10.0);
I = work; //drop decimals to get array index
decimals = work - I; //restore decimals for interpolation between array points
Val = R290_Saturation_Temp[I]; //get array value
Next_val = R290_Saturation_Temp[I+1]; //get Next array value
Span = Next_val - Val; //Calc Span
Saturation_Temp = Val + (Span * decimals);//interpolate between array points
return (Saturation_Temp);
} //end if in bounds
else {return (-1);} // error return, out of bounds
} // end calc_R290_Saturated_Temp
int Read_10K_NTC (int which_sensor)
{
#define sample_cnt 30
float alpha = 0.9; // factor to tune
float average = 0.0;
float steinhart;
int I;
// resistance at 25 degrees C
#define THERMISTORNOMINAL 10000
// TEMP. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3892
// the value of the series resistor
#define SERIESRESISTOR 10000
for (I=0; I< sample_cnt; I++)
{
average = alpha * analogRead(which_sensor) + (1-alpha) * average;
delay(10);
}
// convert the value to resistance
average = 1023 / average - 1;
average = SERIESRESISTOR / average;
steinhart = average / THERMISTORNOMINAL; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert to C
return (round(steinhart * 1.8) + 32); // round to whole number
// & convert to F
} // end of Read_10K_NTC