3,5 TFT MCUFRIEND TOUCH SCREEN, ILI9486

Hello guys. I have 3 problems and when you help me fix one of them, I will be really happy. I use it with
arduino UNO to test it but I want to use it with MEGA finally.

  1. Using display with MC_UFRIEND library.
  • I detect a place of touch but when I want to perform action with display after that (like tft.fillScreen,...),
    display is "freezed" or just flash, but screen doesnt change.
  1. So I try to fix problem one using other libraries - UTFT and URTouch
  • but in this case is screen just white :smiley:
  1. I replace UTFT library with UTFTGLUE library
  • the screen now shows something, but not, what I want :slight_smile:
  • CODES ARE TOO LONG, THE WILL BE DOWN :slight_smile:

#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv displej;
#include <TouchScreen.h>
#define MINTLAK 250
#define MAXTLAK 1000



int zadaneHesloPole[8];
byte indexPole = 0;
boolean zadavaniHeslaP;

// Promenne pro detekci a spravu tlacitek na displeji
int x_dotyk, y_dotyk;                               //=
byte stisknutoP, stiskP;                              //=
int x_souradnice, y_souradnice, pomocna;           //=   
// ===================================================

const int XP=8,XM=A2,YP=A3,YM=9; //320x480 ID=0x9486
const int TS_LEFT=122,TS_RT=899,TS_TOP=947,TS_BOT=93;

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

#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 VRCHNI 0xFFD700


void setup(void)
{

  
    Serial.begin(9600);
    uint16_t ID = displej.readID();
   
    if (ID == 0xD3D3) ID = 0x9486; // write-only shield
    
    displej.begin(ID);
    displej.setRotation(1);            //LANDSCAPE

    pinMode(YP, OUTPUT);      //restore shared pins
    pinMode(XM, OUTPUT);
    digitalWrite(YP, HIGH);   //because displej control pins
    digitalWrite(XM, HIGH);

   displej.fillRect(0,0,480,320,BLACK);
   vrchniPanel();
   zadavaniHesla();
   

}

void loop(void){
  TSPoint p = ts.getPoint();
  
   if (p.z > MINTLAK && p.z < MAXTLAK){
        x_dotyk = round(map(p.y, 947, 93, 0, 480)); 
        y_dotyk = round(map(p.x, 122, 899, 320, 0));
        zadaneHeslo();
        
        
        delay(350);
   }


  

}

// Vrchní panel
void vrchniPanel(){
    //displej.fillScreen(MAGENTA);
    displej.fillRect(0, 0, 479, 25, BLACK);
    
    displej.drawLine(0, 26, 479, 26, WHITE);
    displej.drawLine(0, 27, 479, 27, WHITE);
    
    displej.setCursor(65,1);
    displej.setTextColor(CYAN);
    displej.setTextSize(3);

    displej.print("Zabezpecovaci system");
}

void zadavaniHesla(){
     displej.fillRoundRect(80, 60, 320, 45, 10, CYAN);
     displej.drawRoundRect(80, 60, 320, 45, 10, YELLOW);
     displej.drawRoundRect(80-1, 60-1, 320+2, 47, 12, YELLOW);
     displej.setCursor(80+24,60+9);
     displej.setTextColor(BLACK);
     displej.setTextSize(4);
     displej.println("Zadej heslo");
    
    zadavaniHeslaP = true;
    
    x_souradnice = 250; 


for (int i = 0; i < 3; i++){
  y_souradnice = 140+(60*i);
  pomocna = 9-(3*i);
  Serial.print("i = ");Serial.println(i);
  for (int y = 0; y < 3; y++){
     Serial.print("y = ");Serial.println(y);
     displej.fillRoundRect(x_souradnice-(110*y), y_souradnice, 90, 45, 10, GREEN);
     displej.drawRoundRect(x_souradnice-(110*y), y_souradnice, 90, 45, 10, YELLOW);
     displej.drawRoundRect((x_souradnice-(110*y))-1, y_souradnice-1, 92, 47, 12, YELLOW);
     displej.setCursor((x_souradnice-(110*y))+36,y_souradnice+10);
     displej.print(pomocna-y); 
  }}

// Tlacitko ENTER - x 360 - 470, y 260 - 305,
     displej.fillRoundRect(360, 260, 110, 45, 10, CYAN);
     displej.drawRoundRect(360, 260, 110, 45, 10, YELLOW);
     displej.drawRoundRect(360-1, 259, 112, 47, 12, YELLOW);
     displej.setCursor(360+12,270+2);
     displej.setTextSize(3);
     displej.print("Enter");

// Tlacitko CLEAR (Smazat posledni cislici) - x 360 - 470, y 140 - 185,
     displej.fillRoundRect(360, 140, 110, 45, 10, RED);
     displej.drawRoundRect(360, 140, 110, 45, 10, YELLOW);
     displej.drawRoundRect(360-1, 140-1, 112, 47, 12, YELLOW);
     displej.setCursor(360+11,150+2);
     displej.print("Clear");        

// Tlacitko X (Smazat posledni cislici) - x 360 - 470, y 200 - 245,
     displej.fillRoundRect(360, 200, 110, 45, 10, MAGENTA);
     displej.drawRoundRect(360, 200, 110, 45, 10, YELLOW);
     displej.drawRoundRect(360-1, 200-1, 112, 47, 12, YELLOW);
     displej.setCursor(360+1,210+4);
     displej.setTextSize(2);
     displej.print("Backspace");
            
}

boolean detekceKlavesyHeslo(){

for (int i = 0; i < 3; i++){
  y_souradnice = 260 - (60*i); 
  pomocna = 1+(3*i);
   
  for (int y = 0; y < 3; y++){
    x_souradnice = 30+(110*y);
    
    if (x_dotyk > (x_souradnice-1) and x_dotyk < (x_souradnice+111) and y_dotyk > (y_souradnice-1) and y_dotyk < (y_souradnice+46)){
        stisknutoP = pomocna + y;
        y = 3;
        i = 3;
        Serial.print("Stisknuto "); Serial.println(stisknutoP);
        zobrazitHeslo();
        }

    if (x_dotyk > 360 and x_dotyk < 470 and y_dotyk > 260 and y_dotyk < 305){
        Serial.println("Stistknuto ENTER");
        delay(500);}
  }}

}

void zobrazitHeslo(){
  zadaneHesloPole[indexPole] = stisknutoP;
  indexPole++;
  // výpis x: 80 - 320, y: 60 - 105 (69 opt)
   displej.fillScreen(BLACK);
   vrchniPanel();
   Serial.println("Zobrazit heslo");
   displej.fillRoundRect(80, 60, 320, 45, 10, CYAN);
   displej.drawRoundRect(80, 60, 320, 45, 10, YELLOW);
   displej.drawRoundRect(80-1, 60-1, 320+2, 47, 12, YELLOW);
   displej.setCursor(80+24,60+9);
   displej.setTextColor(BLACK);
   displej.setTextSize(4);
   displej.println(stisknutoP);
}
     
boolean stisknuto(void)
{
    TSPoint p = ts.getPoint();
    bool stisk = (p.z > 300 && p.z < 1000);
    if (stisk) {
        x_dotyk = map(p.y, 947, 93, 0, 480); //.kbv makes sense to me
        y_dotyk = map(p.x, 122, 899, 320, 0);
        Serial.println("Detekovan stisk");
        displej.fillScreen(GREEN);
    }
    return stisk;
}
#include <UTFT.h> 
#include <URTouch.h>


//Vytvoreni objektu
UTFT displej(ILI9486, A2, A1, A3, A4, A0);
URTouch  myTouch( 6, 5, 4, 3, 2);
//UTFTGLUE displej (0x0154,A2,A1,A3,A4,A0);

extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];
//==== Defining Variables

byte zadaneHesloPole[8];
int x_dotyk, y_dotyk, x_souradnice, y_souradnice, pomocna;;
boolean zadavaniHeslaP;
char stranka;
byte stisknutoP, indexPole;

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

void setup() {
// Initial setup
  displej.InitLCD(1);
  displej.clrScr();
  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);

  vrchniPanel();  // Draws the Home Screen
  zadavaniHesla();
  stranka = '0'; // Indicates that we are at Home Screen
}

void loop() { 
  // Home Screen
  if (stranka == '0') {
    if (myTouch.dataAvailable()) {
      myTouch.read();
      x_dotyk=myTouch.getX(); // X coordinate where the screen has been pressed
      y_dotyk=myTouch.getY(); // Y coordinates where the screen has been pressed
    }
  }}

void vrchniPanel(){
    displej.setColor(BLACK); // Nastavím barvu
    displej.fillRect(0, 0, 479, 25);

    displej.setFont(BigFont);
    displej.setColor(CYAN);
    displej.setBackColor(CYAN);
    displej.print("Zabezpecovaci system", CENTER, 0);

    displej.setColor(255,255,255); // Nastavím barvu
    displej.drawLine(0, 27, 479, 27);
    displej.drawLine(0, 28, 479, 28);
}

void zadavaniHesla(){
zadavaniHeslaP = true;

  
     displej.setFont(BigFont);
     displej.setColor(CYAN);
     displej.fillRoundRect(80, 60, 400, 105);
     displej.setColor(YELLOW);
     displej.drawRoundRect(80, 60, 400, 105);
     displej.setColor(BLACK);

     displej.print("Zadej heslo", CENTER, 60+8);
    x_souradnice = 30; 
    y_souradnice = 260;
    displej.setColor(GREEN);
    
for (int i = 0; i < 3; i++){
  pomocna = 1+(3*i);
  if (i == 2)displej.setColor(CYAN);
  if (i == 0)displej.setColor(RED);
  if (i == 1)displej.setColor(MAGENTA);
  displej.fillRoundRect(360, 140+(60*i), 470, (140+(60*i))+45);
  
  for (int y = 0; y < 3; y++){
     displej.setColor(GREEN);
     displej.fillRoundRect(x_souradnice+(110*y), y_souradnice-(60*i), (x_souradnice+(110*y)) + 90, (y_souradnice-(60*i)) + 45);
     displej.setColor(YELLOW);
     displej.drawRoundRect(x_souradnice+(110*y), y_souradnice-(60*i), (x_souradnice+(110*y)) + 90, (y_souradnice-(60*i)) + 45);

     //tft.setCursor((x_souradnice+(110*y))+36, (y_souradnice-(60*i))+10);
     displej.printNumI(pomocna+y, (x_souradnice+(110*y))+36, (y_souradnice-(60*i))+10);
     }}
     displej.print("Enter", 360+12,270+2);
     displej.print("Clear", 360+11, 150+2);        
     displej.print("Backspace", 360+1,210+4);            
}

void detekceKlavesyHeslo(){
for (int i = 0; i < 3; i++){
  y_souradnice = 260 - (60*i); 
  pomocna = 1+(3*i);
   
  for (int y = 0; y < 3; y++){
    x_souradnice = 30+(110*y);
    
    if (x_dotyk > (x_souradnice-1) and x_dotyk < (x_souradnice+111) and y_dotyk > (y_souradnice-1) and y_dotyk < (y_souradnice+46)){
        stisknutoP = pomocna + y;
        y = 3;
        i = 3;
        Serial.print("Stisknuto "); Serial.println(stisknutoP);
        zobrazitHeslo();
        }
   
    if (x_dotyk > 360 and x_dotyk < 470 and y_dotyk > 260 and y_dotyk < 305){
        Serial.println("Stistknuto ENTER");
        delay(500);}
  }}

}

void zobrazitHeslo(){
  zadaneHesloPole[indexPole] = stisknutoP;
  indexPole++;
  // výpis x: 80 - 320, y: 60 - 105 (69 opt)
   displej.clrScr();
   vrchniPanel();
   displej.setColor(CYAN);
    displej.fillRoundRect(80, 60, 400, 105);
    displej.setColor(YELLOW);
    displej.drawRoundRect(80, 60, 400, 105);
    displej.setColor(BLACK);

   displej.printNumI(stisknutoP, CENTER, 60+8);
   Serial.println("Zobrazit heslo");
}
  1. you have a Blue 3.5 inch Mcufriend Shield with ILI9486.
  2. you have calibrated the Touch successfully.
  3. the Touch pins are shared with the TFT pins. You must restore XM, YP to OUTPUT after every ts.getPoint() call
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.x, TS_LEFT, TS_RT, 0, tft.width()); //.kbv makes sense to me
        pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
    }
    return pressed;
}
  1. UTFT does not support your Mcufriend TFT

  2. URTouch does not support a resistive Touch Screen.

  3. there is no point in trying to use URTouch (it expects a XPT2046 controller chip)

  4. please do not use UTFTGLUE.h for a new project.

  5. UTFTGLUE is designed for old legacy UTFT sketches.

I suggest that you study the MCUFRIEND_kbv examples

David.

I must say: I love you :3
Thank you for help, you solved my problem :slight_smile:

May I ask you for another question?
Can I print some text or number exactly to the center?

Yes, you calculate the width and height of the text by using the Adafruit_GFX method:

from Adafruit_GFX.h

void getTextBounds(const char *string, int16_t x, int16_t y,
      int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h);

then adjust the print position with tft.setCursor() before actually printing with tft.print()

It all looks a bit complicated. Just write a helper function that you can re-use in several projects.

Sit down with pencil, paper and a nice cup of tea.
Try and write your own function.

If you have a problem, paste your function to a message and ask for help.

David.