Touchscreen ILI9486 uninitializes with no reason

Hello,
I recently started using touchscreens, I bought a 3.5" TFT shield, and after some issues with librairies I finally got some results with the MCU friend librairy.

I wrote a program who shows a menu and a page if we click on a text of the menu.
The problem is : for come back to the menu, I should end the "Resorcs" void doing a "break;" in a while (see program below)

But when I click on "back", I see a white screen.
While searching, I figured out that we actually uninitializes the screen when we click "back", so I have to reinitialise it, and that's a waste of time...

Do anyone have already had this problem ?

(Sorry for bad english and for the badly written program)


#include <Adafruit_GFX.h> // Hardware-specific library
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>

uint8_t YP = A3; // must be an analog pin, use "An" notation!
uint8_t XM = A2; // must be an analog pin, use "An" notation!
uint8_t YM = 9; // can be a digital pin
uint8_t XP = 8;

uint16_t TS_LEFT = 128;
uint16_t TS_RT = 894;
uint16_t TS_TOP = 949;
uint16_t TS_BOT = 108;
uint16_t identifier;

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

bool BT = false;
int Antiretour = 0;
void setup()
{
Serial.begin(9600);
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
TSPoint tp;
#define MINPRESSURE 20
#define MAXPRESSURE 1000

Serial.begin(9600);
tft.reset();
identifier = tft.readID();
if (identifier == 0xEFEF) identifier = 0x9486;
tft.begin(identifier);
tft.invertDisplay(true);
tft.setRotation(3);
tft.fillScreen(0x0000);
Serial.println("MCDU V1");
}

void loop()
{
uint16_t x = 50, y = 100;

if(Antiretour == 0) menu();

uint16_t xpos, ypos;
tp = ts.getPoint(); //tp.x, tp.y are ADC values

pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
pinMode(XP, OUTPUT);
pinMode(YM, OUTPUT);

if (tp.z > MINPRESSURE && tp.z < MAXPRESSURE) {
if((tp.x < 500) && (tp.x > 350) && (tp.y < 500)){
ThrSetup();
}
else if((tp.x < 750) && (tp.x > 600) && (tp.y < 500)){
Checklist();
}
else if((tp.x < 500) && (tp.x > 350) && (tp.y < 1000) && (tp.y > 650)){
State();
}
else if((tp.x < 750) && (tp.x > 600) && (tp.y < 1000) && (tp.y > 600)){
Resorcs();
}
}
}

void menu(){
Serial.println("Menu");
Antiretour = 1;
tft.fillScreen(0x0000);
tft.setCursor(120, 20);
tft.setTextSize(5);
tft.println("MCDU_001");
tft.setCursor(10, 125);
tft.setTextSize(3);
tft.println("- THR SETUP");
tft.setCursor(10, 225);
tft.println("- CHECKLIST");
tft.setCursor(345, 125);
tft.println("STATE -");
tft.setCursor(310, 225);
tft.println("RESORCS -");
}

void ThrSetup(){
Serial.println("Throttle Setup");
Antiretour = 0;
tft.fillScreen(0x0000);
tft.setCursor(120, 20);
tft.setTextSize(5);
tft.println("THR SETUP");
tft.setCursor(100, 200);
tft.setTextSize(2);
tft.println("Feature not implemented yet");
delay(10000);
}

void Checklist(){
Serial.println("Checklist");
tft.fillScreen(0x0000);
tft.setCursor(120, 20);
tft.setTextSize(5);
tft.println("CHECKLIST");
tft.setCursor(100, 200);
tft.setTextSize(2);
tft.println("Feature not implemented yet");
delay(10000);
Antiretour = 0;
}

void State(){
Serial.println("State");
Antiretour = 0;
tft.fillScreen(0x0000);
tft.setCursor(120, 20);
tft.setTextSize(5);
tft.println("STATE");
tft.setCursor(100, 200);
tft.setTextSize(2);
tft.println("Feature not implemented yet");
delay(10000);
}

void Resorcs(){
Serial.println("Ressources");
Antiretour = 0;
tft.fillScreen(0x0000);
tft.setCursor(120, 20);
tft.setTextSize(5);
tft.println("RESSOURCES");
tft.setCursor(10, 120);
tft.setTextSize(2);
tft.println("CL power : ");
back();
while (!((tp.z > MINPRESSURE) && (tp.z < MAXPRESSURE) && (tp.x < 500) && (tp.x > 350) && (tp.y < 1000) && (tp.y > 650))) {
tft.setCursor(150, 120);
tft.setTextSize(2);
tft.print("90");
tp = ts.getPoint();
}
}

void back(){
tft.setTextSize(3);
tft.setCursor(345, 125);
tft.println("BACK -");
}

Please upgrade the MCUFRIEND_kbv library via the Library Manager. Current Release is v2.9.8

Please study the "button_simple" example. And the "TouchScreen_Calibr_native" example.

You should map getPoint() values to the screen pixel coordinates.
Life is simpler with Adafruit_GFX_Button

Your crash problem is due to getPoint() without restoring pinMode() in Resorce()

David.

I did not think it was necessary, it works again, thank you :slight_smile:

Design your project e.g. what each screen should look like. what each button should do.

A rough pencil drawing is fine. Post photos.

I will help you to use Adafruit_GFX_Button

The "button_simple" example is easy to understand.

There is an alternative method in the other TAB. This is far more useful when you have multiple screens with multiple buttons.

David.