Hello guys, this is my first time posting and I need help!!
My LCD readings disappear on the display after 20 iteration of my code running. I don't know what is the problem and why it does that?
// include the library code
#include <Wire.h>
#include <thermistor.h>
#include <LiquidCrystal_I2C.h>
#include <math.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 20 chars and 4 line display
float therm1val_c;
float therm2val_c;
float therm3val_c;
float therm4val_c;
float therm5val_c;
float therm6val_c;
THERMISTOR therm1val(A0, 10000, 3435, 10000);
THERMISTOR therm2val(A1, 10000, 3435, 10000);
THERMISTOR therm3val(A8, 10000, 3435, 10000); //
THERMISTOR therm4val(A9, 10000, 3435, 10000); //
THERMISTOR therm5val(A10, 10000, 3435, 10000); //
THERMISTOR therm6val(A11, 10000, 3435, 10000); //
float therm1val_f_new = 0.0;
float therm2val_f_new = 0.0;
float therm3val_f_new = 0.0;
float therm4val_f_new = 0.0;
float therm5val_f_new = 0.0;
float therm6val_f_new = 0.0;
float avgtemp1_f= 0.0;
float avgtemp2_f = 0.0;
float avgtemp3_f = 0.0;
float avgtemp4_f = 0.0;
float avgtemp5_f = 0.0;
float avgtemp6_f = 0.0;
float therm1val_f_avg_round;
float therm2val_f_avg_round;
float therm3val_f_avg_round;
float therm4val_f_avg_round;
float therm5val_f_avg_round;
float therm6val_f_avg_round;
float avgpress1= 0.0;
float avgpress2 = 0.0;
float trans1voltage;
float trans2voltage;
float trans1pressure;
float trans2pressure;
float trans1pressure_new = 0.0;
float trans2pressure_new = 0.0;
float trans1pressure_avg_int;
float trans2pressure_avg_int;
char delimiter = '\n';
#define M1 75.0 //slope for suction transducer curve
#define CONST1 37.5 //off set for suction transducer curve
#define PSIA 14.7 //psis to psia conversion
#define M2 163.25
#define CONST2 67.625
//function prototypes
float thermistor_to_temp_f(float thermistor_val);
float rounding_function(float value, int n );
float averagefunction(float &avgtemp, float thermval, float coeff1, float coeff2);
float voltage(float value);
float screen_write(int avg_int, int x, int y, int z);
/*********************************************************/
void setup()
{
lcd.init(); //initialize the lcd
lcd.backlight(); //open the backlight
lcd.clear();
lcd.setCursor (0, 0);
lcd.print ("Welcome to our lab");
lcd.setCursor (0, 1);
lcd.print("unit! Senior class");
lcd.setCursor (0, 2);
lcd.print("of 2021");
delay(2500);
Serial.begin(9600);
Serial.print("Discharge");
Serial.print(",");
Serial.print("Suction");
Serial.print(",");
Serial.print("EWT");
Serial.print(",");
Serial.print("LWT");
Serial.print(",");
Serial.print("Therm3");
Serial.print(",");
Serial.print("Therm4");
Serial.print(",");
Serial.print("Therm5");
Serial.print(",");
Serial.print("Therm6");
Serial.print("\n");
}
/*********************************************************/
void loop()
{
for (int i=0; i<10; i++){ //take 10 samples over the course of 1 second
//entering water thermistor
lcd.clear();
lcd.setCursor (0, 0); // go to the top left corner
lcd.print("Discharge"); // write this string on the top row
lcd.setCursor (0, 1); // go to the 2nd row
lcd.print("Suction");
lcd.setCursor (0, 2); // go to the third row
lcd.print("EWT");
lcd.setCursor (0, 3); // go to the fourth row
lcd.print("LWT");
lcd.setCursor (17, 2);
lcd.print(char(223));
lcd.print('F');
lcd.setCursor (17, 3);
lcd.print(char(223));
lcd.print('F');
lcd.setCursor (17, 1);
lcd.print("PSI");
lcd.setCursor (17, 0);
lcd.print("PSI");
therm1val_c = therm1val.read(); //read thermistor in celsius
float therm1val_f_avg = averagefunction(avgtemp1_f, thermistor_to_temp_f(therm1val_c), 0.75, 0.25);
therm1val_f_avg_round = rounding_function(therm1val_f_avg,1);
//leaving water thermistor
therm2val_c = therm2val.read(); //read thermistor in celsius
float therm2val_f_avg = averagefunction(avgtemp2_f, thermistor_to_temp_f(therm2val_c), 0.75, 0.25);
therm2val_f_avg_round = rounding_function(therm2val_f_avg,1);
//suction transducer
trans1voltage = voltage(analogRead(A2)); //gets transducer voltage
trans1pressure = (M1*trans1voltage) -CONST1 + PSIA; //voltage to psis conversion (offset by 1 atm or 14.7psi). add 14.7 to get psia
float trans1pressure_avg= averagefunction(avgpress1, trans1pressure, 0.95, 0.05);
trans1pressure_avg_int= rounding_function(trans1pressure_avg,0);
//discharge transducer
trans2voltage = voltage(analogRead(A3)); //gets transducer voltage
trans2pressure = (M2*trans2voltage) -CONST2; //voltage to psia conversion
float trans2pressure_avg= averagefunction(avgpress2, trans2pressure, 0.95, 0.05);
trans2pressure_avg_int= rounding_function(trans2pressure_avg,0);
delay(100);
if (therm1val_f_new != therm1val_f_avg_round) //compares new value to rounded average value
{
therm1val_f_new = screen_write(therm1val_f_avg_round, 11, 2, 15, 1);
}
if (therm2val_f_new != therm2val_f_avg_round)
{
therm2val_f_new = screen_write(therm2val_f_avg_round, 11, 3, 15, 1);
}
if (trans1pressure_new != trans1pressure_avg_int)
{
trans1pressure_new = screen_write(trans1pressure_avg_int, 11, 1, 13, 0);
}
if (trans2pressure_new != trans2pressure_avg_int)
{
trans2pressure_new = screen_write(trans2pressure_avg_int, 11, 0, 13, 0);
}
delay(2000);
lcd.clear();
lcd.setCursor (0, 0); // go to the top left corner
lcd.print("Therm3"); // write this string on the top row
lcd.setCursor (0, 1); // go to the 2nd row
lcd.print("Therm4");
lcd.setCursor (0, 2); // go to the third row
lcd.print("Therm5");
lcd.setCursor (0, 3); // go to the fourth row
lcd.print("Therm6");
lcd.setCursor (17, 2);
lcd.print(char(223));
lcd.print('F');
lcd.setCursor (17, 3);
lcd.print(char(223));
lcd.print('F');
lcd.setCursor (17, 1);
lcd.print(char(223));
lcd.print("F");
lcd.setCursor (17, 0);
lcd.print(char(223));
lcd.print("F");
therm3val_c = therm3val.read(); //read thermistor in celsius
float therm3val_f_avg = averagefunction(avgtemp3_f, thermistor_to_temp_f(therm3val_c), 0.75, 0.25);
therm3val_f_avg_round = rounding_function(therm3val_f_avg,1);
//fourth thermistor
therm4val_c = therm4val.read(); //read thermistor in celsius
float therm4val_f_avg = averagefunction(avgtemp4_f, thermistor_to_temp_f(therm4val_c), 0.75, 0.25);
therm4val_f_avg_round = rounding_function(therm4val_f_avg,1);
//Fifth thermistor
therm5val_c = therm5val.read(); //read thermistor in celsius
float therm5val_f_avg = averagefunction(avgtemp5_f, thermistor_to_temp_f(therm5val_c), 0.75, 0.25);
therm5val_f_avg_round = rounding_function(therm5val_f_avg,1);
//Sixth thermistor
therm6val_c = therm6val.read(); //read thermistor in celsius
float therm6val_f_avg = averagefunction(avgtemp6_f, thermistor_to_temp_f(therm6val_c), 0.75, 0.25);
therm6val_f_avg_round = rounding_function(therm6val_f_avg,1);
delay(100);
// }
// }
if (therm3val_f_new != therm3val_f_avg_round)
{
therm3val_f_new = screen_write(therm3val_f_avg_round, 11, 1, 13, 1); // rkm el screen 3
}
if (therm4val_f_new != therm4val_f_avg_round)
{
therm4val_f_new = screen_write(therm4val_f_avg_round, 11, 0, 13, 1); //rkm el screen 4
}
if (therm5val_f_new != therm5val_f_avg_round)
{
therm5val_f_new = screen_write(therm5val_f_avg_round, 11, 2, 15, 1); // rkm el screen 5
}
if (therm6val_f_new != therm6val_f_avg_round)
{
therm6val_f_new = screen_write(therm6val_f_avg_round, 11, 3, 15, 1); //rkm el screen 6
}
Serial.print(trans2pressure_avg_int,0);
Serial.print(",");
Serial.print(trans1pressure_avg_int,0);
Serial.print(",");
Serial.print(therm1val_f_avg_round,1);
Serial.print(",");
Serial.print(therm2val_f_avg_round,1);
Serial.print(",");
Serial.print(therm3val_f_avg_round,1);
Serial.print(",");
Serial.print(therm4val_f_avg_round,1);
Serial.print(",");
Serial.print(therm5val_f_avg_round,1);
Serial.print(",");
Serial.print(therm6val_f_avg_round,1);
Serial.print("\n");
delay(2000);
lcd.clear();
}
}
//Functions Below
//function for weighted average used for thermistors & transducers
float averagefunction(float &avgtemp, float thermval, float coeff1, float coeff2){
return avgtemp= coeff1*avgtemp + coeff2*thermval;
}
//function for thermistor positive celsius to fahrenheit
float thermistor_to_temp_f(float thermistor_val){
float temp_f= ((thermistor_val)/10.0)*1.8 + 32.0;
return temp_f;
}
//function for rounding to n decimal places
float rounding_function(float value, int n ){
int integer = round(value * pow(10.0, n)); //make into integer to later round to n decimal places
float value_round = integer / pow(10.0, n);
return value_round;
}
//function for voltage from adc value
float voltage(float value){
float voltage_value= (5.0 * value / 1023.0);
return voltage_value;
}
//function to write over and clear characters
float screen_write(float avg_round, int x, int y, int z, int n){
if (avg_round >=100) // rewrites over values greater than or equal to 100
{
lcd.setCursor (x, y); //off set location for print value
lcd.print(avg_round, n); //display n dec place
float value = avg_round;
return value;
}
else //if value less than 100, write blank space over previous last digit to remove artifact
{
lcd.setCursor(z, y); //set new cursor location for blank character
lcd.print(" ");
lcd.setCursor (x, y); //off set location for print value
lcd.print(avg_round, n); //display n dec place
float value = avg_round;
return value;
}
}
Thank you!