#include "Wire.h" //allows communication over i2c devices
#include "LiquidCrystal_I2C.h" //allows interfacing with LCD screens
#define relay1 A2
#define relay2 A3
#define menu 4
#define up 5
#define down 6
#define set 7
//Pressure sensor 1
const int pressureInput1 = A1; //select the analog input pin for the pressure transducer
const int pressureInput2 = A0;
const int pressureZero = 88.064; //analog reading of pressure transducer at 0psi
const int pressureMax = 921.6; //analog reading of pressure transducer at 100psi
const int pressuretransducermaxPSI = 100; //psi value of transducer being used
const int baudRate = 9600; //constant integer to set the baud rate for serial monitor
const int sensorreadDelay = 250; //constant integer to set the sensor read delay in milliseconds
int Ratio_CV[9] = {100 , 90 , 80 , 70 , 60, 50, 40, 30, 20};
int index_ratio=0;
String valve[2] = {"TDV_A" , "TDV_S"};
String model_A[38]= {"50x50" ,"65x65" ,"80x65" ,"80x80" ,"100x65" ,"100x80" ,"100x100",
"125x80" ,"125x100" ,"125x125" ,"150x100" ,"150x125" ,"150x150" ,"200x125" ,"200x150",
"200x200" ,"250x150" ,"250x200" ,"250x250" ,"300x150" ,"300x200" ,"300x250" ,"300x300",
"350x200" ,"350x250" ,"350x300" ,"350x350" ,"400x250" ,"400x300" ,"400x400" ,"450x250",
"450x300" ,"450x450" ,"500x350" ,"500x500" ,"600x600" ,"700x500" ,"700x700"};
int index_A;
String model_S[14]={"50x50" ,"65x65" ,"80x80" ,"100x100" ,"125x125" ,"150x150" ,
"200x200" ,"250x250" ,"300x300", "350x350","400x400","450x450","500x500","600x600"};
int index_S=0;
int CV_A[38][9]=
{
{75, 72, 67, 60, 52, 45, 36, 27, 18},
{124, 119, 110, 98, 86, 73, 59, 45, 29},
{126, 121, 113, 100, 88, 75, 60, 46, 30},
{172, 165, 153, 136, 120, 102, 82, 63, 41},
{129, 124, 115, 102, 90, 77, 61, 47, 31},
{173, 166, 155, 138, 121, 103, 82, 63, 41},
{364, 350, 325, 290, 255, 217, 173, 133, 87},
{175, 168, 156, 139, 122, 104, 83, 64, 42},
{366, 351, 327, 291, 256, 217, 174, 134, 87},
{526, 505, 470, 418, 368, 313, 250, 193, 125},
{369, 354, 329, 293, 258, 219, 175, 135, 88},
{529, 508, 472, 420, 370, 314, 252, 194, 126},
{831, 798, 742, 660, 581, 494, 395, 304, 198},
{531, 510, 474, 422, 371, 316, 253, 194, 126},
{833, 799, 743, 662, 582, 495, 396, 305, 198},
{1485, 1425, 1325, 1180, 1038, 882, 706, 544, 353},
{834, 801, 745, 663, 583, 496, 397, 305, 199},
{1486, 1426, 1326, 1180, 1039, 883, 706, 544, 354},
{2475, 2376, 2210, 1967, 1731, 1471, 1177, 906, 589},
{838, 804, 748, 666, 586, 498, 398, 307, 199},
{1487, 1427, 1327, 1181, 1040, 884, 707, 544, 354},
{2477, 2378, 2211, 1968, 1732, 1472, 1178, 907, 589},
{3076, 2953, 2746, 2444, 2151, 1828, 1462, 1126, 732},
{1487, 1428, 1328, 1182, 1040, 884, 707, 545, 354},
{2478, 2379, 2213, 1969, 1733, 1473, 1178, 907, 590},
{3077, 2954, 2747, 2445, 2152, 1829, 1463, 1127, 732},
{3570, 3427, 3187, 2837, 2496, 2122, 1698, 1307, 850},
{2479, 2380, 2213, 1970, 1733, 1473, 1179, 908, 590},
{3080, 2957, 2750, 2447, 2154, 1831, 1464, 1128, 733},
{4796, 4605, 4282, 3811, 3354, 2851, 2281, 1756, 1141},
{2482, 2383, 2216, 1972, 1736, 1475, 1180, 909, 591},
{3081, 2958, 2751, 2448, 2155, 1831, 1465, 1128, 733},
{6058, 5816, 5408, 4814, 4236, 3601, 2880, 2218, 1442},
{3576, 3433, 3192, 2841, 2500, 2125, 1700, 1309, 851},
{7479, 7180, 6677, 5943, 5230, 4445, 3566, 2738, 1780},
{10770, 10339, 9615, 8558, 7531, 6401, 5121, 3943, 2563},
{7481, 7182, 6679, 5944, 5231, 4446, 3557, 2739, 1780},
{15128, 14523, 13506, 12021, 10578, 8991, 7193, 5539, 3600}
};
int CV_S[14][9]=
{
{62, 60, 55, 49, 43, 37, 29, 23, 15},
{90, 86, 80, 72, 63, 53, 43, 33, 21},
{140, 134, 125, 111, 98, 83, 67, 51, 33},
{300, 288, 268, 238, 210, 178, 143, 110, 71},
{428, 411, 382, 340, 299, 254, 204, 157, 102},
{673, 646, 601, 535, 471, 400, 320, 246, 160},
{1200, 1152, 1071, 954, 839, 713, 571, 439, 286},
{2000, 1920, 1786, 1589, 1398, 1189, 951, 732, 476},
{2500, 2400, 2232, 1986, 1748, 1486, 1189, 915, 595},
{2900, 2784, 2589, 2304, 2028, 1724, 1379, 1062, 690},
{3900, 3744, 3482, 3099, 2727, 2318, 1854, 1428, 928},
{5200, 4992, 4643, 4132, 3636, 3091, 2473, 1904, 1237},
{7000, 6720, 6250, 5562, 4895, 4160, 3328, 2563, 1666},
{8800, 8448, 7857, 6992, 6154, 5230, 4184, 3222, 2094}
};
float pressureValue1_psi = 0;
float pressureValue2_psi = 0;
float pressureValue1_bar = 0;
float pressureValue2_bar = 0;
float pressureValue12_psi=0;
float pressureValue12_bar=0;
//set initial values for set point in both bar and PSI
float set_point1_m3_h = 123;
float set_point1_usgpm = 4.403*123;
float set_point2_m3_h = 150;
float set_point2_usgpm = 4.403*150;
float orig_dppsi = 0;
float orig_dpbar = 0;
float flowrate_m3_h = 0.0;
float flowrate_usgpm = 0.0;
int CvValue=0; // Cv value for the valve
int Ratio=100;
bool first_screen=true;
//custom datatype define to do pressure setting in different units
enum unit_pressure {psi , bar};
// default unit for pressue
unit_pressure current_unit_P = psi;
//custom datatype define to do pressure setting in different units
enum unit_flow {m3_h , usgpm};
// default unit for pressue
unit_flow current_unit_F = m3_h;
//defining variables for pressure tracking in both bar and PSI
float flowValue1_psi = 0;
float flowValue1_bar = 0;
float flowValue2_psi = 0;
float flowValue2_bar = 0;
//defining custom data type to track settings that can be modified
enum settings {set_sp1 , set_sp2 , set_valve , set_model,set_unit_F,set_unit_P};
//defaul arrow position when setting page load
settings setting_1 = set_sp1;
//temperory values that store values in setting (these can be assigned to original variables if user
// select to save settings)
float set_point1m3_h_temp =0;
float set_point2m3_h_temp =0;
float set_point1usgpm_temp =0;
float set_point2usgpm_temp =0;
float set_point1psi_temp=0;
float set_point2psi_temp=0;
float set_point1bar_temp=0;
float set_point2bar_temp=0;
String set_valve_temp;
String set_model_temp;
unit_flow temp_unit_F = m3_h;
unit_pressure temp_unit_P =psi;
String temp_F="M3/H";
String temp_P="PSI";
LiquidCrystal_I2C lcd(0x27, 20, 4); //sets the LCD I2C communication address; format(address, columns, rows)
void setup() //setup routine, runs once when system turned on or reset
{
Serial.begin(baudRate); //initializes serial communication at set baud rate bits per second
lcd.init(); //initializes the LCD screen
//turn on back light
lcd.backlight();
//setting relay pins as output
pinMode(relay1 , OUTPUT);
pinMode(relay2 , OUTPUT);
//setting buttons as input
pinMode(menu , INPUT);
pinMode(up , INPUT);
pinMode(down , INPUT);
pinMode(set , INPUT);
index_A=7;
set_valve_temp = valve[0];
set_model_temp = model_A[0];
Serial.print("VALVE=");
Serial.println(set_valve_temp);
Serial.print("MODEL=");
Serial.println(set_model_temp);
delay(100);
}
void loop() //loop routine runs over and over again forever
{
if(valve[0])
{CvValue=CV_A[index_A][index_ratio];}
else
{CvValue=CV_S[index_S][index_ratio];}
pressure_measurement();
flowrate_m3_h= (sqrt(abs(pressureValue12_psi)) / 1.167) * CvValue;
flowrate_usgpm = (sqrt(abs(pressureValue12_bar)) / 1.167) * CvValue;
//run thbelow lines if flow is set as m3/h
if(current_unit_F == m3_h)
{
Serial.print("FLOW:");
Serial.print(flowrate_m3_h, 1); //prints value from previous line to serial
Serial.println("M3/H "); //prints label to serial
Serial.print("RATIO:");
Serial.print(Ratio); //prints value from previous line to serial
Serial.println("% "); //prints label to serial
Serial.print("LIVE DP:");
Serial.print(pressureValue12_psi, 1); //prints value from previous line to serial
Serial.println("psi "); //prints label to serial
Serial.print("SP1:");
Serial.println(set_point1_m3_h , 1); //prints value from previous line to serial
Serial.print("SP2:");
Serial.println(set_point2_m3_h, 1); //prints value from previous line to serial
lcd.setCursor(0,0); //sets cursor to column 0, row 0
lcd.print("FLOW :"); //prints label
lcd.print(flowrate_m3_h, 1); //prints flowrate_psi value to lcd screen, 1 digit on float
lcd.print("M3/H "); //prints label
lcd.print(" "); //prints label
lcd.setCursor(0,1); //sets cursor to column 0, row 1
lcd.print("RATIO :"); //prints label
lcd.print(Ratio, 1); //prints Ratio value to lcd screen, 1 digit on float
lcd.print("%"); //prints label after value
lcd.print(" "); //to clear the display after large values or negatives
lcd.setCursor(0,2); //sets cursor to column 0, row 2
lcd.print("LIVE DP:"); //prints label
lcd.print(pressureValue12_psi, 1); //prints pressure value to lcd screen, 2 digit on float
lcd.print("psi"); //prints label after value
lcd.print(" "); //to clear the display after large values or negatives
lcd.setCursor(0,3); //sets cursor to column 0, row 3
lcd.print(" SP1:"); //prints label
lcd.print(set_point1_m3_h, 1); //prints pressure value to lcd screen, 2 digit on float
lcd.print(" SP2:"); //prints label after value
lcd.print(set_point2_m3_h , 1);
//turning on relay if condition meet
if(set_point1_m3_h <= flowrate_m3_h)
{
digitalWrite(relay1 , HIGH);
}
else
{//turn off relay if condition do not meet
digitalWrite(relay1 , LOW);
}
//turn on second relay if condition meet
if(set_point2_m3_h <= flowrate_m3_h)
{
digitalWrite(relay2 , HIGH);
}
else
{//turn off relay if condition do not meet
digitalWrite(relay2 , LOW);
}
delay(sensorreadDelay); //delay in milliseconds between read values
}
//run below lines if flow is set as usgpm
else
{
Serial.print("FLOW:");
Serial.print(flowrate_usgpm, 1); //prints value from previous line to serial
Serial.println("USgpm "); //prints label to serial
Serial.print("RATIO:");
Serial.print(Ratio); //prints value from previous line to serial
Serial.println("% "); //prints label to serial
Serial.print("LIVE DP:");
Serial.print(pressureValue12_bar, 1); //prints value from previous line to serial
Serial.println("psi "); //prints label to serial
Serial.print("SP1:");
Serial.println(set_point1_usgpm, 1); //prints value from previous line to serial
Serial.print("SP2:");
Serial.println(set_point2_usgpm, 1); //prints value from previous line to serial
lcd.setCursor(0,0); //sets cursor to column 0, row 0
lcd.print("FLOW :"); //prints label
lcd.print(flowrate_usgpm, 1); //prints flowrate_psi value to lcd screen, 1 digit on float
lcd.print("USgpm "); //prints label
lcd.print(" "); //prints label
lcd.setCursor(0,1); //sets cursor to column 0, row 1
lcd.print("RATIO :"); //prints label
lcd.print(Ratio, 1); //prints Ratio value to lcd screen, 1 digit on float
lcd.print("%"); //prints label after value
lcd.print(" "); //to clear the display after large values or negatives
lcd.setCursor(0,2); //sets cursor to column 0, row 2
lcd.print("LIVE DP:"); //prints label
lcd.print(pressureValue12_bar, 2); //prints pressure value to lcd screen, 2 digit on float
lcd.print("bar"); //prints label after value
lcd.print(" "); //to clear the display after large values or negatives
lcd.setCursor(0,3); //sets cursor to column 0, row 3
lcd.print(" SP1:"); //prints label
lcd.print(set_point1_m3_h , 2); //prints pressure value to lcd screen, 2 digit on float
lcd.print(" SP2:"); //prints label after value
lcd.print(set_point2_m3_h , 2);
//check if relay need to turn on
if(set_point1_usgpm <= flowrate_usgpm )
{
//turn on relay
digitalWrite(relay1 , HIGH);
}
else
{
//turn off relay if condition do not meet
digitalWrite(relay1 , LOW);
}
//check if relay need to turn on
if(set_point2_usgpm <= flowrate_usgpm)
{
digitalWrite(relay2 , HIGH);
}
else
{//turn off the relay if conditions do not meet
digitalWrite(relay2 , LOW);
}
delay(sensorreadDelay); //delay in milliseconds between read values
}
//check if menu button pressed
if(digitalRead(menu)==true)
{
setting_1 = set_sp1; //set the row selected before settings page load
lcd.clear();//clear display to remove previous values
setting_display();//display settings page
lcd.setCursor(18,0); lcd.print("<");//arrow on first line index 17
setting_page();//go to settings page to complete settings
delay(500);
}
}
//settings page function
void setting_page()
{
//setting intial values to load in setting page
setting_1 = set_sp1;
set_point1m3_h_temp=set_point1_m3_h;
set_point2m3_h_temp=set_point2_m3_h;
set_point1usgpm_temp=set_point1_usgpm;
set_point2usgpm_temp=set_point2_usgpm;
temp_unit_F=current_unit_F;
temp_unit_P=current_unit_P;
int delay_val = 1000;
while(1) //stay in this loop until settings complete
{
setting_display(); //display setting page
if (temp_unit_F == m3_h) //if unit is selected as psi run following lines
{
switch (setting_1) //switch case statement to change different settings based on user input
{
case set_sp1: // in case menu button is pressed once, update sp1 setting on up and low button
lcd.setCursor(18,0); lcd.print("<");
while(digitalRead(up) == HIGH) // if up button is pressed run following lines
{
set_point1m3_h_temp=set_point1m3_h_temp+1;
set_point1usgpm_temp=set_point1m3_h_temp/4.403;
setting_display();
lcd.setCursor(18,0); lcd.print("<");
delay(delay_val);
delay_val = delay_val - 100;
if(delay_val ==0) delay_val = 100;
}
delay_val = 1000;
while(digitalRead(down) == HIGH) //if down button is pressed run following lines
{
set_point1m3_h_temp=set_point1m3_h_temp-1;
if(set_point1m3_h_temp<0) set_point1m3_h_temp = 0;
set_point1usgpm_temp=set_point1m3_h_temp/4.403;
setting_display();
lcd.setCursor(18,0); lcd.print("<");
delay(delay_val);
delay_val = delay_val - 100;
if(delay_val ==0) delay_val = 100;
}
delay_val = 1000;
//run following lines if menu button pressed again
while(digitalRead(menu)==true)
{
setting_1 = set_sp2;//set settings value to sp2
//runing display page again and assign arrow to next line
setting_display();
lcd.setCursor(18,0); lcd.print(" ");
lcd.setCursor(18,1); lcd.print("<");
delay(500);
}
break;
case set_sp2: //code to change values for sp2
while(digitalRead(up) == HIGH) //if up button pressed run following lines
{
set_point2m3_h_temp=set_point2m3_h_temp+1;
set_point2usgpm_temp=set_point2m3_h_temp/4.403;
setting_display();
lcd.setCursor(18,1); lcd.print("<");
delay(delay_val);
delay_val = delay_val - 100;
if(delay_val ==0) delay_val = 100;
}
delay_val = 1000;
while(digitalRead(down) == HIGH) //if down button pressed run following lines
{
set_point2m3_h_temp=set_point2m3_h_temp-1;
if(set_point2m3_h_temp<0) set_point2m3_h_temp = 0;
set_point2usgpm_temp=set_point2m3_h_temp/4.403;
setting_display();
lcd.setCursor(18,1); lcd.print("<");
delay(delay_val);
delay_val = delay_val - 100;
if(delay_val ==0) delay_val = 100;
}
delay_val = 1000;
while(digitalRead(menu)==true)//if menu button pressed again
{
setting_1 = set_valve; //apply user action up and down on set_valve
//load setting page again and show arrow < on next line
setting_display();
lcd.setCursor(18,1); lcd.print(" ");
lcd.setCursor(18,2); lcd.print("<");
delay(500);
}
break;
case set_valve: // lines to do changes in valve
while(digitalRead(down) == HIGH) //if down button pressed run following lines
{
set_valve_temp=valve[0];
setting_display();
}
while(digitalRead(up) == HIGH) //if up button pressed run following lines
{
set_valve_temp=valve[1];
setting_display();
}
while(digitalRead(menu)==true)//if menu button pressed move arrow to next position
{
setting_1 = set_model;
setting_display();
lcd.setCursor(18,2); lcd.print(" ");
lcd.setCursor(18,3); lcd.print("<");
delay(500);
}
break;
case set_model: // lines to do changes in unit_P
while(digitalRead(down) == HIGH) //if down button pressed run following lines
{
lcd.clear();
lcd.setCursor(18,2); lcd.print(" ");
lcd.setCursor(18,3); lcd.print("<");
if(set_valve_temp==valve[0])
{
index_A=index_A-1;
if(index_A <=0)
{index_A=37;}
set_model_temp=model_A[index_A];
setting_display();
}
else
{
index_S=index_S-1;
if(index_S <=0)
{index_S=13;}
set_model_temp=model_S[index_S];
setting_display();
}
}
while(digitalRead(up) == HIGH) //if up button pressed run following lines
{
lcd.clear();
lcd.setCursor(18,2); lcd.print(" ");
lcd.setCursor(18,3); lcd.print("<");
if(set_valve_temp==valve[0])
{
index_A=index_A+1;
if(index_A >=38)
{index_A=0;}
set_model_temp=model_A[index_A];
setting_display();
}
else
{
index_S=index_S+1;
if(index_S >=14)
{index_S=0;}
set_model_temp=model_S[index_S];
setting_display();
}
}
while(digitalRead(menu)==true)//if menu button pressed move arrow to next position
{
setting_1 = set_unit_F;
setting_display();
lcd.clear();
first_screen=false;
lcd.setCursor(18,3); lcd.print(" ");
lcd.setCursor(18,0); lcd.print("<");
//delay(500);
setting_display();
}
break;
case set_unit_F: // lines to do changes in unit
while(digitalRead(down) == HIGH) //if down button pressed run following lines
{
temp_unit_F=m3_h;
temp_F="M3/H";
setting_display();
}
while(digitalRead(up) == HIGH) //if up button pressed run following lines
{
temp_unit_F=usgpm;
temp_F="USgpm";
setting_display();
}
while(digitalRead(menu)==true)//if menu button pressed move arrow to next position
{
setting_1 = set_unit_P;
setting_display();
lcd.setCursor(18,0); lcd.print(" ");
lcd.setCursor(18,1); lcd.print("<");
delay(500);
//lcd.clear();
first_screen=true;
}
break;
case set_unit_P: // lines to do changes in unit
while(digitalRead(down) == HIGH) //if down button pressed run following lines
{
temp_unit_P=psi;
temp_P="PSI";
setting_display();
}
while(digitalRead(up) == HIGH) //if up button pressed run following lines
{
temp_unit_P=bar;
temp_P="BAR";
setting_display();
}
while(digitalRead(menu)==true)//if menu button pressed move arrow to next position
{
setting_1 = set_sp1;
setting_display();
lcd.setCursor(18,1); lcd.print(" ");
lcd.setCursor(18,0); lcd.print("<");
delay(500);
}
break;
}
}
else //if pressure is selected as bar
{
////**************** same format as settings in psi *****************//////
/// just displying different pressure on screen
switch (setting_1) {
case set_sp1:
lcd.setCursor(18,0); lcd.print("<");
while(digitalRead(up) == HIGH)
{
set_point1bar_temp = set_point1bar_temp +1;
set_point1psi_temp = set_point1bar_temp* 0.0689476;
setting_display();
lcd.setCursor(17,0); lcd.print("<");
delay(delay_val);
delay_val = delay_val - 100;
if(delay_val ==0) delay_val = 100;
}
delay_val = 1000;
while(digitalRead(down) == HIGH)
{
set_point1bar_temp = set_point1bar_temp -1;
if(set_point1bar_temp<0) set_point1bar_temp = 0;
set_point1psi_temp = set_point1bar_temp* 0.0689476;
setting_display();
lcd.setCursor(18,0); lcd.print("<");
delay(delay_val);
delay_val = delay_val - 100;
if(delay_val ==0) delay_val = 100;
}
delay_val = 1000;
while(digitalRead(menu)==true)
{
setting_1 = set_sp2;
setting_display();
lcd.setCursor(18,0); lcd.print(" ");
lcd.setCursor(18,1); lcd.print("<");
delay(500);
}
break;
case set_sp2:
while(digitalRead(up) == HIGH)
{
set_point2bar_temp = set_point2bar_temp +1;
set_point2psi_temp = set_point2bar_temp* 0.0689476;
setting_display();
lcd.setCursor(18,1); lcd.print("<");
delay(delay_val);
delay_val = delay_val - 100;
if(delay_val ==0) delay_val = 100;
}
delay_val = 1000;
while(digitalRead(down) == HIGH)
{
set_point2bar_temp = set_point2bar_temp -1;
if(set_point2bar_temp<0) set_point2bar_temp = 0;
set_point2psi_temp = set_point2bar_temp* 0.0689476;
setting_display();
lcd.setCursor(18,1); lcd.print("<");
delay(delay_val);
delay_val = delay_val - 100;
if(delay_val ==0) delay_val = 100;
}
delay_val = 1000;
while(digitalRead(menu)==true)
{
setting_1 = set_valve;
setting_display();
lcd.setCursor(18,1); lcd.print(" ");
lcd.setCursor(18,2); lcd.print("<");
delay(500);
}
break;
case set_valve:
while(digitalRead(down) == HIGH) //if down button pressed run following lines
{
set_valve_temp=valve[0];
setting_display();
}
while(digitalRead(up) == HIGH) //if up button pressed run following lines
{
set_valve_temp=valve[1];
setting_display();
}
while(digitalRead(menu)==true)//if menu button pressed move arrow to next position
{
setting_1 = set_model;
setting_display();
lcd.setCursor(18,2); lcd.print(" ");
lcd.setCursor(18,3); lcd.print("<");
delay(500);
}
break;
case set_model: // lines to do changes in unit_P
while(digitalRead(down) == HIGH) //if down button pressed run following lines
{
lcd.clear();
lcd.setCursor(18,2); lcd.print(" ");
lcd.setCursor(18,3); lcd.print("<");
if(set_valve_temp==valve[0])
{
index_A=index_A-1;
if(index_A <=0)
{index_A=37;}
set_model_temp=model_A[index_A];
setting_display();
}
else
{
index_S=index_S-1;
if(index_S <=0)
{index_S=13;}
set_model_temp=model_S[index_S];
setting_display();
}
}
while(digitalRead(up) == HIGH) //if up button pressed run following lines
{
lcd.clear();
lcd.setCursor(18,2); lcd.print(" ");
lcd.setCursor(18,3); lcd.print("<");
if(set_valve_temp==valve[0])
{
index_A=index_A+1;
if(index_A >=38)
{index_A=0;}
set_model_temp=model_A[index_A];
setting_display();
}
else
{
index_S=index_S+1;
if(index_S >=14)
{index_S=0;}
set_model_temp=model_S[index_S];
setting_display();
}
}
while(digitalRead(menu)==true)//if menu button pressed move arrow to next position
{
setting_1 = set_unit_F;
setting_display();
lcd.clear();
first_screen=false;
lcd.setCursor(18,3); lcd.print(" ");
lcd.setCursor(18,0); lcd.print("<");
//delay(500);
setting_display();
}
break;
case set_unit_F: // lines to do changes in unit
while(digitalRead(down) == HIGH) //if down button pressed run following lines
{
temp_unit_F=m3_h;
temp_F="M3/H";
setting_display();
}
while(digitalRead(up) == HIGH) //if up button pressed run following lines
{
temp_unit_F=usgpm;
temp_F="USgpm";
setting_display();
}
while(digitalRead(menu)==true)//if menu button pressed move arrow to next position
{
setting_1 = set_unit_P;
setting_display();
lcd.setCursor(18,0); lcd.print(" ");
lcd.setCursor(18,1); lcd.print("<");
delay(500);
//lcd.clear();
first_screen=true;
}
break;
case set_unit_P: // lines to do changes in unit
while(digitalRead(down) == HIGH) //if down button pressed run following lines
{
temp_unit_P=psi;
temp_P="PSI";
setting_display();
}
while(digitalRead(up) == HIGH) //if up button pressed run following lines
{
temp_unit_P=bar;
temp_P="BAR";
setting_display();
}
while(digitalRead(menu)==true)//if menu button pressed move arrow to next position
{
setting_1 = set_sp1;
setting_display();
lcd.setCursor(18,1); lcd.print(" ");
lcd.setCursor(18,0); lcd.print("<");
delay(500);
}
break;
}
}
if(digitalRead(set)==true) // if set button pressed exit from the while loop
{
lcd.clear();
setting_1 = set_sp1;
save_display();
delay(1000);
break;
}
}
save_page();//load save page
}
void pressure_measurement()
//function to read pressure values
{
pressureValue1_psi = analogRead(pressureInput1); //reads value from input pin and assigns to variable
pressureValue2_psi = analogRead(pressureInput2);
pressureValue1_psi = ((pressureValue1_psi-pressureZero)*pressuretransducermaxPSI)/(pressureMax-pressureZero); //conversion equation to convert analog reading to psi
pressureValue2_psi = ((pressureValue2_psi-pressureZero)*pressuretransducermaxPSI)/(pressureMax-pressureZero); //conversion equation to convert analog reading to psi
if(pressureValue1_psi< 0) pressureValue1_psi = 0;
if(pressureValue2_psi< 0) pressureValue2_psi = 0;
pressureValue12_psi = pressureValue1_psi - pressureValue2_psi;
// if(pressureValue12_psi < 0) pressureValue12_psi = pressureValue12_psi*-1;
//bar to psi conversion
pressureValue1_bar = pressureValue1_psi/14.5038;
pressureValue2_bar = pressureValue2_psi/14.5038;
pressureValue12_bar = pressureValue12_psi/14.5038;
}
void setting_display()
{
if(temp_unit_F == m3_h)
{
if(((setting_1 == set_sp1)&&(first_screen==true))||(setting_1 == set_sp2)||(setting_1 == set_valve)||(setting_1 == set_model))
{
lcd.setCursor(0,0); //sets cursor to column 0, row 0
lcd.print("SET SP1:"); //prints label
lcd.print(set_point1m3_h_temp,1); //prints pressure value to lcd screen, 1 digit on float
lcd.print("M3/H"); //prints label
lcd.print(" "); //prints label
lcd.setCursor(0,1); //sets cursor to column 0, row 01
lcd.print("SET SP2:"); //prints label
lcd.print(set_point2m3_h_temp,1); //prints pressure value to lcd screen, 1 digit on float
lcd.print("M3/H"); //prints label after value
lcd.print(" "); //to clear the display after large values or negatives
lcd.setCursor(0,2); //sets cursor to column 0, row 02
lcd.print("VALVE :"); //prints label
lcd.print(set_valve_temp); //prints pressure value to lcd screen, 1 digit on float
//lcd.print(" "); //prints label after value
lcd.setCursor(0,3); //sets cursor to column 0, row 03
lcd.print("MODEL :"); //prints label
lcd.print(set_model_temp); //prints label after value
//lcd.print(" "); //prints label after value
}
if((setting_1 == set_unit_F)||(setting_1 == set_unit_P))
{
lcd.setCursor(0,0); //sets cursor to column 0, row 03
lcd.print("UNIT(F):"); //prints label
lcd.print(temp_F); //prints label after value
lcd.setCursor(0,1); //sets cursor to column 0, row 03
lcd.print("UNIT(P):"); //prints label
lcd.print(temp_P); //prints label after value
}
}
else
{
lcd.setCursor(0,0); //sets cursor to column 0, row 0
lcd.print("SET SP1:"); //prints label
lcd.print(set_point1usgpm_temp, 2); //prints pressure value to lcd screen, 1 digit on float
lcd.print("USgpm"); //prints label
lcd.print(" "); //prints label
lcd.setCursor(0,1); //sets cursor to column 0, row 1
lcd.print("SET SP2:"); //prints label
lcd.print(set_point2usgpm_temp, 2); //prints pressure value to lcd screen, 1 digit on float
lcd.print("USgpm"); //prints label after value
lcd.print(" "); //to clear the display after large values or negatives
lcd.setCursor(0,2); //sets cursor to column 0, row 2
lcd.print("VALVE :"); //prints label
lcd.print(set_valve_temp); //prints pressure value to lcd screen, 1 digit on float
lcd.print(" "); //to clear the display after large values or negatives
lcd.setCursor(0,3); //sets cursor to column 0, row 03
lcd.print("MODEL :"); //prints label
lcd.print(set_model_temp); //prints label after value
if((setting_1 == set_unit_F)||(setting_1 = set_unit_P))
{
lcd.setCursor(0,0); //sets cursor to column 0, row 03
lcd.print("UNIT(F):"); //prints label
lcd.print(temp_F); //prints label after value
lcd.setCursor(0,1); //sets cursor to column 0, row 03
lcd.print("UNIT(P):"); //prints label
lcd.print(temp_P); //prints label after value
}
}
}
void save_display()
{
lcd.setCursor(0,0); //sets cursor to column 0, row 0
lcd.print("SAVE THE VALUES?"); //prints label
lcd.setCursor(0,2); //sets cursor to column 0, row 02
lcd.print("PRESS SET TO SAVE"); //prints label
lcd.setCursor(0,3); //sets cursor to column 0, row 03
lcd.print("PRESS MENU TO CANCEL"); //prints label
}
void save_page()
{
while(1)
{
if(digitalRead(set) == HIGH) //read the button by user to save or discard settings
{
set_point1_usgpm = set_point1psi_temp;
set_point2_usgpm = set_point2psi_temp;
set_point1_m3_h = set_point1bar_temp;
set_point2_m3_h = set_point2bar_temp;
current_unit_F = temp_unit_F;
current_unit_P = temp_unit_P;
lcd.clear();
return;
}
if (digitalRead(menu) == HIGH)
{
lcd.clear();
return;
}
}
}