Unable to refresh temp reading in my sketch

Good Morning, I am in need of some programming assistance. I am down to the final element of my sketch and I have just not been able to make it work. I am displaying text on 5 buttons on a 2.8 TFT screen using a Mega 2560. The bottom four squares "Buttons" are active but in the top button, I simply just want to display the temperature. In my sketch, I am able to display the temp but it does not refresh because the code is in the setup. When I try to put it into a loop or use the millis function, I just get a blank screen? I know this should not be this hard but I am still new to this. Can anyone take a look and see what I am doing wrong? Thank you all in advance. BTW, I am using a DHT11 Temp and Humidity sensor.

[code]



#include <MCUFRIEND_kbv.h>
#include <Adafruit_GFX.h>
#include <Elegoo_GFX.h>                   // Core graphics library
#include <Elegoo_TFTLCD.h>                // Hardware-specific library
#include <TouchScreen.h>                  // Touch Support
#include <AccelStepper.h>
#include <DHT.h>              //DTH Library
#include <DHT_U.h>            //DTH Library
#include <Adafruit_Sensor.h>  //Adafruit Library Sensor

const byte motorInterfaceType = 1;
const byte dirPin = 50;
const byte stepPin = 48;
const int ENAPIN = 52;

const byte dirPin1 = 26;
const byte stepPin1 = 28;
const int ENAPIN1 = 24;

unsigned long startMillis;           //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 1000;   //the value is a number of milliseconds





AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin); //Attaches A4988 driver to Accel Library
AccelStepper stepper1 = AccelStepper(motorInterfaceType, stepPin1, dirPin1); //Attaches A4988 driver to Accel Library

#define DHTPIN 42
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

float temp;
float converted = 0.0;



#define readTempSensor
#define TS_MINX 920
#define TS_MINY 120
#define TS_MAXX 150
#define TS_MAXY 940
#define YP A3                             // must be an analog pin, use "An" notation!
#define XM A2                             // must be an analog pin, use "An" notation!
#define YM 9                              // can be a digital pin
#define XP 8                              // can be a digital pin

#define ENAPIN 52
#define stepPin 48
#define dirPin 50

#define ENAPIN1 24
#define stepPin1 28
#define dirPin1 26

#define ENAPIN2 42
#define stepPin2 44
#define dirPin2 46

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Elegoo_GFX_Button buttons; //Button object declaration


#define BLACK 0x0000   // macros for color (16 bit)
#define NAVY 0x000F
#define DARKGREEN 0x03E0
#define DARKCYAN 0x03EF
#define MAROON 0x7800
#define PURPLE 0x780F
#define OLIVE 0x7BE0
#define LIGHTGREY 0xC618
#define DARKGREY 0x7BEF
#define BLUE 0x001F
#define GREEN 0x07E0
#define CYAN 0x07FF
#define RED 0xF800
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define ORANGE 0xFD20
#define GREENYELLOW 0xAFE5
#define PINK 0xF81F

#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                      // Can alternately just connect to Arduino's reset pin


uint16_t identifier;                 //Store Screen Identifier

Elegoo_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);



void setup() {
  Serial.begin(9600);
  tft.reset();                            // Reset LCD
  tft.begin(0x9341);                      // Initialise LCD
  uint16_t ID = tft.readID();



  startMillis = millis();  //initial start time

  dht.begin();                           // Begin Sensor
  tft.fillScreen(BLACK);                  // Black Background
  tft.fillRect(0, 0, 240, 64, WHITE);    // First WHITE Rectange
  tft.fillRect(0, 64, 240, 64, YELLOW);      // Second YELLOW Rectangle
  tft.fillRect(0, 128, 240, 64, RED); // Third RED Rectangle
  tft.fillRect(0, 192, 240, 64, MAGENTA);    // Fourth MAGENTA Rectangle
  tft.fillRect(0, 256, 240, 64, ORANGE);    // Fith ORANGE Rectangle
  tft.drawRect(0, 0, 240, 64, BLACK);    // White borders to the rectangles
  tft.drawRect(0, 64, 240, 64, BLACK);   // White borders to the rectangles
  tft.drawRect(0, 128, 240, 64, BLACK);  // White borders to the rectangles
  tft.drawRect(0, 192, 240, 64, BLACK);  // White borders to the rectangles
  tft.drawRect(0, 256, 240, 64, BLACK);  // White borders to the rectangles
  tft.setTextColor(BLACK);                // Set Text Proporties

  tft.setTextSize(2);   //Write Temp and reading in the square
  tft.setCursor(5, 30);
  tft.println("Temperature");

  float f = dht.readTemperature(true); // Read temperature as Fahrenheit
  temp = dht.readTemperature();
  converted = ( temp * 1.8 ) + 32;
  tft.setTextColor(BLACK);
  tft.setTextSize(2);
  tft.setCursor(150, 30);
  tft.print(converted);
  tft.print("F");


  tft.setCursor(35, 90);
  tft.println("1 CHEESE SLICE");                 // Write Text on LCD
  tft.setCursor(30, 150);
  tft.println("1 SAUSAGE SLICE");
  tft.setCursor(10, 215);
  tft.println("CHEESE AND SAUSAGE ");
  tft.setCursor(60, 280);
  tft.println("SLICE MODE ");

  stepper.setMaxSpeed(5000); //Sets Max Speed
  stepper.setAcceleration(5000.0);
  stepper.setSpeed(5000); // Sets the speed of the motor

  stepper1.setMaxSpeed(1000); //Sets Max Speed
  stepper1.setAcceleration(1000.0);
  stepper1.setSpeed(1000); // Sets the speed of the motor


  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(ENAPIN, OUTPUT);

  pinMode(stepPin1, OUTPUT);
  pinMode(dirPin1, OUTPUT);
  pinMode(ENAPIN1, OUTPUT);



}

void loop() {


  TSPoint p = ts.getPoint();
  boolean led_state = false;
  if (p.z > 10 && p.z < 1000)             // Check touch validity
  {
    p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
    p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
    if (p.x > 0 && p.x < 240)
    {
      if (p.y > 64 && p.y < 128)

      {

        digitalWrite(ENAPIN, LOW);
        stepper.runSpeed();
        stepper.setCurrentPosition(0); //Start at Zero   // Touched Cheese rectangle
        stepper.runToNewPosition(-7000); //Move CCW
        delay(500);
        led_state = true;

      }

      else if (p.y > 128 && p.y < 192)
      {
        digitalWrite(ENAPIN1, LOW);
        stepper1.runSpeed();
        stepper1.setCurrentPosition(0); //Start at Zero   // Touched Sausage rectangle
        stepper1.runToNewPosition(1500); //Move CCW
        delay(500);
        led_state = true;

      }

      else if (p.y > 192 && p.y < 256)

      {
        digitalWrite(ENAPIN, LOW);
        stepper.runSpeed();
        stepper.setCurrentPosition(0); //Start at Zero   // Touched Cheese rectangle
        stepper1.setCurrentPosition(0); //Start at Zero   // Touched Sausage rectangle
        stepper.moveTo(-7000);  // Move CCW
        stepper1.moveTo(1500);  //Move CCW
        // While either is not done
        while (stepper.distanceToGo() != 0 ||
               stepper1.distanceToGo() != 0)
        {
          stepper.run();
          stepper1.run();
        }

        delay(500);
        led_state = true;

      }

      else if (p.y > 256 && p.y < 320)

      {
        digitalWrite(ENAPIN, LOW);
        stepper.runSpeed();
        stepper.setCurrentPosition(0); //Start at Zero   // Touched Cheese rectangle
        stepper1.setCurrentPosition(0); //Start at Zero   // Touched Sausage rectangle
        stepper.moveTo(-70000);  // Move CCW
        stepper1.moveTo(15000);  //Move CCW
        // While either is not done
        while (stepper.distanceToGo() != 0 ||
               stepper1.distanceToGo() != 0)
        {
          stepper.run();
          stepper1.run();
        }
        delay(500);
        led_state = true;

      }



      Serial.print("LED State ");
      Serial.println(led_state);            // Print the status of the LED
    }
    delay (100);
  }




}

Could it be a simple as you have NOT defined readTempSensor as anything at all, so it is ignored?

I will add it and try but it does display the temp as it is it just won't refresh. At this point I will try any and all suggestions. I will reply again after I try it.

I just looked at my code and I have already defined read temp sense. Thank you though.

Yes, you defined it to be nothing - I'm surprised it compiles.

Also, I can't see any code in loop to get or print the temperature.

Hmm. Ok. What would you suggest defining it as? It does compile so that is weird. I didn’t put any code for the sensor in the loop because when I try to do that it wiped out the rest of the code and I end up with a blank screen then. I think I am doing something all wrong but just don’t know what it is yet. Thanks for the response.

Sure it compiles as it is just tossed aside as never being used. You likely got a warning for it.
Actually, you must have had a reason for coding it. Seems you have a ways to go with the program.

Ok, any suggestions?

Post the code you used to display the temperature in loop.

Well, I put it back into the setup because I couldn't figure out how to make this darn thing work, sorry but I am learning as I go but here it is I think. The whole sketch is posted previously.





#define DHTPIN 42
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

float temp;
float converted = 0.0;



#define readTempSensor
#define TS_MINX 920
#define TS_MINY 120
#define TS_MAXX 150
#define TS_MAXY 940
#define YP A3                             // must be an analog pin, use "An" notation!
#define XM A2                             // must be an analog pin, use "An" notation!
#define YM 9                              // can be a digital pin
#define XP 8                              // can be a digital pin

#define ENAPIN 52
#define stepPin 48
#define dirPin 50

#define ENAPIN1 24
#define stepPin1 28
#define dirPin1 26

#define ENAPIN2 42
#define stepPin2 44
#define dirPin2 46

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Elegoo_GFX_Button buttons; //Button object declaration


#define BLACK 0x0000   // macros for color (16 bit)
#define NAVY 0x000F
#define DARKGREEN 0x03E0
#define DARKCYAN 0x03EF
#define MAROON 0x7800
#define PURPLE 0x780F
#define OLIVE 0x7BE0
#define LIGHTGREY 0xC618
#define DARKGREY 0x7BEF
#define BLUE 0x001F
#define GREEN 0x07E0
#define CYAN 0x07FF
#define RED 0xF800
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define ORANGE 0xFD20
#define GREENYELLOW 0xAFE5
#define PINK 0xF81F

#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                      // Can alternately just connect to Arduino's reset pin


uint16_t identifier;                 //Store Screen Identifier

Elegoo_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);



void setup() {
  Serial.begin(9600);
  tft.reset();                            // Reset LCD
  tft.begin(0x9341);                      // Initialise LCD
  uint16_t ID = tft.readID();



  startMillis = millis();  //initial start time

  dht.begin();                           // Begin Sensor
  tft.fillScreen(BLACK);                  // Black Background
  tft.fillRect(0, 0, 240, 64, WHITE);    // First WHITE Rectange
  tft.fillRect(0, 64, 240, 64, YELLOW);      // Second YELLOW Rectangle
  tft.fillRect(0, 128, 240, 64, RED); // Third RED Rectangle
  tft.fillRect(0, 192, 240, 64, MAGENTA);    // Fourth MAGENTA Rectangle
  tft.fillRect(0, 256, 240, 64, ORANGE);    // Fith ORANGE Rectangle
  tft.drawRect(0, 0, 240, 64, BLACK);    // White borders to the rectangles
  tft.drawRect(0, 64, 240, 64, BLACK);   // White borders to the rectangles
  tft.drawRect(0, 128, 240, 64, BLACK);  // White borders to the rectangles
  tft.drawRect(0, 192, 240, 64, BLACK);  // White borders to the rectangles
  tft.drawRect(0, 256, 240, 64, BLACK);  // White borders to the rectangles
  tft.setTextColor(BLACK);                // Set Text Proporties

  tft.setTextSize(2);   //Write Temp and reading in the square
  tft.setCursor(5, 30);
  tft.println("Temperature");

  float f = dht.readTemperature(true); // Read temperature as Fahrenheit
  temp = dht.readTemperature();
  converted = ( temp * 1.8 ) + 32;
  tft.setTextColor(BLACK);
  tft.setTextSize(2);
  tft.setCursor(150, 30);
  tft.print(converted);
  tft.print("F");


  tft.setCursor(35, 90);
  tft.println("1 CHEESE SLICE");                 // Write Text on LCD
  tft.setCursor(30, 150);
  tft.println("1 SAUSAGE SLICE");
  tft.setCursor(10, 215);
  tft.println("CHEESE AND SAUSAGE ");
  tft.setCursor(60, 280);
  tft.println("SLICE MODE ");
}

I'm pretty sure this is the part to make the sensor work (Pasted Below), All the rest of the code does what I need it to do, I know there is probably a better way but for a beginner, I am just glad that it works.

tft.setTextColor(BLACK); // Set Text Proporties

tft.setTextSize(2); //Write Temp and reading in the square
tft.setCursor(5, 30);
tft.println("Temperature");

float f = dht.readTemperature(true); // Read temperature as Fahrenheit
temp = dht.readTemperature();
converted = ( temp * 1.8 ) + 32;
tft.setTextColor(BLACK);
tft.setTextSize(2);
tft.setCursor(150, 30);
tft.print(converted);
tft.print("F");

SETUP only runs once. LOOP runs often. Your code to update needs to be inside LOOP. Hopefully with a millis() between readings since the sensors are slow (seconds).

Ok, I have just learned that a couple days ago, now my trouble is trying to execute it. I am having trouble with the correct syntax of the code…. Slowly learning. Thank you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.