ILI9341 + Touch - debouncing problem

Status
Not open for further replies.

Jens

Member
Hi,

I wrote a little programm with some different screens where I use one button to go back to the last screen.
The "back" button is always at the same position. The problem is if I press the "back" button in one screen, it
will go back until the first screen.

I tried many (useless) ways to debounce the touchscreen so that every new screen is displayed only after releasing the touch and wait
for another touch on the back button.
Yesterday I saw the great button library from defragster, but this exceeds my programming skills a lot.

Is there a simple way to do that?

If I don´t use the IRQ it works better but not in that way I would like to have it working.

This is the sample code from the XPT2046 (with irq):

I use the Teensy 3.5.


Code:
#include <XPT2046_Touchscreen.h>
#include <SPI.h>

#define CS_PIN  8
// MOSI=11, MISO=12, SCK=13

// The TIRQ interrupt signal must be used for this example.
#define TIRQ_PIN  7
XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN);  // Param 2 - Touch IRQ Pin - interrupt enabled polling

void setup() {
  Serial.begin(38400);
  ts.begin();
  ts.setRotation(1);
  while (!Serial && (millis() <= 1000));
}

void loop() {

  // tirqTouched() is much faster than touched().  For projects where other SPI chips
  // or other time sensitive tasks are added to loop(), using tirqTouched() can greatly
  // reduce the delay added to loop() when the screen has not been touched.
  if (ts.tirqTouched()) {
    if (ts.touched()) {
      TS_Point p = ts.getPoint();
      Serial.print("Pressure = ");
      Serial.print(p.z);
      Serial.print(", x = ");
      Serial.print(p.x);
      Serial.print(", y = ");
      Serial.print(p.y);
      delay(30);
      Serial.println();
    }
  }
}

I believe I am an good electronic engineer but my programming skill in C (Arduino) looks weak and must be improved.

Jens
 
Last edited:
I too am having problems with the touch screen commands. I started with the 'touchPaint' example. It appears that they do not function correctly (as advertised) and others also have similar (unresolved?) issues if you go searching for them. I am using the 3.5" display with resistive touch panel from Buydisplay.com.

One thing I notice is that p.x and p.y don't change when one removes the pressure from the screen so this could be a source of your return to the beginning menu level. I also note that p.z does change and with a bit of qualifying ' if (p.z >= 500<some arbitrary level>) ' you can at least determine if the screen has a valid 'touch' and not just a fly walking on it. Maybe p.z could be a means of qualifying the p.x & p.y values and steer the values away from the 'return' condition.
 
I'm back to looking at the Touch code - so I might be able to help.

@Jens : In the header is this :: #define TS_LIMIT 10 // millisecond limit to check touch value

That prevents this code from reporting a touch event faster than 10 millisconds. It is edited below to show the essential test if you wrote a function to call to test for touch - and should work.
Code:
boolean TS_GetMap( int16_t &xx, int16_t &yy, boolean twoHits )
{
  static elapsedMillis tsLimit;
  [B]if ( tsLimit < TS_LIMIT ) return false;[/B]
  if (!ts.touched()) return false;
  tsLimit = 0;

The elapsedMillis variable is just a time reference to millis() - and hides the math. Once set to ZERO it will increment each ms. It is much better than embedding delay()'s in the code.
Using the _irq is the best way to do it - it does speed up the code in some fashion so that may be why in this case it 'seems' to help.

@grease_l : it is possible you have a different display touch controller? Or some wiring problem? I'm looking at a larger display now and am seeing things I never saw before - even though it is using the same touch controller - because the display doesn't have SPI treated the same.
 
Hi defragster and grease_lighting,

thank you for take your time to help me. I think I understand how it should work, but my implementation seems not to work as I expected.
The position is fired out of the serial port as long as I put my pen on the touchscreen depending on the milliseconds I add for the delay (TS_LIMIT).
What I want it to do is after I press the Touchscreen with the pen it shoud follow whatever I clicked in that moment after I release the pen from the touchpanel
and put out the new touch position after I click it again.

Code:
#include <XPT2046_Touchscreen.h>
#include <SPI.h>

#define CS_PIN  8
#define TS_LIMIT 400 // millisecond limit to check touch value

#define TS_MINX 190  
#define TS_MINY 185  
#define TS_MAXX 3921 
#define TS_MAXY 3867 


// The TIRQ interrupt signal must be used for this example.
#define TIRQ_PIN  7
XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN);  // Param 2 - Touch IRQ Pin - interrupt enabled polling

void setup() {
  Serial.begin(38400);
  ts.begin();
  ts.setRotation(1);
  while (!Serial && (millis() <= 1000));
}

void loop() 
{

  // tirqTouched() is much faster than touched().  For projects where other SPI chips
  // or other time sensitive tasks are added to loop(), using tirqTouched() can greatly
  // reduce the delay added to loop() when the screen has not been touched.
  boolean istouched = false;
  int16_t x, y, z;

  if (ts.tirqTouched()) 
    {
     istouched = TS_GetMap(x,y,z);        
    
      if (istouched) 
      {        
        Serial.print("Pressure = ");
        Serial.print(z);      
        Serial.print(", x = ");
        Serial.print(x);
        Serial.print(", y = ");
        Serial.print(y);
        delay(30);
        Serial.println();
      }
    }
}

boolean TS_GetMap( int16_t &xx, int16_t &yy, int16_t &zz )
{
  static elapsedMillis tsLimit;
  if ( tsLimit < TS_LIMIT ) return false;
  if (!ts.touched()) return false;
  tsLimit = 0;
  
  TS_Point p = ts.getPoint();                
  
  xx = map(p.x, TS_MINX, TS_MAXX, 0, 320); 
  yy = map(p.y, TS_MINY, TS_MAXY, 0, 240);    
  zz = p.z; 
  return true;
}

One more thing (maybe it has to do with the Touchpanel library): If I just put out the data for the touchpanel on the serial consonle it will always put out the last position if
I don´t touch the touchscreen instead of showing zero for x,y and z.
 
@Defragster, Please standby as all my info for this project is at the workplace, I'll respond on Monday.

@Jens, It appears you are reporting what I am with the x & y data values staying the same when the screen is not touched. Does your z value stay the same or return to zero (0) when not touched? Ideally x & y should go to something like -1, when not touched.
 
@grease_lighting

Yes, the z value will go back to 0 if not touched while the x and y values stay at the last recorded value.
This is the code I use for test
Code:
#include <XPT2046_Touchscreen.h>
#include <SPI.h>

#define CS_PIN  8
// MOSI=11, MISO=12, SCK=13

// The TIRQ interrupt signal must be used for this example.
#define TIRQ_PIN  7
XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN);  // Param 2 - Touch IRQ Pin - interrupt enabled polling

void setup() {
  Serial.begin(38400);
  ts.begin();
  ts.setRotation(3);
  while (!Serial && (millis() <= 1000));
}

void loop() 
{
      TS_Point p = ts.getPoint();
      Serial.print("Pressure = ");
      Serial.print(p.z);
      Serial.print(", x = ");
      Serial.print(p.x);
      Serial.print(", y = ");
      Serial.print(p.y);
      delay(30);
      Serial.println();
}
 
The display that I use is from buydisplay.com. it is a TFTM032-3 display with a resistive touch screen using the xpt-2046 controller.

I am using these divers;
#include <ILI9341_t3.h> and #include <XPT2046_Touchscreen.h>

The pins being used are
Code:
  //  ----  Define Teensy IO pins
// The display also uses hardware SPI, plus #9 & #10
#define TFT_CS 21       //  tft Cs (D21, -1 to inhibit)
#define TFT_DC  6       //  tft control       (also try #9)                  temporary mod to use valid CS pin type as in examples for debug
#define TFT_BL 22       //  tft Back Light control  low==off, high==on,   PWM dimming
#define TFT_RST 255     //  255 = unused connected to 3.3v      temporary mod to apply RESET to lcd display for debug.
#define TFT_MOSI  11
#define TFT_SCLK  14
#define TFT_MISO  12
#define ethRst 255      //  WIZ820/850 reset pin  (#9 not used)
#define ethCs 255       //  WIX820/850 CS pin (D10, 255 to inhibit)
#define sdWp 255        //  full size SD Card Write Protect signal on main PCB
#define sdPres 255      //  full size SD Card presense detect switch  (#13 not used)  
#define sdCs 8          //  (D08) full size SD Card CS on main PCB  
#define tftSdCs 23      //  (D23) micro SD Card on touch panel  (static high)
#define rtpCs 15
#define CS_PIN 15       //  touch CS -- must use name shown in example
#define rtpPen 5
#define TIRQ_PIN 5      //  touch IRQ -- must use name shown in example

My wiring is on a PCB PCBwiring.png


I have gotten around the ts.touch problem by doing ths

Code:
  TS_Point p = ts.getPoint();   // Retrieve a point  
  px=p.x;
  py=p.y;     //  make vars global
  pz=p.z;
  tsRotate();       //  rotate px/py according to rotation 
  // Scale from ~0->4000 to tft.width using the calibration #'s
  px = map(px, TS_MINX, TS_MAXX, 0, tft.width());
  py = map(py, TS_MINY, TS_MAXY, 0, tft.height());
  //  Serial.print("X = "); Serial.print(px);
  //  Serial.print("\tY = "); Serial.print(py);               //  \t (tab)  puts output into 'nice' columns
  delay(200);                                                 //  slow down screen response
  if (pz>=500)  {scrBlnk();}                                  //  un-blank only when actually touched
  p1 = px / BOXSIZE;  //  Serial.print("Box-X = "); Serial.print(p1);     //  translate position into
  p2 = py / BOXSIZE;  //  Serial.print("\tBox-Y = "); Serial.print(p2);   //  box co-ordinates
      Serial.print("\tPressure = "); Serial.println(pz);
  p7=1;
  //  decode touches  qualify with pz>threshold for a valid touch
  if ((page==1) && (p1==7) && (p2==0) && (pz>=500)) {dispTwo();}
  if ((page==1) && (p1>=3) && (p1<=5) && (p2==0) && (pz>=500)) {stCycle();}
  if ((page==2) && (p1==7) && (p2==0) && (pz>=500)) {dispOne();}

As @Defragster has previously stated in #4, " I'm looking at a larger display now and am seeing things I never saw before - even though it is using the same touch controller - because the display doesn't have SPI treated the same."
 
Status
Not open for further replies.
Back
Top