TFT display resets after any instruction sent to it

Hello, I am a bit confused with the state of my display right now.
It's a touchscreen shield that works with the mcufriend library, it has a screen resolution of 320x480. So the problem is, everytime I go into a loop to check if the screen is being touched in a certain range of coordinates (any range of coordinates given to it), it works fine. But the moment I send a tft.print() in the if statement, it just goes blank until a tft.reset() is executed. The touch is still responsive when the screen goes blank.

Any help will be appreciated a LOT

Here's ALL of my code:

// the regular Adafruit "TouchScreen.h" library only works on AVRs

// different mcufriend shields have Touchscreen on different pins
// and rotation.
// Run the TouchScreen_Calibr_native sketch for calibration of your shield

#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;       // hard-wired for UNO shields anyway.
#include <TouchScreen.h>

char *name = "Fcalc shield";  //edit name of shield
/*
const int XP=6,XM=A2,YP=A1,YM=7; //ID=0x9341
const int TS_LEFT=907,TS_RT=136,TS_TOP=942,TS_BOT=139;
*/

const int XP=8,XM=A2,YP=A3,YM=9; //320x480 ID=0x9486
const int TS_LEFT=123,TS_RT=909,TS_TOP=956,TS_BOT=95;

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
TSPoint tp;

// Object with String keys and String values
typedef struct _tuple { const char key; int val; } Tuple;
// ntouch means number keypad touch button coordinates

#define MINPRESSURE 200
#define MAXPRESSURE 1000

uint8_t Orientation = 0;    //PORTRAIT
uint16_t ID;

// Assign human-readable names to some common 16-bit color values:
#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
#define GRAY    0x7BEF

#define topBarColor 0xCE79
#define topTextColor BLACK
#define topLineColor 0x0000

void show_Serial(void)
{
    Serial.println(F("Most Touch Screens use pins 6, 7, A1, A2"));
    Serial.println(F("But they can be in ANY order"));
    Serial.println(F("e.g. right to left or bottom to top"));
    Serial.println(F("or wrong direction"));
    Serial.println(F("Edit name and calibration statements\n"));
    Serial.println(name);
    Serial.print(F("ID=0x"));
    Serial.println(ID, HEX);
    Serial.println("Screen is " + String(tft.width()) + "x" + String(tft.height()));
    Serial.println("Calibration is: ");
    Serial.println("LEFT = " + String(TS_LEFT) + " RT  = " + String(TS_RT));
    Serial.println("TOP  = " + String(TS_TOP)  + " BOT = " + String(TS_BOT));
    Serial.println("Wiring is always PORTRAIT");
    Serial.println("YP=" + String(YP)  + " XM=" + String(XM));
    Serial.println("YM=" + String(YM)  + " XP=" + String(XP));
}

void show_tft(void)
{
    String msg = "";
    tft.fillScreen(WHITE);
    tft.setTextColor(BLACK);
    tft.setCursor(0, 0);
    tft.setTextSize(1);
    tft.print(F("ID=0x"));
    tft.println(ID, HEX);
    tft.println("Current panel: " + String(tft.width()) + "x" + String(tft.height()));
    tft.println("");
    tft.setTextColor(BLUE);
    tft.setTextSize(2);
    tft.println("Formula Calculator v0.1");
    tft.println("Designed by Kosain");
    tft.setTextColor(BLACK);
    tft.setTextSize(1);
    tft.println("");
    tft.println("PORTRAIT Values:");
    tft.println("LEFT = " + String(TS_LEFT) + " RT  = " + String(TS_RT));
    tft.println("TOP  = " + String(TS_TOP)  + " BOT = " + String(TS_BOT));
    tft.println("\nWiring is: ");
    tft.println("YP=" + String(YP)  + " XM=" + String(XM));
    tft.println("YM=" + String(YM)  + " XP=" + String(XP));
    tft.setTextSize(2);
    tft.setTextColor(0x0320);
    tft.setCursor((tft.width() - 65) / 2, (tft.height() * 2) / 4);
    tft.print("Run >_");
    tft.setCursor(0, 0);
    tft.drawRect(110, 220, 105, 55, 0x0320);
    tft.setTextColor(BLACK, WHITE);
    tft.setCursor(0, (tft.height() * 6) / 8);
    tft.println("Touch screen for");
    tft.print("pos and depth");
    while (1) {
        tp = ts.getPoint();
        tft.setTextColor(BLACK, WHITE);
        pinMode(XM, OUTPUT);
        pinMode(YP, OUTPUT);
        if (tp.z < MINPRESSURE || tp.z > MAXPRESSURE) continue;
        if (tp.x > 402 && tp.x < 645  && tp.y > 480 && tp.y < 565) break;
        tft.setCursor(0, (tft.height() * 3) / 4);
        tft.println("tp.x=" + String(tp.x) + " tp.y=" + String(tp.y));
        if (tp.z < 540) {
          tft.setTextColor(MAGENTA, WHITE);
          msg = "Push lightly                 ";
        }
        if (tp.z < 500) {
          tft.setTextColor(RED, WHITE);
          msg = "Don't push too hard!!        ";
        }
        if (tp.z < 400) {
          tft.setTextColor(RED, WHITE);
          msg = "Don't use fingers!           ";
        }
        if (tp.z < 300) {
          tft.setTextColor(RED, WHITE);
          msg = "Multitouch isn't supported...";
        }
        if (tp.z > 540) {
          tft.setTextColor(BLACK, WHITE);
          msg = "                             ";
        }
        tft.println("tp.z=" + String(tp.z) + "       ");
        tft.println(msg);

        
    }
}


void setup(void)
{
    
    uint16_t tmp;

    tft.reset();
    ID = tft.readID();
    tft.begin(ID);
    Serial.begin(9600);
    show_Serial();
    tft.setRotation(Orientation);
    tft.fillScreen(BLACK);
    
    show_tft();

    // BOXSIZE = tft.width() / 6;

    tft.fillScreen(BLACK);
    tft.fillRect(0, 0, tft.width(), 30, BLACK);
    tft.drawLine(0, 30, tft.width(), 30, WHITE);
}

uint16_t xpos, ypos;  //screen coordinates


void touchPos(){
  // uint16_t xpos, ypos;  //screen coordinates
  tp = ts.getPoint();   //tp.x, tp.y are ADC values
  switch (Orientation) {
  case 0:
    xpos = map(tp.x, TS_LEFT, TS_RT, 0, tft.width());
    ypos = map(tp.y, TS_TOP, TS_BOT, 0, tft.height());
    break;
  case 1:
    xpos = map(tp.y, TS_TOP, TS_BOT, 0, tft.width());
    ypos = map(tp.x, TS_RT, TS_LEFT, 0, tft.height());
    break;
  case 2:
    xpos = map(tp.x, TS_RT, TS_LEFT, 0, tft.width());
    ypos = map(tp.y, TS_BOT, TS_TOP, 0, tft.height());
    break;
  case 3:
    xpos = map(tp.y, TS_BOT, TS_TOP, 0, tft.width());
    ypos = map(tp.x, TS_LEFT, TS_RT, 0, tft.height());
    break;
  }
}

void screenBtn(String type){
  if (type == "back"){
    tft.setCursor(283, 47);
    tft.fillRect(280, 40, 30, 30, 0x2800);
    tft.drawRect(279, 39, 31, 31, RED);
    tft.drawRect(280, 40, 30, 31, RED);
    tft.setTextColor(WHITE);
    tft.print("<-");
  }
  if (type == "close"){
    tft.setCursor(290, 47);
    tft.fillRect(280, 40, 30, 30, 0x2800);
    tft.drawRect(279, 39, 31, 31, RED);
    tft.drawRect(280, 40, 30, 31, RED);
    tft.setTextColor(WHITE);
    tft.print("X");
  }
}

int x1, x2, y1, y2;

bool screenBtnTouch(String type, bool debugPrintValues){
  tp = ts.getPoint();
  
  if (type == "back")
  {
    // int x1, x2, y1, y2;
    x1 = 820;
    x2 = 870;
    y1 = 838;
    y2 = 888;
  }
  
  if (debugPrintValues == true)
  {
    Serial.println(String(tp.x) + " " + String(tp.y));
    Serial.println("pressure: " + String(tp.z));
  }
  return (tp.x > x1 && tp.x < x2 && tp.y > y1 && tp.y < y2);
}

void loadScreen(String scr, bool resetTft){
  if (resetTft)
  {
    tft.begin(ID);
    tft.fillScreen(WHITE);
  }
  
  tft.fillRect(0, 0, tft.width(), 30, topBarColor);
  tft.drawLine(0, 30, tft.width(), 30, topLineColor);

  tft.setTextSize(2);
  tft.setCursor(5, 5);

  tft.setTextColor(topTextColor);
  tft.print("Waiting for GUI Refresh...");
  // tft.fillRect(0, 31, tft.width(), tft.height(), 0xF7BE);
  tft.fillRect(0, 31, tft.width(), tft.height(), WHITE);
  
  tft.setCursor(5, 5);
  
  if (scr == "home")
  {
    tft.setCursor(5, 5);
    tft.setTextColor(topTextColor);

    tft.fillRect(0, 0, tft.width(), 30, topBarColor);
    tft.print("Fcalc v0.1");
    
    tft.setCursor(0, 0);
    tft.setTextSize(3);
    tft.setTextColor(BLACK);

    tft.setCursor(15, 50);
    tft.println("Formulas:");
    tft.setTextSize(2);

    int homeBtnY = 102;
    int homeBtnXlen = 215;
    
    tft.setCursor(22, homeBtnY);
    tft.fillRect(15, homeBtnY - 8, homeBtnXlen, 30, BLACK);
    tft.setCursor(28, homeBtnY - 5);
    tft.fillRect(20, homeBtnY - 12, homeBtnXlen, 30, 0x075F);
    tft.print("Quadratic (Poly2)");

    homeBtnY += 50;
    tft.setCursor(22, homeBtnY);
    tft.fillRect(15, homeBtnY - 8, homeBtnXlen, 30, BLACK);
    tft.setCursor(28, homeBtnY - 5);
    tft.fillRect(20, homeBtnY - 12, homeBtnXlen, 30, 0x07F0);
    tft.print("Term (Tn/Un)");

    homeBtnY += 50;
    tft.setCursor(22, homeBtnY);
    tft.fillRect(15, homeBtnY - 8, homeBtnXlen, 30, BLACK);
    tft.setCursor(28, homeBtnY - 5);
    tft.fillRect(20, homeBtnY - 12, homeBtnXlen, 30, 0x2FE0);
    tft.print("Sum of n (Sn)");
  }
  if (scr == "formulaQuad")
  {
    tft.setTextColor(BLACK);

    tft.fillRect(0, 0, tft.width(), 30, topBarColor);
    tft.setTextColor(topTextColor);
    tft.print("Fcalc v0.1");
    tft.setTextColor(0x03B0);
    tft.print(" -> Quadratic");

    tft.setCursor(0, 0);
    tft.setTextColor(BLACK);

    tft.setTextSize(3);
    tft.setCursor(15, 50);
    tft.println("Quadratic");
    tft.setTextSize(2);
    tft.setCursor(15, 75);
    tft.println("ax^2 + bx + c = 0");

    int homeBtnY = 120;
    int homeBtnXlen = 40;

    int yTBoxA, yTBoxB, yTBoxC;
    
    tft.setCursor(22, homeBtnY);
    tft.fillRect(15, homeBtnY - 8, homeBtnXlen, 30, BLACK);
    tft.setCursor(28, homeBtnY - 5);
    tft.fillRect(20, homeBtnY - 12, homeBtnXlen, 30, 0xFCB2);
    tft.print("a=");
    tft.setCursor(80, homeBtnY - 5);
    yTBoxA = homeBtnY - 5;
    tft.print("?");

    homeBtnY += 50;
    tft.setCursor(22, homeBtnY);
    tft.fillRect(15, homeBtnY - 8, homeBtnXlen, 30, BLACK);
    tft.setCursor(28, homeBtnY - 5);
    tft.fillRect(20, homeBtnY - 12, homeBtnXlen, 30, 0x97F2);
    tft.print("b=");
    yTBoxB = homeBtnY - 5;
    tft.setCursor(80, homeBtnY - 5);
    tft.print("?");

    homeBtnY += 50;
    tft.setCursor(22, homeBtnY);
    tft.fillRect(15, homeBtnY - 8, homeBtnXlen, 30, BLACK);
    tft.setCursor(28, homeBtnY - 5);
    tft.fillRect(20, homeBtnY - 12, homeBtnXlen, 30, 0x94BF);
    tft.print("c=");
    yTBoxC = homeBtnY - 5;
    tft.setCursor(80, homeBtnY - 5);
    tft.print("?");

    screenBtn("back");
  }
  if (scr == "formulaAPTerm")
  {
    tft.setTextColor(BLACK);

    tft.fillRect(0, 0, tft.width(), 30, topBarColor);
    tft.setTextColor(topTextColor);
    tft.print("Fcalc v0.1");
    tft.setTextColor(0x0408);
    tft.print(" -> AP Tn");

    tft.setCursor(0, 0);
    tft.setTextColor(BLACK);

    tft.setTextSize(3);
    tft.setCursor(15, 50);
    tft.println("Term Number");
    tft.setTextSize(2);
    tft.setCursor(15, 75);
    tft.println("Tn = a + (n-1)d");

    int homeBtnY = 120;
    int homeBtnXlen = 40;

    int yTBoxA, yTBoxB, yTBoxC;
    
    tft.setCursor(22, homeBtnY);
    tft.fillRect(15, homeBtnY - 8, homeBtnXlen, 30, BLACK);
    tft.setCursor(28, homeBtnY - 5);
    tft.fillRect(20, homeBtnY - 12, homeBtnXlen, 30, 0xFB2C);
    tft.print("a=");
    tft.setCursor(80, homeBtnY - 5);
    yTBoxA = homeBtnY - 5;
    tft.print("?");

    homeBtnY += 50;
    tft.setCursor(22, homeBtnY);
    tft.fillRect(15, homeBtnY - 8, homeBtnXlen, 30, BLACK);
    tft.setCursor(28, homeBtnY - 5);
    tft.fillRect(20, homeBtnY - 12, homeBtnXlen, 30, 0xFFEC);
    tft.print("d=");
    yTBoxB = homeBtnY - 5;
    tft.setCursor(80, homeBtnY - 5);
    tft.print("?");

    homeBtnY += 50;
    tft.setCursor(22, homeBtnY);
    tft.fillRect(15, homeBtnY - 8, homeBtnXlen, 30, BLACK);
    tft.setCursor(28, homeBtnY - 5);
    tft.fillRect(20, homeBtnY - 12, homeBtnXlen, 30, 0x94BF);
    tft.print("n=");
    yTBoxC = homeBtnY - 5;
    tft.setCursor(80, homeBtnY - 5);
    tft.print("?");

    screenBtn("back");
  }
}

int screenKeypad(bool show, String title)
{
  // action: bring up keypad
  int colorBg, colorTopLine, colorText, colorTitle;
  colorBg = 0xE73C;
  colorTopLine = BLACK;
  colorText = BLACK;
  colorTitle = BLUE;

  if (show == true)
  {
    tft.fillRect(0, 300, tft.width(), tft.height() - 300, colorBg);
    tft.drawLine(0, 300, tft.width(), 300, colorTopLine);
    tft.setCursor(5, 326);
    tft.setTextColor(colorText);
    tft.println(title);
    tft.drawRect(5, 305, tft.width() - 10, 17, BLACK);
    tft.fillRect(6, 306, tft.width() - 12, 15, WHITE);

    tft.fillRect(tft.width() - 20 - 5, tft.height() - 20 - 5, 20, 20, RED); // make keypad close button

    // keypad buttons:
    tft.setTextSize(2);
    tft.drawRect(9, 351, 30, 30, BLACK);
    tft.fillRect(10, 350, 30, 30, WHITE);
    tft.setCursor(18, 355);
    tft.print("1");

    // ntouch means numerical keypad button touch coordinates. Coordinates stored in dictionary
    // touch colliders set in a list:

    /*
    dict ntouch[] = {
      {"onex1", 153},
      {"onex2", 217},
      {"oney1", 276},
      {"oney2", 322}
    };
    */

    int onex1, onex2, oney1, oney2;
    onex1 = 153;
    onex2 = 217;
    oney1 = 276;
    oney2 = 322;

    /*
    ntouch["onex1"] = 153;
    ntouch["onex2"] = 217;
    ntouch["oney1"] = 276;
    ntouch["oney2"] = 322;
    */

    while(1)
    {
      tp = ts.getPoint();
      tft.setCursor(8, 308);
      if (tp.z < MINPRESSURE || tp.z > MAXPRESSURE) continue;
      Serial.println(String(tp.x) + " " + String(tp.y));
      
      /*
      x1 = ntouch["onex1"];
      x2 = ntouch["onex2"];
      y1 = ntouch["oney1"];
      y2 = ntouch["oney2"];
      */

      x1 = 857;
      x2 = 885;
      y1 = 113;
      y2 = 137;
      if (tp.x > x1 && tp.x < x2  && tp.y > y1 && tp.y < y2)
      {
        loadScreen("formulaQuad", true);
        Serial.println("Closed");
        break;
        return NULL;
      }

      // keypad button touch logic:

      if (tp.x > onex1 && tp.x < onex2 && tp.y > oney1 && tp.y < oney2)
      {
        tft.setTextSize(1);
        tft.print("1");
        Serial.println("One pressed");

        while(tp.z > 10) //{tp = ts.getPoint();} // wait until user lets go of button
        {
          tp = ts.getPoint();
          tft.print("hi"); // <- Over here
          // the printing of hi is a test here, it goes blank the moment it executes it
        }

        Serial.println("One released");
        
      }

      
    }
    
  }
  if (show == false)
  {
    tft.fillRect(0, 300, tft.width(), tft.height() - 300, WHITE);
  }
  
}

void loop()
{   
    // if sharing pins, you'll need to fix the directions of the touchscreen pins
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);
    // we have some minimum pressure we consider 'valid'
    // pressure of 0 means no pressing!
    
    
    int x1, x2, y1, y2;
    String screen;
    
    while(1)
    {
      loadScreen("home", true); // formula selection screen
      tp = ts.getPoint();
      String formulaButtonList[] =
      {
        "formulaQuad",
        "formulaAPTerm",
        "formulaAPSum"
      };
      
      while (1) { // logic loop for selecting formula or next screen
        tp = ts.getPoint();
        tft.setTextColor(BLACK, WHITE);
        pinMode(XM, OUTPUT);
        pinMode(YP, OUTPUT);
        if (tp.z < MINPRESSURE || tp.z > MAXPRESSURE) continue;
        // Serial.println(String(tp.x) + " " + String(tp.y));
        if (tp.x > 183 && tp.x < 694  && tp.y > 750 && tp.y < 794)
        {
          screen = formulaButtonList[0];
          break;
        }
        if (tp.x > 183 && tp.x < 694  && tp.y > 656 && tp.y < 705)
        {
          screen = formulaButtonList[1];
          break;
        }
        if (tp.x > 183 && tp.x < 694  && tp.y > 576 && tp.y < 619)
        {
          screen = formulaButtonList[2];
          //break;
        }
      }

      tft.setCursor(5, 5);
      tft.fillRect(0, 0, tft.width(), 30, topBarColor);

      // load corresponding screen:
      loadScreen(screen, false);

      tft.setTextColor(BLACK);
      tft.setTextSize(2);
      
      while(1) // logic for handling the interactivity of the screen:
      {
        // back button logic:
        tp = ts.getPoint();
        pinMode(XM, OUTPUT);
        pinMode(YP, OUTPUT);
        if (tp.z < MINPRESSURE || tp.z > MAXPRESSURE)
        {
          if (screenBtnTouch("back", false) == true)
          {
            screen = "home";
            break;
          }
        }

        if (tp.z < MINPRESSURE || tp.z > MAXPRESSURE) continue;
        // quadratic formula interactivity:
        if (screen == "formulaQuad")
        {
          tft.setTextColor(BLACK);
          tft.setTextSize(2);
          
          int a, b, c;
          
          x1 = 186;
          x2 = 258;
          y1 = 720;
          y2 = 764;

          if (tp.x > x1 && tp.x < x2  && tp.y > y1 && tp.y < y2)
          {
            int a = screenKeypad(true, "Enter value for (a).");
            
            tft.setTextColor(BLACK);
            tft.setTextSize(2);
          }
        }
      }
    }
}

Thanks in advance.

Have you identified the offending tft.print?

Also, have you tried breaking up

in 4 separate instructions to avoid using “String" function?

Are you getting stuck in one of the five while(1) loops?

I ran this code with an mcufriend TFT display and it worked fine. Only difference is that my screen is 240x320.

See, the strange part is that this line of code does not do this issue.

I'm pretty sure it isn't stuck because the output from the serial monitor proves that it does get out.

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