Looping function preventing touchscreen input

Hi all!

Im trying to create a pretty simple weather station and have been able to get a touchscreen working using the Elegoo 2.8’’ touchscreen and the DHT11 sensor. Unfortunately however for the touchscreen to work screens f1, f2, and f3 can’t loop which obviously makes the weather station much less useful if you have to manually trigger a refresh.

Any idea’s where I’m going wrong?

#include <Elegoo_GFX.h>    // Core graphics library
#include <Elegoo_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h>

#include "DHT.h"
#define DHTPIN 38
// Specified type of DHT sensor
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

#if defined(__SAM3X8E__)
#undef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif



#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

//Touch For New ILI9341 TP
#define TS_MINX 120
#define TS_MAXX 900

#define TS_MINY 70
#define TS_MAXY 920

// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
// optional
#define LCD_RESET A4


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

#define PENRADIUS 3


long crontimer = millis();
String runtime = "00:00:00";
int t_sec = 0;
int t_min = 0;
int t_hour = 0;
int fbutton_id = 0;
int clickcounter = 0;
int screen_id = 0;
int value_bar = 0;

void setup() {
  
  Serial.begin(9600);
  dht.begin();

  tft.reset();
  tft.setRotation(1);
  screen_main();
}

#define MINPRESSURE 10
#define MAXPRESSURE 1000

void loop() {

  // Reads the humidity
  float humidity = dht.readHumidity();

  float temperature = dht.readTemperature();


  

  TSPoint p = ts.getPoint();
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);

  if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {

    // scale from 0->1023 to tft.width
    p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
    p.y = (tft.height() - map(p.y, TS_MINY, TS_MAXY, tft.height(), 0));

    //Display received X an Y value from touch
    Serial.print("("); Serial.print(p.x);
    Serial.print(", "); Serial.print(p.y);
    Serial.println(")");

// ==================== Defining touch screen (START) ====================  // 

if (screen_id == 0 || screen_id == 1 || screen_id == 2 || screen_id == 3) {//Get touch on Main-Screen

                                
            // ========== button one (START) ==========  //
        
              if (p.x > 260 && p.y < 230 && p.y > 160) {  //p.x > 260 means the user can touch up to x 260. There is no other px value because this needs to extend to the bottom of the screen
                screen_f1();
              }
              
            // ========== button one (END) ==========  //

//============================================================================//

           // ========== button two (START) ==========  //

              
            else if (p.x > 260 && p.y < 230 && p.y > 60) {
              screen_f2();
            }

            // ========== button one (END) ==========  //

//============================================================================//

          // ========== button three (START) ==========  //
          
          if (p.x > 260  && p.y <= 60 && p.y >= 0) {//F4 screen action
            for (int i = 0; i <= 10; i = i + 2) {
              screen_f3();
              delay(4000);
            }
          }
    }
          
          // ========== button three (END) ==========  //




  }
}
// ==================== Defining touch screen (END) ====================  // 

// ==================== structure of the main screen (START) ====================  //
void screen_main() {
  screen_id = 0;
  tft.fillScreen(BLACK);


// ==================== structure of the main screen (END) ====================  //

     // ========== this is the weather station banner (START) ==========  //
     
                        tft.setCursor(25, 13);
                        tft.setTextColor(WHITE);
                        tft.setTextSize(3);
                        tft.println("Weather station");
                      
     // =========== this is the weather station banner (END) =========== //

//============================================================================// 

     // =================== option prompt (START) ===================  //
     
                        tft.setCursor(45, 140);
                        tft.setTextColor(WHITE);
                        tft.setTextSize(2);
                        tft.println("What would you like");
                        tft.setCursor(102, 160);
                        tft.println(" to view?");
                       
     // ==================== option prompt (END) ===================== //

//============================================================================// 

     // ================== temp only button (START) =================== // 
              
                            //Build F1 button
                            tft.setCursor(20, 200);
                            tft.setTextColor(WHITE);
                            tft.setTextSize(3);
                            tft.println("Temp");
              
      // =================== temp only button (END) =================== //

//============================================================================// 

      // ================== humid only button (START) ================= // 
              
                            //Build F2 button
                      tft.setCursor(117, 200);
                      tft.setTextColor(WHITE);
                      tft.setTextSize(3);
                      tft.println("Humid");
              
      // ================== humid only button (END) =================== //

//============================================================================// 

      // ===================== both button (START) ==================== // 
              
                      //Build F3 button
                      tft.setCursor(235, 200);
                      tft.setTextColor(WHITE);
                      tft.setTextSize(3);
                      tft.println("Both");}
              

void screen_f3() {
  screen_id = 3;
  
    // Reads the humidity
  float humidity = dht.readHumidity();

  // Reads the temp as celsius (this is by default)
  float temperature = dht.readTemperature();
  
  tft.fillScreen(BLACK);
  tft.drawLine(10, 2, 310, 2, CYAN);     // Line above weather station banner
  tft.drawLine(10, 3, 310, 3, BLACK);     // Line above weather station banner
  tft.drawLine(10, 4, 310, 4, CYAN);     // Line above weather station banner
  tft.setCursor(25, 13);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("Weather station");
  tft.drawLine(10, 42, 310, 42, CYAN);   // Line below weather station banner
  tft.drawLine(10, 43, 310, 43, BLACK);   // Line below weather station banner
  tft.drawLine(10, 44, 310, 44, CYAN);   // Line below weather station banner

  tft.setCursor(65, 55);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("Temperature");

  tft.setCursor(90, 120);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("Humidity");

  tft.drawLine(10, 190, 310, 190, CYAN);     // Line above option buttons
  tft.drawLine(10, 192, 310, 192 , CYAN);    // Line above option buttons

  tft.drawLine(10, 232, 310, 232, CYAN);     // Line below option buttons
  tft.drawLine(10, 234, 310, 234, CYAN);     // Line below option buttons

  
  
  

  tft.drawLine(219, 193, 219, 232, WHITE); // line between options
  tft.drawLine(221, 193, 221, 232, WHITE); // line between options

  tft.setCursor(20, 200);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("Temp");

  tft.setCursor(117, 200);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("Humid");

  tft.setCursor(235, 200);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("Both");

  tft.drawLine(100, 193, 100, 232, WHITE); // line between options
  tft.drawLine(102, 193, 102, 232, WHITE); // line between options

    tft.setCursor(125, 90);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println(temperature);


  tft.setCursor(90, 140);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println(humidity);

}

I suspect the following is the problem

            for (int i = 0; i <= 10; i = i + 2) {
              screen_f3();
              delay(4000);
            }

What is the purpose of the FOR loop and the huge delay() ?

If you want a responsive program you should not use delay() and you should only use FOR or WHILE loops if they will complete within a millisec.

Use millis() for timing without blocking and use IF and allow loop() to do the repetition.

Have a look at how the code is organized in Several Things at a Time

Note how each function runs very briefly and returns to loop() so the next one can be called. None of the functions tries to complete a task in one call. And there may be dozens of calls to a function before it is actually time for it to do anything.

And have a look at Using millis() for timing. A beginners guide if you need more explanation.

…R

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