Go Down

Topic: 2.8 TFT Touch screen not displaying text (Read 3533 times) previous topic - next topic

retexas


Just can't seem to understand why I cant write to the screen the x or y position of a simple touch to the screen. I can write to the screen, with text, and draw on the screen, and witness the  x,y,z values on the serial monitor. A little advice please?

Basically just lifted the touch screen demo I thought?


#include <stdint.h>  //from touchscreen
#include <Adafruit_GFX.h>    // Core graphics library
#include <TouchScreen.h>
#include <AdaLGDP4535TFTLCD.h> // Hardware-specific library
if(identifier == 0x4535)

Code: [Select]

void loop()
{
  //Draw crosshatch
  FrameColor=BLUE;
  tft.fillRect(131, 9, 8,149, FrameColor);
  //fillRect(Left)x0,(Down)y0,(top)w,(bot)h,color);

  tft.fillRect(9, 80, 253,6, FrameColor);
  //fillRect(Left)x0,(Down)y0,(top)w,(bot)h,color);

   
  // a point object holds x y and z coordinates
 TSPoint p = ts.getPoint();
  p.x = map(p.x, TS_MINX, TS_MAXX, 239,0);
  p.y = map(p.y, TS_MINY, TS_MAXY, 0,319);
 
  // we have some minimum pressure we consider 'valid'
  // pressure of 0 means no pressing!
  if (p.z > ts.pressureThreshhold)
  {
     Serial.print("X = "); Serial.print(p.x);
     Serial.print("\tY = "); Serial.print(p.y);
     Serial.print("\tPressure = "); Serial.println(p.z);

     tft.setCursor(20,170);
     tft.setTextColor(BLUE);
     tft.setTextSize(2);
     tft.print(p.x);

  }

  delay(100);

}

retexas

#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin

#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

Seems odd that A3 and A2 are shared between devices?

svan

If I change TSPoint to TS_Point, your loop prints a number with an Adafruit 2.8" TFT Touch Shield resistive screen on an UNO board.

david_prentice

Please post a link to the actual display that you are using.

I Googled LGDP4535 and the only one I could find was a Blue module with Sd card and SPI Touch controller.    It is 3.3V only and not an Arduino Shield.

If you just have the resistive screen with no Touch controller chip,  you need to select Analog pins for YP and XM but YM, XP can be any other pins.    It is unwise (tm) to share with the TFT CS pin.

David.

retexas

The TS_Point, does not work in my case and checking the touchscreen.h file it is ref'd as TSPoint .


Code: [Select]

// Touch screen library with X Y and Z (pressure) readings as well
// as oversampling to avoid 'bouncing'
// (c) ladyada / adafruit
// Code under MIT License

#ifndef _ADAFRUIT_TOUCHSCREEN_H_
#define _ADAFRUIT_TOUCHSCREEN_H_
#include <stdint.h>

class TSPoint {
 public:
  TSPoint(void);
  TSPoint(int16_t x, int16_t y, int16_t z);
 
  bool operator==(TSPoint);
  bool operator!=(TSPoint);

  int16_t x, y, z;
};

class TouchScreen {
 public:
  TouchScreen(uint8_t xp, uint8_t yp, uint8_t xm, uint8_t ym);
  TouchScreen(uint8_t xp, uint8_t yp, uint8_t xm, uint8_t ym, uint16_t rx);

  bool isTouching(void);
  uint16_t pressure(void);
  int readTouchY();
  int readTouchX();
  TSPoint getPoint();
  int16_t pressureThreshhold;

private:
  uint8_t _yp, _ym, _xm, _xp;
  uint16_t _rxplate;
};

#endif




Display works fine at 5vdc and no issues with the actual display. Touch works as expected, other that rotation 180 degree of (0,0) point of display. The touchpoint routine displays as expected on the serial monitor.  I think I'm failing at the code construction to read x,y,z and display it on the TFT.

Board pic's:

david_prentice

Ah-ha.   You have a blue "mcufriend UNO " shield.    I intend to release a new version v2.2 of my MCUFRIEND_kbv.h library later today.    This has an example that will show you what to do with your Touchscreen.

It is just a question of using the map() function to transform your (X, Y) from the Touch to the (X, Y) of the screen.

Where did you buy your display?   I found http://www.banggood.com/2_8-Inch-TFT-LCD-Shield-Touch-Display-Module-For-Arduino-UNO-p-989697.html

You need to remove the protective plastic film.   The screen will look miles better.

David.

retexas

Actually picked up from a place in Dallas for $9, soon as I make head way I plan to go buy a bunch. As far as the film,  just trying to keep it on until I'm read as it scoots around the test bench and protect the surface until I get the Sketchup model printed out.

 Looking forward to your solution!

david_prentice

As far as I can see,   some BLUE mcufriend shields use a LGDP4535 and some use a Raydium R68090 controller.    I bet that others are used too.  The RED shields certainly use many different makes and models of controller.

David.

retexas

I made good progress after lifting some code off a number pad demo and changing drivers to reflect the AdaLGDP4535TFTLCD.h lib. Then getting the pins to align with the Map, it works pretty good. One last bit of coaching requested.

When I write text to the screen, in this case select a number from the displayed pad, it displays in the middle as expected But how do I clear the previous value without refreshing the whole screen causing it to flash , or otherwise how can I just clear the value, and enter the new value. As it is, it just accumulates until the two digits are filled.  Thanks

Code: [Select]

{
  tft.setCursor(90,150); //  setCursor(x0,y)
  tft.setTextColor(BLUE); //  setTextColor(color);
  tft.setTextSize(3); //  setTextSize(#);
   tft.print("");                                                        //Clear previous value? um no!
  tft.print(actual);
  Serial.println(actual);
}



Basic demo for LGDP4535 for anyone else needing a starter kit.

Code: [Select]

// mega with AdaLGDP4535TFTLCD.h // Hardware-specific library
//
// Lifted and modified from from http://forum.arduino.cc/index.php?topic=292777.0
//Modifed input pins Display in portait mode
//Display (Number Pad) 1-16
// X
//╔═══════╗
// 13,9 ,5,1
// 14,10,6,2
// 15,11,7,3    ║  USB(Mega)
// 16,12,8,4
//╚═══════╝ Y
//TFT
// X
//╔══════════════╗
//║ 240,0          240,320 ║
//║                        ║
//║                        ║  USB(Mega)
//║ 0,0              0,320 ║
//╚══════════════╝ Y
//
//#define DEBUG
#include <Adafruit_GFX.h>    // Core graphics library
//#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h>
#include <AdaLGDP4535TFTLCD.h> // Hardware-specific library
// The control pins for the LCD can be assigned to any digital or
// analog pins...but we'll use the analog pins as this allows us to
// double up the pins with the touch screen (see the TFT paint example).
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0

#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin

// 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

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

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

#define TS_MINX 130  //140
#define TS_MINY 130  //140
#define TS_MAXX 960  //900
#define TS_MAXY 970  //950
//Touch
// X
//╔══════════════╗
//║ 960,130         960,970║
//║                        ║
//║                        ║  USB(Mega)
//║ 130,130         130,970║
//╚══════════════╝ Y

#define PENRADIUS 10  //increased from 3
#define MINPRESSURE 10
#define MAXPRESSURE 1000

  int x,y;
  static int anterior=0, actual;
  static unsigned long int tanterior=0, tactual=0;
 
// 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);

int matriz[4][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};

void setup(void) {
  Serial.begin(115200);  //Changed Baud
  Serial.println(F("TFT LCD test"));
  Serial.print("TFT size is "); Serial.print(tft.width()); Serial.print("x"); Serial.println(tft.height());

  tft.reset();

  uint16_t identifier = tft.readID();
  Serial.println(identifier, HEX);
  tft.begin(identifier);
  DibuRects(WHITE);
  DibuNums(RED); 
//End Setup
}

void loop(void)
{
 
  TSPoint p = ts.getPoint();
    // if sharing pins, you'll need to fix the directions of the touchscreen pins
  //pinMode(XP, OUTPUT);
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);
  //pinMode(YM, OUTPUT);

  // we have some minimum pressure we consider 'valid'
  // pressure of 0 means no pressing!

  if (p.z > MINPRESSURE && p.z < MAXPRESSURE)
  {
   
     // scale from 0->1023 to tft.width
    p.x = tft.width()-(map(p.x, TS_MINX, TS_MAXX, 0,tft.width()));  //swapped
    p.y = tft.height()-(map(p.y, TS_MINY, TS_MAXY, 0,tft.height()));  //swapped
   
  x=(4*p.x)/tft.width();
  y=(4*p.y)/tft.height();
 
    actual=matriz[y][x];
    tactual=millis();

    if (tactual-tanterior>1000)
    {
      tanterior=tactual;
      anterior=0;
//  DibuRects(WHITE);  //clear display of text
 // DibuNums(RED);     //clear display of text
    }
    if (actual!=anterior )
    {
      //Serial.println(actual);
      PrintTFT();
      anterior=actual;
      #ifdef DEBUG 
      tft.fillCircle(p.x, p.y, PENRADIUS, BLUE);
      #endif
    }
  }
//End Loop
}
unsigned long DibuRects(uint16_t color)
{

  int width,  height, i, j;

  tft.fillScreen(BLACK);
  width= tft.width()/4;
  height= tft.height()/4;

  for(i=0; i<4; i++)
    for(j=0; j<4; j++)
    {
     tft.drawRect(i*width, j*height, width, height, color);
    }
}

unsigned long DibuNums(uint16_t color)
{
  int width,  height, i, j;
  width= tft.width()/4;
  height= tft.height()/4;
  #if defined __AVR_ATmega2560__
  tft.setRotation(4);
  #endif
  for(i=0; i<4; i++)
    for(j=0; j<4; j++) {
      if((i+j*4+1)<=9)tft.setCursor(i*width+width/3, j*height+height/3);
      else tft.setCursor(i*width+width/10, j*height+height/3);
      tft.setTextColor(color);
      tft.setTextSize(4);
      tft.println(matriz[j][i]);
  }
}

void PrintTFT()

{
  tft.setCursor(90,150); //  setCursor(x0,y)
  tft.setTextColor(BLUE); //  setTextColor(color);
  tft.setTextSize(3); //  setTextSize(#);
   tft.print("");
  tft.print(actual);
  Serial.println(actual);
}


 

david_prentice

If you want to print text on a fresh background,   use the full method  e.g. tft.setTextColor(BLUE, RED);

retexas

That's a good fix, but the right most digit remains on a single digit entry.

david_prentice

Add a trailing space.   This is the common trick to use on a display.    Much simpler than drawing a filled rectangle to blank the area.   
In fact,   you probably do not even notice the new text being drawn.

retexas

Thanks, I get the concept, just not the implementation?

 tft.print(actual);  to something like  tft.print(actual & " "); or  tft.print((actual) & " ");

But this won't compile ?

david_prentice

You just do:
Code: [Select]

{
  tft.setCursor(90,150);       //
  tft.setTextColor(BLUE, RED); // Foreground AND Background colours
  tft.setTextSize(3);          //
  tft.print(actual);           // overwrite the previous contents.
  tft.print(" ");              // add a trailing space
  Serial.println(actual);
  Serial.println(String(actual) + " extra trailing");  //Compiler sees this complex expression as a String
}


There are many ways to skin a cat.

David.

retexas


Go Up