#include <LiquidCrystal_I2C.h> // if you don´t have I2C version of the display, use LiquidCrystal.h library instead
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
int tempo; //to store the millis() function
int rd_tm = 500; //[ms] to set time of reading
int read = 0;
float volt = 0;
float pressure = 0;
//support variable
bool debug = 0; //For print more info in the serial port
bool display = 1; //To print on diplay
void setup() {
//presentazione
if (display)
{
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.setCursor(4,0);
lcd.print("-----------");
lcd.setCursor(4,1);
lcd.print("|");
lcd.setCursor(5,1);
lcd.print("TRASDUINO");
lcd.setCursor(14,1);
lcd.print("|");
lcd.setCursor(4,2);
lcd.print("-----------");
delay(2000);
//impostazione
lcd.clear();
lcd.setCursor(1,1);
lcd.print("configurazione....");
}
Serial.begin(115200);
Serial.print("--Setup--");
pinMode(A0,INPUT);
//Set up
if (display)
{
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Sen_1(A0): ");
//Print of the interface
lcd.setCursor(16,3);
lcd.print("[V]");
//
lcd.setCursor(15,1);
lcd.print("[Bar]");
}
Serial.print("\n\n--interface printing--");
Serial.print("----------TRASDUINO-----------");
}
//this function convert the value readed by the analog port to the corrispondent voltage value
float readToVolt(int read, float scale)
{
if(debug)
{
Serial.print("| |scale -> ");
Serial.print(scale);
}
float volt = read*scale;
Serial.print("| |Volt -> ");
Serial.print(volt);
return volt;
}
//this function convert the Voltage value in Bar (linear correlation)
float VoltToBar(int volt)
{
float Bar = volt*35 -32.97; //empirical relation for the sensor
Serial.print("| |Bar -> ");
Serial.print(Bar);
return Bar;
}
void loop()
{
//start
tempo = millis();
read = analogRead(A0);
if (debug)
{
Serial.print("\n\n time ->");
Serial.print(tempo);
Serial.print("| |read ->");
Serial.print(read);
}
else
{
Serial.print("\n\n");
}
//update the display
//voltage printing
volt = readToVolt(read,0.005);
if(display)
{
lcd.setCursor(12,3);
lcd.print(volt);
}
//pressire printing
pressure = VoltToBar(volt);
if (display)
{
lcd.setCursor(10,1);
lcd.print(pressure);
}
delay(rd_tm);
}