Difficulty updating data on 3.5" TFTLCD

Hi, guys
I am doing a test with a DHT22 and an arduino UNO and a 3.5TFTLCD Shield "I have never worked with this LCD", and I can not update the data coming from the DHT22 on the LCD. I would really like it if you can help me select this problem.
I leave here the code as well as a photo of the problem I'm having.

I also made a FOR counter and it also does not show the correct numbers

Thank you in advance for all the help

#include <Adafruit_GFX.h> //graphix library for drawing
#include <Adafruit_TFTLCD.h> //LCD library to drive screen
#include "DHT.h" //dht library

#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0

#define LCD_RESET A4 //LCD reset to analog 4
#define DHTPIN 53 //dht22 on digital 53 (Mega)
#define DHTTYPE DHT22 //define dht type (11,21,22)

#define BLACK 0x0000 //hex colors to english
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET); //create instance of LCD called tft
DHT dht(DHTPIN, DHTTYPE); //create instance of dht called dht
String t;
void setup(void) {
Serial.begin(9600); //serial comms for debug

tft.reset(); //reset the screen

tft.begin(0x9341); //start screen using chip identifier hex
tft.setRotation(3);
tft.fillScreen(BLACK);
}

void loop(void) {

float f = dht.readTemperature(); //read temp in F from dht22
float h = dht.readHumidity(); //read humidity from dht
tft.setCursor(0, 0); //cursor to upper left

tft.setTextColor(WHITE); //set text color white
tft.setTextSize(3); //set text size to 2 (1-6)
tft.println(" Temperature Humidity"); //print header to screen

tft.drawRoundRect(10, 50, 180, 100, 6, WHITE); //draw rounded box (x,y,width,height,radius,color)
tft.drawRoundRect(220, 50, 180, 100, 6, WHITE); //draw rounded box (x,y,width,height,radius,color)

tft.setTextColor(GREEN); //set text color to green
tft.setTextSize(4); //set text size 4
tft.setCursor(40, 85); //put cursor in left box
tft.print(f); //print the temperature

tft.setCursor(240, 85); //put cursor in right box
tft.print(h); ; //print the humidity
tft.setCursor(85, 250); //put cursor in right box

for(int i=0;i<10;i++)
{
tft.setCursor(85, 250);
tft.setTextColor(GREEN); //set text color to green
tft.setTextSize(4);
tft.print(i);

delay(3000);//wait 3 seconds
}

"It doesn't work" is the least useful thing you can say here. The code does something. You need to tell us what it does. You expect the code to do something. You need to tell us what you expect it to do.

tft.print(t);                                                  //print the temperature

The temperature you stored in f? Or the uninitialized String?

Hi PaulS thanks for answering

What I want the code to do is to show the temperature and humidity on the LCD read on the DHT22 temperature sensor, and as you see in the photo it shows a strange reading!
I wish the LCD shows me the correct sensor reading!
Also the For counter does not show the correct count on the LCD, it happens just like when reading the sensor!
I would like to know what is wrong with the code so it does not work!
thanks for helping

 float f = dht.readTemperature();                            //read temp in F from dht22

So the temperature is in the variable named f

tft.print(t);                                                  //print the temperature

But you print a different variable

HI UKHeliBob!

I already corrected the error, it was me that did many tests and I forgot when I posted the post to verify this error, but even correcting this error continues with the same problem!

Anyone have any ideas how to solve this problem?

Thank you.

One thing you could do is post your latest code and latest picture. Use code tags not quotes. Your published code is missing at least one '}' at the end.
Apart from what has already been pointed out, your picture shows 3 RoundRectangles. Your code drawas only 2.

Another thing you can attempt is clean out the test area before printing a number:

 tft.setCursor(40, 85);                                         //put cursor in left box
 tft.print("        ") ;        //clean
 tft.setCursor(40, 85);
 tft.print(f);

Maybe also check that you can use tft.print() to print floats. I haven't found any restriction but have seen examples of code where people have tried all sorts of tricks to convert floats to a character representation.

HI 6v6gt thanks for answering

Well doing all the tests and updates as you posted in my code, but unfortunately it still does not work!
I've done several searches to see some example but I did not find anything that helps me.
Then put the code again, and the updates made so far, as well as a new photo.
I appreciate your help

#include <Adafruit_GFX.h>                                       //graphix library for drawing
#include <Adafruit_TFTLCD.h>                                    //LCD library to drive screen
#include "DHT.h"                                                //dht library

#define LCD_CS A3                                               // Chip Select goes to Analog 3
#define LCD_CD A2                                               // Command/Data goes to Analog 2
#define LCD_WR A1                                               // LCD Write goes to Analog 1
#define LCD_RD A0                                               // LCD Read goes to Analog 0

#define LCD_RESET A4                                            //LCD reset to analog 4
#define DHTPIN 53                                               //dht22 on digital 53 (Mega)
#define DHTTYPE DHT22                                           //define dht type (11,21,22)


#define	BLACK   0x0000                                          //hex colors to english
#define	BLUE    0x001F
#define	RED     0xF800
#define	GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET); //create instance of LCD called tft
DHT dht(DHTPIN, DHTTYPE);                                       //create instance of dht called dht
String t; 
void setup(void) {
  Serial.begin(9600);                                           //serial comms for debug

  tft.reset();                                                  //reset the screen

  tft.begin(0x9341);                                            //start screen using chip identifier hex
  tft.setRotation(3); 
  tft.fillScreen(BLACK);
}

void loop(void) {

 float f = dht.readTemperature();                            //read temp in F from dht22
  float h = dht.readHumidity();                                   //read humidity from dht                                    
  tft.setCursor(0, 0);                                          //cursor to upper left
  
 
  tft.setTextColor(WHITE);                                      //set text color white
  tft.setTextSize(3);                                           //set text size to 2 (1-6)
  tft.println(" Temperature    Humidity");                     //print header to screen


 tft.drawRoundRect(10, 50, 180, 100, 6, WHITE);                 //draw rounded box (x,y,width,height,radius,color)
 tft.drawRoundRect(220, 50, 180, 100, 6, WHITE);                //draw rounded box (x,y,width,height,radius,color)
 
 tft.setTextColor(GREEN);                                       //set text color to green
 tft.setTextSize(4);                                            //set text size 4
 tft.setCursor(40, 85); //put cursor in left box
 tft.print("       ");
 tft.setCursor(40, 85);
 tft.print(f);                                                  //print the temperature


 tft.setCursor(240, 85); 
 tft.print("       ");
 tft.setCursor(240, 85);
 tft.print(h);                                                        //print the humidity

 for(int i=0;i<10;i++)
 {
 //tft.setTextColor(GREEN);                                       //set text color to green
 //tft.setTextSize(4); 
 tft.setCursor(85,250);
 tft.print("       ");
 tft.setCursor(85,250); 
 tft.print(i); 
 delay(500);
 } 
 delay(3000);//wait 3 seconds
}

tft.print("  TEST      ");

Have you try just plain text?

Like this

tft.print("  TEST      ");

You are "printing " float variable "f" not a text , is my guess.

Set a background color for your text. If you only have a foreground color then it does not erase what was already on the screen.

2 things to try:

  1. In the loop(), output the following values using Serial.println() :
   dht.readTemperature();
   dht.readHumidity();

see what happens:

  1. As a temporary test, change the datatypes to int
  int f = dht.readTemperature();                            //read temp in F from dht22
  int h = dht.readHumidity();                                   //read humidity from dht

232:

tft.print("  TEST      ");

Have you try just plain text?

Like this

tft.print("  TEST      ");

You are "printing " float variable "f" not a text , is my guess.

HI!

What I really want is to print the data read from the DHT22 sensor and print it on the TFTLCD and not a text, but I do not know how to do it.

sprintf() is your friend.

It is part of the basic C library so it does not appear in the Arduino reference even though it is there to use.

6v6gt:
2 things to try:

  1. In the loop(), output the following values using Serial.println() :
   dht.readTemperature();

dht.readHumidity();



see what happens:

2. As a temporary test, change the datatypes to int



int f = dht.readTemperature(); //read temp in F from dht22
int h = dht.readHumidity(); //read humidity from dht

HI

I did the tests that you told me and Serial did not show anything in the serial
with the variables" f and t" as( int )in lcd does not show anything as shown in the photo.

MorganS:
sprintf() is your friend.

It is part of the basic C library so it does not appear in the Arduino reference even though it is there to use.

Hi

sprintf does not work with the(Adafruit_TFTLCD) libraries in this code.

sprintf is part of
stdio.h file you need to #include in your code
Here is an example how to do that :

/* sprintf example */
#include <stdio.h>

int main ()
{
  char buffer [50];
  int n, a=5, b=3;
  n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);
  printf ("[%s] is a string %d chars long\n",buffer,n);
  return 0;
}

Fiasgardone:
HI

I did the tests that you told me and Serial did not show anything in the serial
with the variables" f and t" as( int )in lcd does not show anything as shown in the photo.

Very odd. I was expecting to see plausible looking numbers, but with the decimal part truncated away.
But when you use Serial.print() to dump the values f and h (as floats) to the serial monitor, what do you see ? Clearly, if it is still nothing, then you must solve that before going on to look at the methods suggested to convert floats to a "printable" format. In addition, there is dtostrf().

Hi, guys

I Did all the tests with the examples of colleagues here posted but without success, now I did some more tests with the serial to see if I got any results and I actually get the serial temperature and humidity inside my house, but the LCD remains the same problem of printing the correct values
I also put photos that I took so you can see the results I had.

I am using a TFTLCD 3.5 "and the libraries of ADAFRUIT in this code, will need some specific command of this library to print on the LCD ?? I do not know and I already researched and I did not find anything!

In the FOR counter I made the test code, the counter counts but the numbers are printed one on top of the others, as shown in the photo.

If you have any idea how to solve this problem, thank you

I see that your text looks about right with 'nan' overlayed with 24.00 in the top left hand box.
Probably then, to clear the boxes for the next value, you should do something like:

tft.fillRoundRect(10, 50, 180, 100, 6, BLACK);       // fill assuming black is the current background colour          
tft.fillRoundRect(220, 50, 180, 100, 6, BLACK);

Such statements:

tft.print("       ");

to clear an area may not have been a good idea because this appears in the text colour and not the background colour.

Hi,6v6gt thanks for your help, very important to my problem!

Well .. I made changes to the code just like you did, and it really works, even the FOR counter works just as you suggested the code!

But there are still some things that have to improve, now the LCD is flashing for a few seconds, what will cause the LCD to blink?
I removed FOR but the LCD still blinks!

I wonder if that's possible because it does not work with tft.drawRoundRect(10, 50, 180, 100, 6, WHITE); ??

The picture of how the LCD is now!

And here the current code (in case you understand that the code can improve please indicate what improvements we can make)

#include <Adafruit_GFX.h>                                       //graphix library for drawing
#include <Adafruit_TFTLCD.h>                                    //LCD library to drive screen
#include "DHT.h"                                                //dht library

#define LCD_CS A3                                               // Chip Select goes to Analog 3
#define LCD_CD A2                                               // Command/Data goes to Analog 2
#define LCD_WR A1                                               // LCD Write goes to Analog 1
#define LCD_RD A0                                               // LCD Read goes to Analog 0

#define LCD_RESET A4                                            //LCD reset to analog 4
#define DHTPIN 53                                               //dht22 on digital 53 (Mega)
#define DHTTYPE DHT22                                           //define dht type (11,21,22)


#define	BLACK   0x0000                                          //hex colors to english
#define	BLUE    0x001F
#define	RED     0xF800
#define	GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET); //create instance of LCD called tft
DHT dht(DHTPIN, DHTTYPE);                                       //create instance of dht called dht

void setup(void) {
  Serial.begin(9600);                                           //serial comms for debug

  tft.reset();                                                  //reset the screen

  tft.begin(0x9341);                                            //start screen using chip identifier hex
  tft.setRotation(3); 
  tft.fillScreen(BLACK);
}
void loop(void) {
 
 float f = dht.readTemperature();                            //read temp in F from dht22
 float h = dht.readHumidity();                                //read humidity from dht 
                              
  tft.setCursor(0, 0);                                          //cursor to upper left                     //re
   
  tft.setTextColor(WHITE);                                      //set text color white
  tft.setTextSize(3);                                           //set text size to 2 (1-6)
  tft.println("Temperature Humidity");                     //print header to screen


 //tft.drawRoundRect(10, 50, 180, 100, 6, WHITE);                 //draw rounded box (x,y,width,height,radius,color)
 //tft.drawRoundRect(220, 50, 180, 100, 6, WHITE);                //draw rounded box (x,y,width,height,radius,color)
 
tft.fillRoundRect(10, 50, 180, 100, 6, BLUE);       // fill assuming black is the current background colour          
tft.fillRoundRect(220, 50, 180, 100, 6, BLUE);  

 tft.setTextColor(GREEN);                                       //set text color to green
 tft.setTextSize(4);                                            //set text size 4
 tft.setCursor(40, 85); //put cursor in left box
 tft.print(f);                                                  //print the temperature


 tft.setCursor(240, 85); 
 tft.print(h); 
 
 
 for(int i=0;i<10;i++)
 {
 tft.setTextColor(GREEN);                                       //set text color to green
 tft.setTextSize(4); 
 tft.fillRoundRect(10, 220, 180, 80, 6,BLUE);  
 tft.setCursor(85,250); 
 tft.println(i);
 Serial.print(i);  
 delay(500);
 }
 delay(1000);//wait 1 seconds
 
}