1
votes

I am trying to build a menue with a TFT touchpad. Now my problem is that I can't draw anything within the loop function.

If I write any draw-function in the loop the screen turns white. I figured that this is because the screen needs some time to build up. So I added a delay(1000). But then the screen flashes every second wich is obviously also not what I want.

The next weired thing is that the program stops working when I draw once the display is touched. In the code below I have three draw-functions. Two of them are working one is not. (see comments)

#include <SeeedTouchScreen.h>

#include <TFTConsole.h>
#include <Adafruit_TFTLCD.h>
#include <Adafruit_GFX.h>


#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4

#define TS_MINX 169
#define TS_MINY 208
#define TS_MAXX 1781
#define TS_MAXY 1820

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

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

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
TouchScreen ts = TouchScreen(XP, YP, XM, YM);
boolean touched = false;


void setup() {
  Serial.begin(9600);
  Serial.print("Starting...");

  tft.reset();

  tft.begin(0x9325);

  tft.setRotation(1);

  tft.fillScreen(BLACK);

  //Print "PPM CO" Text
  tft.setCursor(50, 30);
  tft.setTextColor(GREEN);
  tft.setTextSize(3);
  tft.print("Hello World"); //<- this is displayed fine
  delay(1000);
}
boolean first = true;
void loop() {
  if (first) {
    first = false;
    tft.drawCircle(119, 160, 20, random(0xFFFF)); //<- This is also displayed
  }


  Point p = ts.getPoint();


  p.x = map(p.x, TS_MINX, TS_MAXX, 0, 240);
  p.y = map(p.y, TS_MINY, TS_MAXY, 0, 320);
  if (p.x <= 240 && p.y <= 320 && p.x >= 0 && p.y >= 0) {
    Serial.println("don't touch me!");
    touched = true;
  }
  else {
    touched = false;
  }
  if (touched) {

    tft.drawCircle(119, 180, 20, RED); //<- This is not displayed and makes the screen flash
    delay(1000);
  }


  //tft.fillScreen(BLUE);
  //delay(500);

}

Only if I have the last drawCircle inside the code the variable touched does not switch back to false if I stop touching the display.

Does anybody has a clue what I am doing wrong?

Update: I stoped the display from flashing by letting the touched event only happen once when the display is touched. But I am still having the problem that nothing is drawn...

void loop() {
  //Touchposition bestimmen
  Point p = ts.getPoint();
  p.x = map(p.x, TS_MINX, TS_MAXX, 0, 240);
  p.y = map(p.y, TS_MINY, TS_MAXY, 0, 320);
  if (p.x <= 240 && p.y <= 320 && p.x >= 0 && p.y >= 0) {
    if (released) {
      touched = true;
      released = false;
    }
  }
  else {
    //Serial.println("Touch me where I like it!");
    touched = false;
    released = true;
  }
  if (touched && !released) {
    Serial.println("don't touch me!");
    drawButton(100, 100, "Manuell");
    touched = false;
    delay(500);
  }

}
1

1 Answers

0
votes

I noticed that the TFT display and Touchscreen share some Analog pins. I think this is causing the problems. So I sectioned my code in a part where the touchscreen is handled. Then I can reassign the pins to the TFT monitor and draw to it. I don't know whether this theory makes sence, but it is working:

 void loop() {
  //Touchposition bestimmen
  if (ts.isTouching()) {
    Point p = ts.getPoint();
    p.x = map(p.x, TS_MINX, TS_MAXX, 0, 240);
    p.y = map(p.x, TS_MINY, TS_MAXY, 0, 320);
    if (released) {
      released = false;
    }
  }
  else {
    //Serial.println("Touch me where I like it!");
    released = true;
  }

  if (ts.isTouching() && !released) {
    //re assing pins to tft because they are also used by the touchscreen
    Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
    Serial.println("don't touch me!");
    drawButton(100, 100, "Manuell");
    touched = false;
  }
}

The drawButton funton contains several draw functions