Hi,
I have been working with a Teensy 3.2 connected to a ILI9341 TFT/Touch display from Paul's store, and I am having a problem debouncing the touch screen. I have a very simple sketch that takes a stylus touch point, maps it to an appropriate display coordinate, and draws a small filled circle at the mapped point. For each point I print out the program time in milliSec, the raw (touch screen) coordinates and the mapped display coordinates. I also have some 'debounce' code to prevent multiple touch detections from a single stylus tap. The debounce code works perfectly, UNLESS I tap on the lower left hand corner of the screen, where I get 10-15 touch detections even with the debounce code present. Here's the sketch:
Code:
/*
Name: KurtTouchV2.ino
Created: 2/15/2021 4:16:52 PM
Author: FRANKNEWXPS15\Frank
*/
//****************************************************************************
// Includes
//****************************************************************************
#include <XPT2046_Touchscreen.h>
#include <ILI9341_t3n.h>
#include "ili9341_t3n_font_Arial.h"
//****************************************************************************
// This is calibration data for the raw touch data to the screen coordinates
//****************************************************************************
// Warning, These are approximate values
#define TS_MINX 600
#define TS_MINY 600
#define TS_MAXX 3758
#define TS_MAXY 3612
#define SCREEN_ORIENTATION_1
//02/16/21 change from using #defines to variables, so can
// update via calibration.
uint16_t ts_minx = TS_MINX;
uint16_t ts_maxx = TS_MAXX;
uint16_t ts_miny = TS_MINY;
uint16_t ts_maxy = TS_MAXY;
//****************************************************************************
// Settings and objects
//****************************************************************************
//02/15/21 pin assignments from https://www.pjrc.com/store/display_ili9341_touch.html
#define TFT_DC 9
#define TFT_CS 10
#define TFT_RST 7
#define TFT_SCK 13
#define TFT_MISO 12
#define TFT_MOSI 11
#define TOUCH_CS 8
XPT2046_Touchscreen ts(TOUCH_CS);
ILI9341_t3n tft = ILI9341_t3n(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCK, TFT_MISO);
// Size of the color selection boxes and the paintbrush size
#define MAX_DISPLAY_X 240
#define MAX_DISPLAY_Y 320
#define NUMBOXES 7
//#define BOXSIZE MAX_DISPLAY_X / NUMBOXES
#define BOXSIZE 34
#define PENRADIUS 3
int oldcolor, currentcolor;
int BoxColorArray[] = { ILI9341_RED, ILI9341_YELLOW, ILI9341_GREEN, ILI9341_CYAN,
ILI9341_BLUE, ILI9341_MAGENTA, ILI9341_WHITE, ILI9341_ORANGE, ILI9341_GREENYELLOW, ILI9341_PINK };
//****************************************************************************
// Setup
//****************************************************************************
void setup(void)
{
Serial.begin(9600);
while (!Serial && (millis() <= 3000))
{
delay(100);
}
tft.begin();
tft.setRotation(1);
// by default use SPI
if (!ts.begin()) {
Serial.println("Couldn't start touchscreen controller");
while (1);
}
Serial.println("Touchscreen started");
tft.fillScreen(ILI9341_BLACK);
}
void loop()
{
// See if there's any touch data for us
// You can also wait for a touch
if (!ts.touched())
{
return;
}
// Retrieve a point
TS_Point p = ts.getPoint();
Serial.printf("%lu: Raw P(%d,%d) maps to ", millis(), p.x, p.y);
// Scale from ~0->4000 to tft.width using the calibration #'s
#ifdef SCREEN_ORIENTATION_1
p.x = map(p.x, TS_MAXX, TS_MINX, 0, tft.width());
p.y = map(p.y, TS_MAXY, TS_MINY, 0, tft.height());
#else
uint16_t px = map(p.y, TS_MINY, TS_MAXY, 0, tft.width());
p.y = map(p.x, TS_MAXX, TS_MINX, 0, tft.height());
p.x = px;
#endif
Serial.printf(" (%d,%d): ", p.x, p.y);
Serial.printf("drawing circle at (%d,%d)\n", p.x, p.y);
tft.fillCircle(p.x, p.y, PENRADIUS, ILI9341_RED);
//touch screen debounce
while (ts.touched())
{
p = ts.getPoint();
delay(10);
}
}
And a photo of the setup, showing some of the dots from the program

And here's some of the printed output, showing individual stylus presses and then a run of touch detections spaced about 10 mSec apart, all with the same approximate coordinates; these were generated from just one stylus touch in the lower left hand corner (corner closest to the Teensy and farthest away from the prototype board)
Code:
Opening port
Port open
19651: Raw P(3766,3562) maps to (0,3): drawing circle at (0,3)
29334: Raw P(708,3459) maps to (309,12): drawing circle at (309,12)
38322: Raw P(3813,362) maps to (-5,258): drawing circle at (-5,258)
38333: Raw P(3825,307) maps to (-6,263): drawing circle at (-6,263)
38343: Raw P(3844,307) maps to (-8,263): drawing circle at (-8,263)
38354: Raw P(3840,305) maps to (-8,263): drawing circle at (-8,263)
38364: Raw P(3840,305) maps to (-8,263): drawing circle at (-8,263)
38375: Raw P(3862,317) maps to (-10,262): drawing circle at (-10,262)
38385: Raw P(3861,301) maps to (-10,263): drawing circle at (-10,263)
38395: Raw P(3845,292) maps to (-8,264): drawing circle at (-8,264)
Any ideas why this is happening?
TIA,
Frank