MCUFriend 3.2 TFT (for UNO) on Mega

I have reached the point of needing advice and help.

I am designing a fuel flow meter/logger for a race car but am having trouble with the initial part of the project where I am trying to setup the TFT touchscreen pages (I am yet to concern myself with data logging or data acquisition).

I am using an Arduino Mega clone and a MCUFriend 3.2" TFT LCD for Arduino UNO in landscape. My current code sets up one page of the display with various buttons. At the moment only the first 2 buttons on the left of screen do anything and for testing just toggle the page heading between green and yellow font. The other buttons have not been programmed.

My code:

// include libraries
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include <TouchScreen.h>

// define constants for stage names
#define STAGE1 "Mooramb."
#define STAGE2 "Dev Hill"
#define STAGE3 "Big Nob"
#define STAGE4 "Smoking."
#define STAGE5 "Mansfie."
#define STAGE6 "Ned Kel."
#define STAGE7 "Mt Bull."
#define STAGE8 "Jamieson"
#define STAGE9 "Serenity"
#define STAGE10 "Transit"

// define colours by name
#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 button press pressures
#define MINPRESSURE 200
#define MAXPRESSURE 1000


MCUFRIEND_kbv tft;

// ALL Touch panels and wiring is DIFFERENT
// copy-paste results from TouchScreen_Calibr_native.ino
const int XP = 7, XM = A1, YP = A2, YM = 6; //320x480 ID=0x5310
const int TS_LEFT = 643, TS_RT = 519, TS_TOP = 427, TS_BOT = 567;

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

// define some button objects
Adafruit_GFX_Button stage1_btn, stage2_btn, stage3_btn, stage4_btn, stage5_btn, stage6_btn, stage7_btn, stage8_btn, stage9_btn, stage10_btn;

// create variables for touch screen
int pixel_x, pixel_y;     //Touch_getXY() updates global vars

bool Touch_getXY(void)
{
    TSPoint p = ts.getPoint();
    pinMode(YP, OUTPUT);      //restore shared pins
    pinMode(XM, OUTPUT);
    digitalWrite(YP, HIGH);   //because TFT control pins
    digitalWrite(XM, HIGH);
    bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
    if (pressed) {
        pixel_x = map(p.y, TS_LEFT, TS_RT, 0, tft.width()); // NOTE THIS SECTION CHANGES DEPENDING ON IF PORT OR LANDSCAPE?????????
        pixel_y = map(p.x, TS_TOP, TS_BOT, 0, tft.height()); // SEE CALIBRATION EXAMPLE SKETCH
    }
    return pressed;
}

void setup() {
    Serial.begin(9600);
    uint16_t ID = tft.readID();
    if (ID == 0xD3D3) ID = 0x9486; // write-only shield
    tft.begin(ID);
    tft.setRotation(1);            //Landscape
    tft.fillScreen(BLACK);
    
    stage1_btn.initButton(&tft, 191, 87, 71, 42, WHITE, CYAN, BLACK, "Select", 2);
    stage2_btn.initButton(&tft, 191, 139, 71, 42, WHITE, CYAN, BLACK, "Select", 2);
    stage3_btn.initButton(&tft, 191, 191, 71, 42, WHITE, CYAN, BLACK, "Select", 2);
    stage4_btn.initButton(&tft, 191, 243, 71, 42, WHITE, CYAN, BLACK, "Select", 2);
    stage5_btn.initButton(&tft, 191, 295, 71, 42, WHITE, CYAN, BLACK, "Select", 2);

    stage6_btn.initButton(&tft, 440, 87, 71, 42, WHITE, CYAN, BLACK, "Select", 2);
    stage7_btn.initButton(&tft, 440, 139, 71, 42, WHITE, CYAN, BLACK, "Select", 2);
    stage8_btn.initButton(&tft, 440, 191, 71, 42, WHITE, CYAN, BLACK, "Select", 2);
    stage9_btn.initButton(&tft, 440, 243, 71, 42, WHITE, CYAN, BLACK, "Select", 2);
    stage10_btn.initButton(&tft, 440, 295, 71, 42, WHITE, CYAN, BLACK, "Select", 2);

    stage1_btn.drawButton(false);
    stage2_btn.drawButton(false);
    stage3_btn.drawButton(false);
    stage4_btn.drawButton(false);
    stage5_btn.drawButton(false);
    stage6_btn.drawButton(false);
    stage7_btn.drawButton(false);
    stage8_btn.drawButton(false);
    stage9_btn.drawButton(false);
    stage10_btn.drawButton(false);

    /*tft.drawLine(2, 2, 479, 2, TFT_WHITE);
    tft.drawLine(2, 319, 479, 319, TFT_WHITE);
    tft.drawLine(2, 2, 2, 479, TFT_WHITE);
    tft.drawLine(479, 2, 479, 319, TFT_WHITE);*/

    // setup screen text elements
    tft.setCursor(18, 6);  tft.setTextColor(TFT_GREEN);  tft.setTextSize(5);  tft.println("STAGE SELECTION");
    tft.setCursor(5, 76);  tft.setTextColor(TFT_WHITE);  tft.setTextSize(3);  tft.println(STAGE1);
    tft.setCursor(244, 76);  tft.setTextColor(TFT_WHITE);  tft.setTextSize(3);  tft.println(STAGE6);
    tft.setCursor(5, 128);  tft.setTextColor(TFT_WHITE);  tft.setTextSize(3);  tft.println(STAGE2);
    tft.setCursor(244, 128);  tft.setTextColor(TFT_WHITE);  tft.setTextSize(3);  tft.println(STAGE7);
    tft.setCursor(5, 180);  tft.setTextColor(TFT_WHITE);  tft.setTextSize(3);  tft.println(STAGE3);
    tft.setCursor(244, 180);  tft.setTextColor(TFT_WHITE);  tft.setTextSize(3);  tft.println(STAGE8);
    tft.setCursor(5, 232);  tft.setTextColor(TFT_WHITE);  tft.setTextSize(3);  tft.println(STAGE4);
    tft.setCursor(244, 232);  tft.setTextColor(TFT_WHITE);  tft.setTextSize(3);  tft.println(STAGE9);
    tft.setCursor(5, 284);  tft.setTextColor(TFT_WHITE);  tft.setTextSize(3);  tft.println(STAGE5);
    tft.setCursor(244, 284);  tft.setTextColor(TFT_WHITE);  tft.setTextSize(3);  tft.println(STAGE10);




}

void loop() {

    bool down = Touch_getXY();
    stage1_btn.press(down && stage1_btn.contains(pixel_x, pixel_y));
    stage2_btn.press(down && stage2_btn.contains(pixel_x, pixel_y));
    if (stage1_btn.justReleased())
        stage1_btn.drawButton();
    if (stage2_btn.justReleased())
        stage2_btn.drawButton();
    if (stage1_btn.justPressed()) {
        stage1_btn.drawButton(true);
        tft.setCursor(18, 6);  tft.setTextColor(TFT_YELLOW);  tft.setTextSize(5);  tft.println("STAGE SELECTION");
    }
    if (stage2_btn.justPressed()) {
        stage2_btn.drawButton(true);
        tft.setCursor(18, 6);  tft.setTextColor(TFT_GREEN);  tft.setTextSize(5);  tft.println("STAGE SELECTION");
    }
}

I did the development work on a UNO but could not get the touch sensitive areas for the 2 buttons to line up with those buttons. Both buttons work but the touch area is only the right hand edge of the buttons or thereabouts (depends on what the latest calibration values were that I entered). I used the mcufriend.kvb calibration example program and dutifully copied the output into my code. I could not get the calibration numbers to be repeatable though. I copied the data into the line at the top;

// ALL Touch panels and wiring is DIFFERENT
// copy-paste results from TouchScreen_Calibr_native.ino
const int XP = 7, XM = A1, YP = A2, YM = 6; //320x480 ID=0x5310
const int TS_LEFT = 643, TS_RT = 519, TS_TOP = 427, TS_BOT = 567;

Do I also need to change this area;

if (pressed) {
        pixel_x = map(p.y, TS_LEFT, TS_RT, 0, tft.width()); // NOTE THIS SECTION CHANGES DEPENDING ON IF PORT OR LANDSCAPE?????????
        pixel_y = map(p.x, TS_TOP, TS_BOT, 0, tft.height()); // SEE CALIBRATION EXAMPLE SKETCH
    }

I have changed map(p.y, to map(p.x, and vice versa.

Can anybody suggest why it is not calibrating??? I have another identical screen that is cracked and that does the same.

Eventually realised that there were no digital pins spare for use with my sensors so I therefore moved to using the TFT on a Mega (still having the touch position issue).

Upon loading my code (above) I now do not have any touch functionality although the screen displays as it should. The mcufriend.kvb calibration example always shows “BROKEN TOUCHSCREEN”. The button_simple sketch displays but with no touch functionality. The example sketch graphictest_kvb works perfectly.

I really am at the limits of my ability here. I read in a thread that the data in the serial monitor after running the example sketch LCD_ID_readreg is of use in diagnostics so I have included it here;

I guess I mainly need help with getting the Mega to work with the TFT shield and then if I still have the touch alignment issue then help with that too. I think I have included all relevant info?

I note that the screen is a shield and hence plugs into fixed pins on the Arduino. Are you sure that all of the pins used on the Mega correspond exactly to the pins on the Uno in their functionality ?

UKHeliBob:
I note that the screen is a shield and hence plugs into fixed pins on the Arduino. Are you sure that all of the pins used on the Mega correspond exactly to the pins on the Uno in their functionality ?

I have made an assumption that that is most likely the source of my issues as I have read that the Mega pins, although labelled the same, have different functionality in some areas. However, I am not skilled enough to know what the differences are or how to work around them.

I had thought the UNO shield would work on a Mega but maybe it just wont. Will have to see what others say…

Some of the GPIO pins on the Uno have particular uses, such as A4 and A5 being SDA and SCL respectively and pins 10, 11, 12 and 13 being SS, MOSI, MISO and SCK.

On the Mega SDA, is pin 20 and SCL is pin 21. Similarly SS, MOSI, MISO and SCK are on pins 50 to 53

If the shield uses these pins then it will not be interchangeable between a Uno and a Mega unless you use jumper wires for connections rather than plugging it in to the Mega

OK. I do have another TFT that sits on a shield that is all made for the Mega but it looks rubbish. Maybe I will play with that unless anybody knows of a work around (sounds unlikely!).

On a related but separate question. Is there any rhyme or reason to which TFT library suits what board? They all baffle my little mind.

Righty-O. I gave up on the mcufriend/mega combo and have been playing with the 320QVT/Mega combo. I have it working nicely using the UTFT library.

However, it has developed an issue. After the touch screen has been working for sometime, if I upload a new or updated sketch the touch screen stops responding. The rest of the display is as should be. I can upload a sketch that previously worked and now it doesn’t. Is this a known issue with touch screens???

Just in case it is the cause (as it stands this sketch creates a display (page2) that contains various buttons. Pressing any button determines the value of a string then displays a new page (page1). Basic so far);

#include <UTFT.h>
#include <URTouch.h>

// define constants for stage names
/*#define STAGE1 "Mooramb."
#define STAGE2 "Dev Hill"
#define STAGE3 "Big Nob"
#define STAGE4 "Smoking."
#define STAGE5 "Mansfie."
#define STAGE6 "Ned Kel."
#define STAGE7 "Mt Bull."
#define STAGE8 "Jamieson"
#define STAGE9 "Serenity"
#define STAGE10 "Transit"*/

// list of stage names - max of 7 characters long
String STAGE[] = {"Moorab.","Dev Hil", "Big Nob", "Smoking","Mansfie", "Ned Kel", "Mt Bull", "Jamieso", "Serenit", "Transit"};

// data logging filename
String strFilename = "";


// Initialize display
// ------------------
// Set the pins to the correct ones for your development board
// -----------------------------------------------------------
// Standard Arduino Uno/2009 Shield            : <display model>,19,18,17,16
// Standard Arduino Mega/Due shield            : <display model>,38,39,40,41
// CTE TFT LCD/SD Shield for Arduino Due       : <display model>,25,26,27,28
// Teensy 3.x TFT Test Board                   : <display model>,23,22, 3, 4
// ElecHouse TFT LCD/SD Shield for Arduino Due : <display model>,22,23,31,33
//
// Remember to change the model parameter to suit your display module!
UTFT    myGLCD(ILI9341_16,38,39,40,41);

// Initialize touchscreen
// ----------------------
// Set the pins to the correct ones for your development board
// -----------------------------------------------------------
// Standard Arduino Uno/2009 Shield            : 15,10,14, 9, 8
// Standard Arduino Mega/Due shield            :  6, 5, 4, 3, 2
// CTE TFT LCD/SD Shield for Arduino Due       :  6, 5, 4, 3, 2
// Teensy 3.x TFT Test Board                   : 26,31,27,28,29
// ElecHouse TFT LCD/SD Shield for Arduino Due : 25,26,27,29,30
//
URTouch  myTouch( 6, 5, 4, 3, 2);

// Declare which fonts we will be using
extern uint8_t BigFont[];
extern uint8_t SmallFont[];
extern uint8_t SevenSegNumFont[];

int x, y;
char stCurrent[20]="";
int stCurrentLen=0;
char stLast[20]="";

/*************************
**   Custom functions   **
*************************/
void drawPage1()
{
    myGLCD.setColor(34, 160, 198);
    myGLCD.setFont(SmallFont);
    myGLCD.fillRoundRect (40, 40, 280, 200);
}
void drawPage2()
{
// Draw the left column of buttons
  for (x=0; x<5; x++)
  {
    myGLCD.setColor(34, 160, 198);
    myGLCD.setFont(SmallFont);
    myGLCD.fillRoundRect (122, 62+(x*36), 159, 88+(x*36));  //draw successive rectangles moving 52 down per one
    myGLCD.setColor(27, 27, 246);
    myGLCD.drawRoundRect (122, 62+(x*36), 159, 88+(x*36));
    myGLCD.setColor(255, 255, 255);
    myGLCD.print("OK", 132, 69+(x*36)); // use font BigFont
  }
  // Draw the right column of buttons
  for (x=0; x<5; x++)
  {
    myGLCD.setColor(34, 160, 198);
    myGLCD.fillRoundRect (281, 62+(x*36), 318, 88+(x*36));  //draw successive rectangles moving 52 down per one
    myGLCD.setColor(27, 27, 246);
    myGLCD.drawRoundRect (281, 62+(x*36), 318, 88+(x*36));
    myGLCD.setColor(255, 255, 255);
    myGLCD.print("OK", 291, 69+(x*36)); // use font BigFont
  }
  // Add Heading
  myGLCD.setColor(35, 246, 27);
  myGLCD.setFont(BigFont);
  myGLCD.print("SELECT STAGE", 60, 10); // use font BigFont

  // Add left column stage names
  for (x=0; x<5; x++)
  {
    myGLCD.setColor(255, 255, 255);
    myGLCD.setFont(BigFont);
    myGLCD.print(STAGE[x], 5, 67+(x*36)); // use font BigFont
  }

  // Add right column stage names
  for (x=0; x<5; x++)
  {
    myGLCD.setColor(255, 255, 255);
    myGLCD.setFont(BigFont);
    myGLCD.print(STAGE[x+5], 164, 67+(x*36)); // use font BigFont
  }
}

// Draw a red frame while a button is touched
void waitForIt(int x1, int y1, int x2, int y2)
{
  myGLCD.setColor(255, 0, 0);
  myGLCD.drawRoundRect (x1, y1, x2, y2);
  while (myTouch.dataAvailable())
    myTouch.read();
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (x1, y1, x2, y2);
}


/*************************
**  Required functions  **
*************************/

void setup()
{
// Initial setup
  myGLCD.InitLCD(LANDSCAPE);
  myGLCD.clrScr();

  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);

  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(0, 0, 0);
  drawPage2();

}

void loop()
{
  while (true)
  {
    if (myTouch.dataAvailable())
    {
      myTouch.read();
      x=myTouch.getX();
      y=myTouch.getY();
      
      if ((y>=62) && (y<=87))  // Row1
      {
        if ((x>=122) && (x<=158))  // Stage1
        {
          waitForIt(122, 62, 158, 87);
          strFilename = STAGE[0];
          drawPage1();
        }
        if ((x>=281) && (x<=317))  // Stage6
        {
          waitForIt(281, 62, 317, 87);
          strFilename = STAGE[5];
          drawPage1();
        }
      }

      if ((y>=98) && (y<=123))  // Row2
      {
        if ((x>=122) && (x<=158))  // Stage2
        {
          waitForIt(122, 98, 158, 123);
          strFilename = STAGE[1];
          drawPage1();
        }
        if ((x>=281) && (x<=317))  // Stage7
        {
          waitForIt(281, 98, 317, 123);
          strFilename = STAGE[6];
          drawPage1();
        }
      }

      if ((y>=134) && (y<=159))  // Row3
      {
        if ((x>=122) && (x<=158))  // Stage3
        {
          waitForIt(122, 134, 158, 159);
          strFilename = STAGE[2];
          drawPage1();
        }
        if ((x>=281) && (x<=317))  // Stage8
        {
          waitForIt(281, 134, 317, 159);
          strFilename = STAGE[7];
          drawPage1();
        }
      }

        if ((y>=170) && (y<=195))  // Row4
      {
        if ((x>=122) && (x<=158))  // Stage4
        {
          waitForIt(122, 170, 158, 195);
          strFilename = STAGE[3];
          drawPage1();
        }
        if ((x>=281) && (x<=317))  // Stage9
        {
          waitForIt(281, 170, 317, 195);
          strFilename = STAGE[8];
          drawPage1();
        }
      }

        if ((y>=206) && (y<=231))  // Row5
      {
        if ((x>=122) && (x<=158))  // Stage5
        {
          waitForIt(122, 206, 158, 231);
          strFilename = STAGE[4];
          drawPage1();
        }
        if ((x>=281) && (x<=317))  // Stage10
        {
          waitForIt(281, 206, 317, 231);
          strFilename = STAGE[9];
          drawPage1();
        }
      }
    }
  }
}

I am using the Arduino IDE 1.8.5 downloaded from the Windows store running on a Windows 10 lappy.

I don’t want to go chasing shadows if these touch displays are normally this unstable…