Hello,
I want to plot a graph of a pressure on an LCD Screen(Elegoo 2.8” TFT). The pressure is calculated from a Voltage reading I get from an ADC ( ADS1115). The equation for the pressure is: pressure = 10^(Voltage-5.5).
I tried it with a 16x2 LCD and it worked fine. The code I used there was:
#include <Adafruit_ADS1X15.h>
Adafruit_ADS1115 ads;
#include <LiquidCrystal.h>
int rs=7;
int en=8;
int d4=9;
int d5=10;
int d6=11;
int d7=12;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
float truevoltage;
float pressure_mbar;
void setup(void)
{
Serial.begin(9600);
Serial.println("Hello!");
Serial.println("Getting single-ended readings from AIN0..3");
Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV/ADS1015, 0.1875mV/ADS1115)");
lcd.begin(16,2);
// The ADC input range (or gain) can be changed via the following
// functions, but be careful never to exceed VDD +0.3V max, or to
// exceed the upper and lower limits if you adjust the input range!
// Setting these values incorrectly may destroy your ADC!
// ADS1015 ADS1115
// ------- -------
// ads.setGain(GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit = 3mV 0.1875mV (default)
// ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV
// ads.setGain(GAIN_TWO); // 2x gain +/- 2.048V 1 bit = 1mV 0.0625mV
// ads.setGain(GAIN_FOUR); // 4x gain +/- 1.024V 1 bit = 0.5mV 0.03125mV
// ads.setGain(GAIN_EIGHT); // 8x gain +/- 0.512V 1 bit = 0.25mV 0.015625mV
// ads.setGain(GAIN_SIXTEEN); // 16x gain +/- 0.256V 1 bit = 0.125mV 0.0078125mV
ads.setGain(GAIN_TWOTHIRDS);
if (!ads.begin()) {
Serial.println("Failed to initialize ADS.");
while (1);
}
}
void loop(void)
{
int16_t adc0, adc1, adc2, adc3;
float volts0, volts1, volts2, volts3;
float truepressure;
adc0 = ads.readADC_SingleEnded(0);
volts0 = ads.computeVolts(adc0);
truevoltage=volts0*2;
lcd.setCursor(8,0);
lcd.print("mbar");
lcd.setCursor(0,0);
pressure_mbar=pow(10,(truevoltage-5.5));
Serial.println(pressure_mbar);
lcd.print(pressure_mbar);
delay(500);
lcd.clear();
}
I looked around the internet and found this website: https://create.arduino.cc/projecthub/andreiflorian/tft-graphing-live-history-graphs-744f3b
I copied the code and used the guide on the site to change the scale of the graph. The website says to change the code under "void graph()". I did that and it works if i write a number for "pressure_mbar".
void graph()
{
pressure_mbar=1000;
//chk = DHT.read11(22);
temp = pressure_mbar;
timeBlock[valuePos] = ((millis() - 4500) / 1000);
valueBlock[valuePos] = temp;
if(proDebug)
{
Serial.println(timeBlock[valuePos]);
}
if(blockPos < 8)
{
// print the time
tft.setCursor((mark[valuePos] - 5), (originY + 16));
tft.setTextColor(graphColor, WHITE);
tft.setTextSize(1);
tft.println(timeBlock[valuePos]);
// map the value
locationBlock[valuePos] = map(temp, 0, graphRange, originY, (originY - sizeY));
// draw point
tft.fillRect((mark[valuePos] - 1), (locationBlock[valuePos] - 1), markSize, markSize, pointColor);
// try connecting to previous point
if(valuePos != 0)
{
tft.drawLine(mark[valuePos], locationBlock[valuePos], mark[(valuePos - 1)], locationBlock[(valuePos - 1)], lineColor);
}
blockPos++;
}
else
{
// clear the graph's canvas
tft.fillRect((originX + 2), (originY - sizeY), sizeX, sizeY, WHITE);
// map the value - current point
locationBlock[valuePos] = map(temp, 0, graphRange, originY, (originY - sizeY));
// draw point - current point
tft.fillRect((mark[7]), (locationBlock[valuePos] - 1), markSize, markSize, pointColor);
// draw all points
for(int i = 0; i < 8; i++)
{
tft.fillRect((mark[(blockPos - (i + 1))] - 1), (locationBlock[(valuePos - i)] - 1), markSize, markSize, pointColor);
}
// draw all the lines
for(int i = 0; i < 7; i++)
{
tft.drawLine(mark[blockPos - (i + 1)], locationBlock[valuePos - i], mark[blockPos - (i + 2)], locationBlock[valuePos - (i + 1)], lineColor);
}
// change time lables
for(int i = 0; i <= 7; i++)
{
tft.setCursor((mark[(7 - i)] - 5), (originY + 16));
tft.setTextColor(graphColor, WHITE);
tft.setTextSize(1);
tft.println(timeBlock[valuePos - i]);
}
}
valuePos++;
}
But as soon as i try to read out the ADC, it does not work anymore. I dont get a error message but the Graph does not plot any values.
What am i doing wrong?
This is the code with the lines to read out the ADC and convert the Voltage to a pressure:
void graph()
{
int16_t adc0, adc1, adc2, adc3;
float volts0, volts1, volts2, volts3;
float truepressure;
adc0 = ads.readADC_SingleEnded(0);
volts0 = ads.computeVolts(adc0);
truevoltage=volts0*2; //Voltage times two to compensate for the Voltage divider
pressure_mbar=pow(10,(truevoltage-5.5));
//chk = DHT.read11(22);
temp = pressure_mbar;
timeBlock[valuePos] = ((millis() - 4500) / 1000);
valueBlock[valuePos] = temp;
if(proDebug)
{
Serial.println(timeBlock[valuePos]);
}
if(blockPos < 8)
{
// print the time
tft.setCursor((mark[valuePos] - 5), (originY + 16));
tft.setTextColor(graphColor, WHITE);
tft.setTextSize(1);
tft.println(timeBlock[valuePos]);
// map the value
locationBlock[valuePos] = map(temp, 0, graphRange, originY, (originY - sizeY));
// draw point
tft.fillRect((mark[valuePos] - 1), (locationBlock[valuePos] - 1), markSize, markSize, pointColor);
// try connecting to previous point
if(valuePos != 0)
{
tft.drawLine(mark[valuePos], locationBlock[valuePos], mark[(valuePos - 1)], locationBlock[(valuePos - 1)], lineColor);
}
blockPos++;
}
else
{
// clear the graph's canvas
tft.fillRect((originX + 2), (originY - sizeY), sizeX, sizeY, WHITE);
// map the value - current point
locationBlock[valuePos] = map(temp, 0, graphRange, originY, (originY - sizeY));
// draw point - current point
tft.fillRect((mark[7]), (locationBlock[valuePos] - 1), markSize, markSize, pointColor);
// draw all points
for(int i = 0; i < 8; i++)
{
tft.fillRect((mark[(blockPos - (i + 1))] - 1), (locationBlock[(valuePos - i)] - 1), markSize, markSize, pointColor);
}
// draw all the lines
for(int i = 0; i < 7; i++)
{
tft.drawLine(mark[blockPos - (i + 1)], locationBlock[valuePos - i], mark[blockPos - (i + 2)], locationBlock[valuePos - (i + 1)], lineColor);
}
// change time lables
for(int i = 0; i <= 7; i++)
{
tft.setCursor((mark[(7 - i)] - 5), (originY + 16));
tft.setTextColor(graphColor, WHITE);
tft.setTextSize(1);
tft.println(timeBlock[valuePos - i]);
}
}
valuePos++;
}
What am i doing wrong? Thank you alot for your help!