2.8" TFT Compile Error: 'class TouchScreen' has no member named 'touched'

I’m trying to check if a button is pressed while in a countdown timer function. In an Adafruit example,

if (ts.touched()) {

was used and this seemed like the perfect way to determine if a button is being pressed (and then jumping to a function to process the button press).

However, there is a compilation error: ‘class TouchScreen’ has no member named ‘touched’

Here are the libraries included in the sketch:

#include <Adafruit_TFTLCD.h>
#include <registers.h>
#include <pin_magic.h>

#include <TouchScreen.h>

#include <gfxfont.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>

Here’s the function in the sketch:

void CountDownTimer(int Minutes, int Seconds)
{

if (Minutes > 0)
{
Minutes–;
}

for (M = Minutes * 60; Minutes > 0; Minutes–)
{

for (S = 59; S > 0; S–)
{
Seconds = S;
PrintCountDown(Minutes, Seconds);
digitalClockDisplay();
Alarm.delay(950);

//***************************************
if (ts.touched()) {
Serial.println(“Touched”);
}
}
}

for (S = Seconds; Seconds > 0; Seconds–);
{
PrintCountDown(Minutes, Seconds);
Alarm.delay(950);
}
digitalWrite(sump, HIGH); // Turn the pumps back ON
digitalWrite(wave, HIGH); // Turn the wavemaker ON
digitalWrite(ato, HIGH); // Turn the ato ON

}

Thank you in advance for your review and suggestions!

You have to post the whole code (as an attachment if necessary). From your snippet, it is not clear of which class ts is an instance. Add a link to the example / tutorial you are following.

Thanks 6v6gt - I’ve attached the complete script below…

ElegooTFT_Mega_Controller_v02.ino (16 KB)

Here is the Adafruit article that calls out ts.touched() (from their Resistive Touchscreen Paint Demo):

Getting data from the touchscreen is fairly straight forward. Start by creating the touchscreen object with
Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);
We're using hardware SPI so the clock, mosi and miso pins are not defined here. For the shield, CS is #8 always.
Then you can start the touchscreen with
ts.begin()
Check to make sure this returns a True value, which means the driver was found. If it wasn't, make sure you have the hardware SPI jumpers set up right: for Leonardo/Mega the ICSP jumpers get closed.

Now you can call
if (! ts.bufferEmpty())
to check if there's any data in the buffer. The touchscreen driver will store touchpoints at all times. When you're ready to get the data, just check if there's any data in the buffer. If there is, you can call
TS_Point p = ts.getPoint();
To get the oldest point from the buffer. TS_Point has .x .y and .z data points. The x and y points range from 0 to 4095. The STMPE610 does not store any calibration data in it and it doesn't know about rotation. So if you want to rotate the screen you'll need to manually rotate the x/y points! The z point is 'pressure' and ranges from 0 to 255, we don't use it here but you can experiment with it on your own, the harder you press, the lower the number.

Since data from the STMPE610 comes in 0-4095 but our screen is 320 pixels by 240 pixels, we can use map to convert 0-4095 to 0-320 or 0-240. Something like
p.x = map(p.x, 0, 4095, 0, tft.width());
p.y = map(p.y, 0, 4095, 0, tft.height());
However, the touchscreen is a bit bigger than the screen, so we actually need to ignore presses beyond the touchscreen itself. We found that these numbers reflected the true range that overlaps the screen
#define TS_MINX 150
#define TS_MINY 130
#define TS_MAXX 3800
#define TS_MAXY 4000
So we use
p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
instead.

One last point (pun intended!) since the touchscreen driver stores points in a buffer, you may want to ask the driver "is the touchscreen being pressed RIGHT NOW?" You can do that with
if (ts.touched())

OK. But where did you get this library which you include in your sketch ? :

#include <TouchScreen.h>

With 18 posts, you should know how to use code tags for posting source code like we see in your original post. You can find useful information in the first set of posts at the top of this Forum.

You're citing an example using the Adafruit_STMPE610 class. However, your code is using the TouchScreen class which apparently doesn't have a method called 'touched'.

6v6gt - I’m using the Adafruit_Touchscreen-master from Github. I think gfvalvo may have it - I took the method out of context, so I will do more reading to determine the appropriate classes and methods.

Apologies for not using the correct code tags - been quite a while since I last posted…