[Solved] 2.4" Touchscreen TFT--Moving On-Screen Button Issue

Hi, so I am just working on a little project of mine and after many many many hours of problemsolving I've got most everything down on how to use the 2.4" Touchscreen TFThttps://www.amazon.com/HiLetgo-Display-ILI9341-240X320-Arduino/dp/B0722DPHN6/ref=sr_1_2_sspa?keywords=hiletgo+arduino+tft+2.4%22+touchscreen+lcd+shield&qid=1552120341&s=gateway&sr=8-2-spons&psc=1

However, I've been stuck for quite a while now on how to move an onscreen button. I can use it alright, but only in a certain spot. I think the problem may lie in having to create new pixel x and y places but am pretty confused about it. Basically, I am unable to figure out how to move a button on the screen to a new spot, as whenever i move it, it shows up, but nothing happens when I touch it.

Here's my code (to simplify matters I have been trying to figure it out on a button example code and then carry over whatever I figure out to my actual project:)

#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
#define MINPRESSURE 200
#define MAXPRESSURE 1000

const int XP = 8, XM = A2, YP = A3, YM = 9; //320x240 ID=0x9341
const int TS_LEFT = 70, TS_RT = 885, TS_TOP = 899, TS_BOT = 117;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Adafruit_GFX_Button on_btn, off_btn;

int pixel_x, pixel_y;     //Touch_getXY() updates global vars
bool Touch_getXY(void)
{
    TSPoint p = ts.getPoint();
    pinMode(YP, OUTPUT);      //restore shared pins
    pinMode(XM, OUTPUT);
    digitalWrite(YP, HIGH);   //because TFT control pins
    digitalWrite(XM, HIGH);
    bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
    if (pressed) {
        pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width()); //.kbv makes sense to me
        pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
    }
    return pressed;
}

#define BLACK   0x0000
#define CYAN    0x07FF
#define WHITE   0xFFFF
#define RED     0xB800
#define GREEN   0x02E1

void setup(void) {
    Serial.begin(9600);
    uint16_t ID = tft.readID();
    Serial.print("TFT ID = 0x");
    Serial.println(ID, HEX);
    Serial.println("Calibrate for your Touch Panel");
    if (ID == 0xD3D3) ID = 0x9486;
    tft.begin(ID);
    tft.setRotation(1);          
    tft.fillScreen(BLACK);
    on_btn.initButton(&tft,  60, 200, 100, 40, WHITE, CYAN, BLACK, "ON", 2);
    off_btn.initButton(&tft, 180, 200, 100, 40, WHITE, CYAN, BLACK, "OFF", 2);
    on_btn.drawButton(false);
    off_btn.drawButton(false);
    tft.fillRect(40, 80, 160, 80, RED);
}

void loop(void) {
    bool down = Touch_getXY();
    on_btn.press(down && on_btn.contains(pixel_x, pixel_y));
    off_btn.press(down && off_btn.contains(pixel_x, pixel_y));
    if (on_btn.justReleased())
        on_btn.drawButton();
    if (off_btn.justReleased())
        off_btn.drawButton();
    if (on_btn.justPressed()) {
        on_btn.drawButton(true);
        tft.fillRect(40, 80, 160, 80, GREEN);
    }
    if (off_btn.justPressed()) {
        off_btn.drawButton(true);
        tft.fillRect(40, 80, 160, 80, RED);
    }
}

I a unsure how to move the btn (button) to a new location. I assume by changing the "60" and "200" valuables as that does indeed move the button to a new location.

on_btn.initButton(&tft,  60, 200, 100, 40, WHITE, CYAN, BLACK, "ON", 2);

However, if I do change it to:

on_btn.initButton(&tft,  50, 50, 100, 40, WHITE, CYAN, BLACK, "ON", 2);

The button moves to the spot desginated (50, 50) like how I want, but when I press it, it no longer recognizes when I press it.

Using an Arduino Mega 2560 and any help would be greatly appreciated!

Thanks!

One thing that may be worth ruling out is that the touch coordinates returned from Touch_getXY() are as expected. As you are likely aware, resistive touch overlays generally require calibration and have the potential for the X & Y coordinates to be swapped / inverted versus your TFT display. I assume the code's XP/YP/XM/YM and TS_* values were captured from your calibration, but it is worth confirming.

To rule any touch mapping issues out, try adding a visual indicator of where the touch presses occurred. After your down = Touch_getXY(); call, insert:

tft.fillRect(pixel_x-1, pixel_y-1, 2, 2, 0xFFE0);

With the above added, look to see if the yellow markers line up to your touch presses.

Hey, thanks yes it was from calibration but the x y values were indeed swapped. I tried what you said and figured it out in no time! Thanks!