TFT 2.8'' doesn't change screen

I bought a LCD TFT 2.8'' and, after a loooong googling, I get it running.
Now I have a problem and can't resolve it:
I draw a screen with one button and I want to press the button and display another screen.
For this I made two procedures with the screen 1 and screen 2 configuration. The curious is that if I call the Screen1 procedure, made a 3 seconds delay and call the Screen2 procedure in the void setup(void) everything works fine, I get the 1st screen, and after 3 seconds I get the 2nd Screen.
Now the problem is when I do this in the void loop(void) I don't get the second Screen. I know that the Screen2() procedure is called when I press the button, but the LCD don't display the Screen.
Anyone knows why?

This is the code I'm using:

#include <Adafruit_GFX.h>
#include "SWTFT.h"
#include "TouchScreen.h"
#include <stdint.h>

// 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 YP A1  // must be an analog pin, use "An" notation!
#define XM A2  // must be an analog pin, use "An" notation!
#define YM 6   // can be a digital pin
#define XP 7   // can be a digital pin
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

int screen = 1;

SWTFT tft;

void setup(void) {
  Serial.begin(9600);
  Serial.println(F("TFT LCD test"));

  pinMode(XM, OUTPUT);
  digitalWrite(XM, LOW);
  pinMode(YP, OUTPUT);
  digitalWrite(YP, HIGH);
  pinMode(YM, OUTPUT);
  digitalWrite(YM, LOW);
  pinMode(XP, OUTPUT);
  digitalWrite(XP, HIGH);

  tft.reset();

  uint16_t identifier = tft.readID();

  tft.begin(identifier);

  Serial.print(F("Text                     "));
  
  Serial.println(Screen1());
  
}

void loop(void) {

  Point p = ts.getPoint();
  int x=p.x;
  int y=p.y;
  Serial.println(" ");
  Serial.print("P.X = ");
  Serial.println(p.x);
  Serial.print("P.Y = ");
  Serial.println(p.y);
  Serial.print("P.Z = ");
  Serial.println(p.z);
  delay(2000);

  
  switch (screen) {
  
    case 1:
      if ((x > 400) && (x<800)) {
        if ((y > 500) && (y < 800)) {
          Serial.println("SCREEN 2-------");
          Screen2();
        }
      }
      
      break;
    
  }

}

unsigned long Screen1() {

  unsigned long start = micros();
  tft.fillScreen(BLACK);
  tft.setRotation(1);
  tft.setTextSize(2);

  tft.setCursor(20, 10);
  tft.setTextColor(RED, YELLOW);
  tft.print("CONTROLO TEMPERATURA!");
  tft.setCursor(10, 50);
  tft.setTextColor(WHITE);
  tft.print("Data: ");
  tft.setTextColor(RED);
  tft.print("2015-07-03 06:38");
  tft.setCursor(10, 75);
  tft.setTextColor(WHITE);
  tft.print("Temperatura: ");
  tft.setTextColor(RED);
  tft.println("37.7 oC");
  tft.setCursor(10, 100);
  tft.setTextColor(WHITE);
  tft.print("Humidade: ");
  tft.setTextColor(RED);
  tft.print("56 %");
  tft.setCursor(10, 125);
  tft.setTextColor(WHITE);
  tft.print("Dias de func.: ");
  tft.setTextColor(RED);
  tft.print("3");

  tft.fillRect(20, 150, 100, 50, RED);
  tft.drawRect(20, 150, 100, 50, RED);
  tft.setTextColor(WHITE);
  tft.setCursor(50, 165);
  tft.print("MENU");
  return micros() - start;
 
  
}


unsigned long Screen2() {

  tft.fillScreen(BLACK);  
  tft.setRotation(1);
  tft.setTextSize(2);
  tft.setCursor(20, 10);
  tft.setTextColor(BLACK, GREEN);
  tft.print("CONTROLO TEMPERATURA 2!");
  tft.setCursor(10, 50);
  tft.setTextColor(WHITE);
  tft.print("Data: ");
  tft.setTextColor(RED);
  tft.print("2015-07-03 06:38");
  tft.setCursor(10, 75);
  tft.setTextColor(WHITE);
  tft.print("Temperatura: ");
  tft.setTextColor(RED);
  tft.println("37.7 oC");
  tft.setCursor(10, 100);
  tft.setTextColor(WHITE);
  tft.print("Humidade: ");
  tft.setTextColor(RED);
  tft.print("56 %");
  tft.setCursor(10, 125);
  tft.setTextColor(WHITE);
  tft.print("Dias de func.: ");
  tft.setTextColor(BLUE);
  tft.print("3");

  tft.fillRect(20, 150, 100, 50, BLUE);
  tft.drawRect(20, 150, 100, 50, BLUE);
  tft.setTextColor(WHITE);
  tft.setCursor(50, 165);
  tft.print("MENU");
}

thanks in advance

I am guessing that you have a red shield with www.mcufriend.com legend.

These may have ILI9320, ILI9325, ILI9327, ILI9488, ST7781, S6D0154, ... controller.
The software detects which type and displays on your Serial terminal.
There are modified "Adafruit_TFTLCD.h" libraries that will cope with the varied controllers.
I suspect that your "SWTFT.h" library is one of these "modded" Adafruit.

The red shields also have a resistive Touch Screen that may be connected in different ways.
I have several variants. e.g.:
// ILI9320: YP=A2, XM=A3, YM=8, XP=9
// ILI9325: YP=A2, XM=A1, YM=6, XP=7
// ST7781 : YP=A2, XM=A1, YM=6, XP=7
// ILI9325: YP=A2, XM=A1, YM=7, XP=6

Likewise, the polarity may need inverting. Run the tftpaint program to test it.

Regarding your "sketch". You provide no mechanism for returning to Screen1 after displaying Screen2.
When you have sorted out the polarity of the Touch Screen, I would use the map() function as in the tftpaint program. Then you can detect the right place for your Menu button and react when pressure is applied.

Incidentally, has anyone ever obtained a red shield with a SPFD5408 controller ?
Or a red shield with an ILI9341 controller?

If so, please could you post a link to the website / shop.

David.

Thanks for your help David,
Yes, I’m using that shield you said, but it’s a blue one.
I think my problem has to do with the software, because, if I change the void setup(void) code to:

void setup(void) {
  Serial.begin(9600);
  Serial.println(F("TFT LCD test"));

  pinMode(XM, OUTPUT);
  digitalWrite(XM, LOW);
  pinMode(YP, OUTPUT);
  digitalWrite(YP, HIGH);
  pinMode(YM, OUTPUT);
  digitalWrite(YM, LOW);
  pinMode(XP, OUTPUT);
  digitalWrite(XP, HIGH);

  tft.reset();
  uint16_t identifier = tft.readID();
  tft.begin(identifier);
  Serial.print(F("Text                     "));
  Screen1();   // Call Screen 1
  delay(3000);  // Wait 3 seconds
  Screen2();   // Call Screen 2
  delay(3000);  //wait 3 seconds
}

This shows the screen1 and after 3 seconds it shows up the screen 2. It’s ok!

If I change the Screen2() code to:

unsigned long Screen2() {
  Serial.println("Beginning Screen 2");
  tft.fillScreen(BLACK);  
  tft.setRotation(1);
  tft.setTextSize(2);
  tft.setCursor(20, 10);
  tft.setTextColor(BLACK, GREEN);
  tft.print("CONTROLO TEMPERATURA 2!");
  tft.setCursor(10, 50);
  tft.setTextColor(WHITE);
  tft.print("Data: ");
  tft.setTextColor(RED);
  tft.print("2015-07-03 06:38");
  tft.setCursor(10, 75);
  tft.setTextColor(WHITE);
  tft.print("Temperatura: ");
  tft.setTextColor(RED);
  tft.println("37.7 oC");
  tft.setCursor(10, 100);
  tft.setTextColor(WHITE);
  tft.print("Humidade: ");
  tft.setTextColor(RED);
  tft.print("56 %");
  tft.setCursor(10, 125);
  tft.setTextColor(WHITE);
  tft.print("Dias de func.: ");
  tft.setTextColor(BLUE);
  tft.print("3");

  tft.fillRect(20, 150, 100, 50, BLUE);
  tft.drawRect(20, 150, 100, 50, BLUE);
  tft.setTextColor(WHITE);
  tft.setCursor(50, 165);
  tft.print("MENU");

  Serial.println("End Screen 2");
}

After pressing the button on Screen1, I get (in the Serial Monitor) the messages:
Beginning Screen 2
End Screen 2

This means the Sreen2() procedure is called, but the LCD doesn't show up the Screen2 (stays on Screen1).

I’m stucked here :frowning:
Any guess?

Finally I get it work! :slight_smile:

I found the solution in this topic:
http://forum.arduino.cc/index.php?topic=336058.0

After the Point p = ts.getPoint(); I have to add the following lines:

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

So, the complete working code is:

#include <Adafruit_GFX.h>
#include "SWTFT.h"
#include "TouchScreen.h"
#include <stdint.h>

// 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 YP A1  // must be an analog pin, use "An" notation!
#define XM A2  // must be an analog pin, use "An" notation!
#define YM 6   // can be a digital pin
#define XP 7   // can be a digital pin
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

int screen = 1;

SWTFT tft;

void setup(void) {
  Serial.begin(9600);
  Serial.println(F("TFT LCD test"));

  pinMode(XM, OUTPUT);
  digitalWrite(XM, LOW);
  pinMode(YP, OUTPUT);
  digitalWrite(YP, HIGH);
  pinMode(YM, OUTPUT);
  digitalWrite(YM, LOW);
  pinMode(XP, OUTPUT);
  digitalWrite(XP, HIGH);

  tft.reset();

  uint16_t identifier = tft.readID();

  tft.begin(identifier);

  Serial.print(F("Text                     "));
  
  Serial.println(Screen1());
  
}

void loop(void) {

  Point p = ts.getPoint();
  int x=p.x;
  int y=p.y;
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);
  
  switch (screen) {
  
    case 1:
      if ((x > 400) && (x<800)) {
        if ((y > 500) && (y < 800)) {
          Serial.println("SCREEN 2-------");
          Screen2();
          screen = 2;
        }
      }
      
      break;

    case 2:
      if ((x > 400) && (x<800)) {
        if ((y > 500) && (y < 800)) {
          Serial.println("SCREEN 1-------");
          Screen1();
          screen = 1;
        }
      }
      
      break;
    
  }

}

unsigned long Screen1() {

  unsigned long start = micros();
  tft.fillScreen(BLACK);
  tft.setRotation(1);
  tft.setTextSize(2);

  tft.setCursor(20, 10);
  tft.setTextColor(RED, YELLOW);
  tft.print("CONTROLO TEMPERATURA!");
  tft.setCursor(10, 50);
  tft.setTextColor(WHITE);
  tft.print("Data: ");
  tft.setTextColor(RED);
  tft.print("2015-07-03 06:38");
  tft.setCursor(10, 75);
  tft.setTextColor(WHITE);
  tft.print("Temperatura: ");
  tft.setTextColor(RED);
  tft.println("37.7 oC");
  tft.setCursor(10, 100);
  tft.setTextColor(WHITE);
  tft.print("Humidade: ");
  tft.setTextColor(RED);
  tft.print("56 %");
  tft.setCursor(10, 125);
  tft.setTextColor(WHITE);
  tft.print("Dias de func.: ");
  tft.setTextColor(RED);
  tft.print("3");

  tft.fillRect(20, 150, 100, 50, RED);
  tft.drawRect(20, 150, 100, 50, RED);
  tft.setTextColor(WHITE);
  tft.setCursor(50, 165);
  tft.print("MENU");
  return micros() - start;
 
  
}


unsigned long Screen2() {
  Serial.println("Beginning Screen 2");
  tft.fillScreen(BLACK);  
  tft.setRotation(1);
  tft.setTextSize(2);
  tft.setCursor(20, 10);
  tft.setTextColor(BLACK, GREEN);
  tft.print("CONTROLO TEMPERATURA 2!");
  tft.setCursor(10, 50);
  tft.setTextColor(WHITE);
  tft.print("Data: ");
  tft.setTextColor(RED);
  tft.print("2015-07-03 06:38");
  tft.setCursor(10, 75);
  tft.setTextColor(WHITE);
  tft.print("Temperatura: ");
  tft.setTextColor(RED);
  tft.println("37.7 oC");
  tft.setCursor(10, 100);
  tft.setTextColor(WHITE);
  tft.print("Humidade: ");
  tft.setTextColor(RED);
  tft.print("56 %");
  tft.setCursor(10, 125);
  tft.setTextColor(WHITE);
  tft.print("Dias de func.: ");
  tft.setTextColor(BLUE);
  tft.print("3");

  tft.fillRect(20, 150, 100, 50, BLUE);
  tft.drawRect(20, 150, 100, 50, BLUE);
  tft.setTextColor(WHITE);
  tft.setCursor(50, 165);
  tft.print("MENU");

  Serial.println("End Screen 2");
}

Look at the logic in the tftpaint program. i.e. how to detect a press.

Note that it restores the XM and YP to OUTPUT after calling ts.getPoint().
Mind you, the TouchScreen library should really put all its pins back to their entry state when it is finished.

Then look at your logic in the Screen1() and Screen2() functions.

Trace the behaviour with pencil and paper.

Which controller does it identify (on the Serial terminal)?
There is a lot to be said for sticking to Adafruit libraries (if they support your controller)

David.