# Problem with TouchScreen library

**URL:** <https://forum.arduino.cc/t/problem-with-touchscreen-library/666567>\
**Category:** Programming\
**Created:** [July 21, 2020, 3:11pm UTC](https://forum.arduino.cc/t/problem-with-touchscreen-library/666567 "2020-07-21T15:11:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![boefffel\_industries](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@boefffel\_industries](https://forum.arduino.cc/u/boefffel_industries)\
**Post date:** [July 21, 2020, 3:11pm UTC](https://forum.arduino.cc/t/problem-with-touchscreen-library/666567/1 "2020-07-21T15:11:57Z")

</div>

Hello there,

I've a problem with the TouchScreen library. I'm using an Arduino UNO with a 2.8" tft lcd Shield from Vellemann:[https://www.vellemanformakers.com/product/2-8-inch-touch-screen-for-arduino-uno-mega-vma412/](https://www.vellemanformakers.com/product/2-8-inch-touch-screen-for-arduino-uno-mega-vma412/)  
Here is my code:

```cpp
#include <Adafruit_GFX.h>    // Core graphics library
#include <MCUFRIEND_kbv.h> // Hardware-specific library
#include <TouchScreen.h>

#if defined( __SAM3X8E__ )
#undef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif

// When using the BREAKOUT BOARD only, use these 8 data lines to the LCD:
// For the Arduino Uno, Duemilanove, Diecimila, etc.:
//  D0 connects to digital pin 8  (Notice these are
//  D1 connects to digital pin 9  NOT in order!)
//  D2 connects to digital pin 2
//  D3 connects to digital pin 3
//  D4 connects to digital pin 4
//  D5 connects to digital pin 5
//  D6 connects to digital pin 6
//  D7 connects to digital pin 7

// For the Arduino Mega, use digital pins 22 through 29
// (on the 2-row header at the end of the board).
//  D0 connects to digital pin 22
//  D1 connects to digital pin 23
//  D2 connects to digital pin 24
//  D3 connects to digital pin 25
//  D4 connects to digital pin 26
//  D5 connects to digital pin 27
//  D6 connects to digital pin 28
//  D7 connects to digital pin 29

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

//Touch For New ILI9341 TP
#define TS_MINX 100
#define TS_MAXX 920

#define TS_MINY 70
#define TS_MAXY 900

// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

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

// Assign human-readable names to some common 16-bit color values:
                            /*  r    g    b */
#define BLACK        0x0000  /*  0,  0,  0 */
#define BLUE        0x001F  /*  0,  0, 255 */
#define RED          0xF800  /* 255,  0,  0 */
#define GREEN        0x07E0  /*  0, 255,  0 */
#define CYAN        0x07FF  /*  0, 255, 255 */
#define MAGENTA      0xF81F  /* 255,  0, 255 */
#define YELLOW      0xFFE0  /* 255, 255,  0 */
#define WHITE        0xFFFF  /* 255, 255, 255 */
#define NAVY        0x000F  /*  0,  0, 128 */
#define DARKGREEN    0x03E0  /*  0, 128,  0 */
#define DARKCYAN    0x03EF  /*  0, 128, 128 */
#define MAROON      0x7800  /* 128,  0,  0 */
#define PURPLE      0x780F  /* 128,  0, 128 */
#define OLIVE        0x7BE0  /* 128, 128,  0 */
#define LIGHTGREY    0xC618  /* 192, 192, 192 */
#define DARKGREY    0x7BEF  /* 128, 128, 128 */
#define ORANGE      0xFD20  /* 255, 165,  0 */
#define GREENYELLOW  0xAFE5  /* 173, 255,  47 */
#define PINK        0xF81F  /* 255,  0, 255 */

MCUFRIEND_kbv tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

#define PENRADIUS 3

int fbutton_id = 0;
int screen_id = 0;

void setup(void) {
  Serial.begin(9600);
  Serial.println(F("Paint!"));
  millis;
  
  tft.reset();

  uint16_t identifier = tft.readID();

  if (identifier == 0x9325) {
    Serial.println(F("Found ILI9325 LCD driver"));
  } else if (identifier == 0x9328) {
    Serial.println(F("Found ILI9328 LCD driver"));
  } else if (identifier == 0x4535) {
    Serial.println(F("Found LGDP4535 LCD driver"));
  } else if (identifier == 0x7575) {
    Serial.println(F("Found HX8347G LCD driver"));
  } else if (identifier == 0x9341) {
    Serial.println(F("Found ILI9341 LCD driver"));
  } else if (identifier == 0x8357) {
    Serial.println(F("Found HX8357D LCD driver"));
  } else if (identifier == 0x0101)
  {
    identifier = 0x9341;
    Serial.println(F("Found 0x9341 LCD driver"));
  } else {
    Serial.print(F("Unknown LCD driver chip: "));
    Serial.println(identifier, HEX);
    Serial.println(F("If using the Elegoo 2.8\" TFT Arduino shield, the line:"));
    Serial.println(F("  #define USE_Elegoo_SHIELD_PINOUT"));
    Serial.println(F("should appear in the library header (Elegoo_TFT.h)."));
    Serial.println(F("If using the breakout board, it should NOT be #defined!"));
    Serial.println(F("Also if using the breakout, double-check that all wiring"));
    Serial.println(F("matches the tutorial."));
    identifier = 0x9341;
  }

  tft.begin(identifier);
  tft.setRotation(1);
  startupScreen();
}

#define MINPRESSURE 10
#define MAXPRESSURE 1000

void loop(){

  digitalWrite(13, HIGH);
  TSPoint p = ts.getPoint();
  digitalWrite(13, LOW);

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

  if (p.z > ts.pressureThreshhold) {

    // scale from 0->1023 to tft.width
    p.x = map(p.x, TS_MAXX, TS_MINX, 0, 320);
    p.y = map(p.y, TS_MAXY, TS_MINY, 0, 240);

    //Display received X an Y value from touch
    Serial.print("("); Serial.print(p.x);
    Serial.print(", "); Serial.print(p.y);
    Serial.println(")");

    if (screen_id == 1) {//Get touch on Main-Screen
      if (p.x > 0 && p.x < 75 && p.y > 180 && p.y < 240) {
        fbutton_id = 1;
        screen_f1();
      }
    }
    
    if (screen_id == 2) { //F1 screen action
      if(p.x > 100 && p.x < 220 && p.y > 150 && p.y < 210){
        fbutton_id = 0;
        screen_main();
      }
        //Action on F1-Screen ok
    }
  }
}

//Build startup screen
void startupScreen(){
  screen_id = 0;
  tft.fillScreen(BLACK);
  tft.setTextColor(CYAN);
  tft.setTextSize(5);
  tft.setCursor(55,70);
  tft.print("BOEFFFEL");
  tft.setTextSize(4);
  tft.setCursor(55,120);
  tft.print("INDUSTRIES");
  delay(777);
  tft.setCursor(55,180);
  tft.setTextSize(2);
  tft.setTextColor(WHITE);
  tft.print("Starting up Systems");
  delay(7777);
  screen_main();
}

//Build MAIN screen
void screen_main() {
  screen_id = 1;
  tft.fillScreen(BLACK);

  tft.drawRect(0,0,50,30,WHITE);
  
  tft.setCursor(10,5);
  tft.setTextSize(1);
  tft.setTextColor(WHITE);
  tft.print("Power:");
  tft.setCursor(17.5,17.5);
  tft.print("77%");

  tft.drawRect(50,0,25,30,WHITE);

  tft.drawRect(75,0,170,30,WHITE);

  tft.setCursor(105,12.5);
  tft.setTextColor(BLUE);
  tft.setTextSize(1);
  tft.print("Boefffel Industries");

  tft.drawRect(245,0,25,30,WHITE);

  tft.drawRect(270,0,50,30,WHITE);

  tft.setCursor(282.5,12.5);
  tft.setTextColor(WHITE);
  tft.print("SOOS!");

  tft.drawRect(0,0,320,240,WHITE);

  tft.drawRect(0,180,75,240,WHITE);
  tft.setCursor(20,200);
  tft.setTextSize(3);
  tft.print("F1");
  
  tft.drawRect(75,180,170,240,WHITE);
  
  tft.setCursor(100,200);
  tft.setTextColor(CYAN);
  tft.print("SHIELDS");
  
  tft.drawRect(245,180,75,240,WHITE);
  tft.setTextColor(WHITE);
  tft.setCursor(265,200);
  tft.print("F2");
  }

//Generate F1 screen
void screen_f1() {
  screen_id = 2;
  tft.fillScreen(BLACK);
  tft.drawRect(0,0,320,240,WHITE);
  tft.fillRoundRect(100,150,120,60,3,WHITE);
  

}

```

I want to make a Okay button in the f1 screen but it doesn't work. I entered the right coordinates but when I click on the butten nothing happens. But when I click between x: 10, y: 180 and x:70, y: 220 then it switches to the main screen. Andin the main screen is also a button and there it works with the right coordinates.

Can somebody help me please?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 12:39am UTC](https://forum.arduino.cc/t/problem-with-touchscreen-library/666567/2 "2021-05-06T00:39:46Z")

</div>

Closed on May 6, 2021, 12:39am UTC


