how do i do this? place one value on a touch button?

i have a touch screen button and im sorry but i am still learning the code here in Arduino Land

my coordinates are as follows

tft.fillRoundRect(5, 38, 35, 25, 10, ILI9341_BLACK);

so how do i define this so i can say if this button is pressed do XYZ

Presumably the tft library has a function to read the coordinates of where the screen is touched which returns the X/Y coordinates. If so you can check whether it has been touched within your button and run code if it has.

What you have there is not a button, it is just a picture of a button.

You need to find out how your screen reports a touch.
Write something to get that X,Y
Then something that will work out if the touched X,Y, falls inside your buttons shape.

If I were you, I'd start off with a rectangular button. Easier to work out than round ones. Upgrade them to fancy round ones in the future.

You need to use a Touch Screen library, not just a TFT library.

There are a bunch out there. I have used UTouch and UTFT in the past. What TFT model do you have? What arduino model do you have?

If I were you, I’d start off with a rectangular button. Easier to work out than round ones. Upgrade them to fancy round ones in the future.

Whilst I understand where you are coming from I would just use a rectangular button with rounded corners, which is what I suspect tft.fillRoundRect(5, 38, 35, 25, 10, ILI9341_BLACK); produces, or even a real round button shape and use the X and Y coordinates used to draw the shape as the limits of the test for the button being pressed. Unless the button shapes are really small and/or crammed together on the screen there is no need for the button press reading to be too exact. Near enough is good enough when dealing with fingers on screens.

Of course, this all depends on the OP being able to read the X/Y coordinates of the screen touch in the first place.

i am using a Adafruit 2.8" TFT Touch Shield v2 - Capacitive shield and it uses 3 libraries

Adafruit_FT6206.h
Adafruit_ILI9341.h
Adafruit_GFX.h

and they seem limited in what i can do
i can draw very basic shapes
i have to start with the top left XY and work from there
im still trying to understand how it all works but this is where im getting my info from

im using a Mega 2560 board

The libraries come with examples. Have you looked at them ?
This is an extract from the CapTouchPaint example

  // Retrieve a point  
  TS_Point p = ctp.getPoint();
  
 /*
  // Print out raw data from screen touch controller
  Serial.print("X = "); Serial.print(p.x);
  Serial.print("\tY = "); Serial.print(p.y);
  Serial.print(" -> ");
 */

It appears that the getPoint() method returns the X and Y coordinates of the point touched.

I suggest that you look at the examples to see how it is done in detail.

yes i saw that and i did do something in the way of

i started to press one of the buttons to get rough x and y Max and Min numbers

To Get

Btn X min 178
Btn X max 191
Btn Y min 299
Btn Y max 318

so i would then put an if statement in to cover the above

if ((p.x > 177 && p.x < 192)&& (p.y > 298 && p.y < 319))
{
 Serial.println("one");
}

i thought i was doing ok with this then realised i have 5 screens all the buttons on each screen are in the same place
so i can’t really put this in the main loop as for example i press button one to open second screen but on the second screen the first button was a setting if i press the first button on the second screen it would just reload the second screen again

so this is where i may as well go a look again to see if there is a better way

so i can’t really put this in the main loop as for example i press button one to open second screen but on the second screen the first button was a setting if i press the first button on the second screen it would just reload the second screen again

It sounds like you need to act when the “button” becomes pressed rather than when it is pressed.

Have a look at the StateChangeDetection example in the IDE. Although the example uses real buttons rather than virtual ones but the principle is the same.

would this work??
if i can get the format correct

if ((tft.fillScreen == (BLACK) &&((p.x > 163 && p.x < 200)&& (p.y > 295 && p.y < 314)))
{
  tft.print("bingo")
}

its showing errors at the mo
but each screen is a different colour so if i use the same code for all screens but change the fullscreen colour

I know almost nothing about the library that you are using but almost certainly that is not what you need to do. Did you look at the example that I suggested ? You can use the same code for each screen using the StateChangeDetection method by putting it in a function and calling when you need to. Get the function to return true if the button has been pressed since the last time the function was called and false if not.

i will try that as this is certainly not working
i am now wishing i bought another touchscreen

mikewitney:
i will try that as this is certainly not working
i am now wishing i bought another touchscreen

The problem is not with your touchscreen. You would have the same issue no matter which touchscreen you bought. You need to either continue down the path you are going, or use other libraries. But, that is the same no matter which touchscreen you would have bought.

Just to explain, the Adafruit uses the libraries you mentioned, but it does not have to. There are a bunch of other graphics/touch libraries you can use instead, including UTFT.

Do not post the same question twice.

mikewitney:
yes i saw that and i did do something in the way of

i started to press one of the buttons to get rough x and y Max and Min numbers

To Get

Btn X min 178
Btn X max 191
Btn Y min 299
Btn Y max 318

so i would then put an if statement in to cover the above

if ((p.x > 177 && p.x < 192)&& (p.y > 298 && p.y < 319))

{
Serial.println(“one”);
}




i thought i was doing ok with this then realised i have 5 screens all the buttons on each screen are in the same place
so i can't really put this in the main loop as for example i press button one to open second screen but on the second screen the first button was a setting if i press the first button on the second screen it would just reload the second screen again

Your concept is correct. You just need to add knowledge of state (i.e.: a simple state machine).

So, I assume you are reading the screen touch position correctly.

Here’s what the loop would like like in pseudocode:

int currentScreen = 0;
boolean touchedLastTime = false;

void loop()
{
   // look for a transition from not touched to touched
   if (ctp.touched() && touchedLastTime == false)
   {
       pt = getPoint();
       if (pt inside button rectangle)
       {
            // each time we press inside the rectangle, increase the screen number
            current_screen++;
       }
   }

   // store the current touchscreen touched/not touched state so we can look 
   // for touching the screen, not holding our finger down
   touchedLastTime = ctp.touched();

   switch (currentScreen)
   {
       case 0:
             // display first screen menus/buttons here
             ...
             break;
       case 1:
             // display second screen menus/buttons here
             ...
            break;
       ...
   }
}