MCUFRIEND & Adafruit_gfx wrong button pressed

I am back to dabbling with my Arduino and TFT display. In my code below (based on the examples) when I push the Stop button the dwn button is shown as pressed and vice versa. I have been puzzling over this for two days, it is obviously something stupid with how I have mapped the buttons and how the button presses are read. If anyone has any suggestions as to what I have got wrong I would be most grateful.

#if 1

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

    
// ALL Touch panels and wiring is DIFFERENT
// copy-paste results from TouchScreen_Calibr_native.ino
const int XP=6,XM=A2,YP=A1,YM=7; //320x480 ID=0x9486
const int TS_LEFT=968,TS_RT=152,TS_TOP=154,TS_BOT=933;
int spd;


TouchScreen ts = TouchScreen(XP, YP, XM, YM, 480);

//Define buttons
Adafruit_GFX_Button start_btn, stop_btn, up_btn, dwn_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, 966, 150, 0, 320); //.kbv makes sense to me 966 150 0 320
        pixel_y = map(p.y, 144, 930, 0, 480); //144 930 0 480
    }
    return pressed;
}

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF


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; // write-only shield
    tft.begin(ID);
    tft.setRotation(1);            //LANDSCAPE
    tft.fillScreen(BLACK);
    start_btn.initButton(&tft,  100, 240, 150, 100, WHITE, GREEN, BLACK, "START", 4);
    stop_btn.initButton(&tft, 380, 240, 150, 100, WHITE, RED, BLACK, "STOP", 4);
    dwn_btn.initButton(&tft, 100, 50, 80, 80, WHITE, CYAN, BLACK, "-", 3);
    up_btn.initButton(&tft, 400, 50, 80, 80, WHITE, CYAN, BLACK, "+", 3);
    start_btn.drawButton(false);
    stop_btn.drawButton(false);
    up_btn.drawButton(false);
    dwn_btn.drawButton(false);
   spd = 0; 
   //stepper.setMaxSpeed(1000);
   //stepper.setAcceleration(500);
}


void loop(void)
{
   
    
  tft.setCursor(200,30);
  tft.setTextColor(WHITE,BLACK);
  tft.setTextSize(4);
  tft.print("SPEED");

  tft.setCursor(240,110);
  tft.setTextColor(WHITE,BLACK);
  tft.setTextSize(6);
  tft.print(spd);
  
    bool down = Touch_getXY();
    
    start_btn.press(down && start_btn.contains(pixel_x, pixel_y));
    stop_btn.press(down && stop_btn.contains(pixel_x, pixel_y));
    up_btn.press(down && up_btn.contains(pixel_x, pixel_y));
    dwn_btn.press(down && dwn_btn.contains(pixel_x, pixel_y));



    if (start_btn.justReleased())
        start_btn.drawButton();
    if (stop_btn.justReleased())
        stop_btn.drawButton();
    if (up_btn.justReleased())
        up_btn.drawButton();
    if (dwn_btn.justReleased())
        dwn_btn.drawButton(); 
           
    if (start_btn.justPressed()) {
        start_btn.drawButton(true);
        
    }
    if (stop_btn.justPressed()) {
        stop_btn.drawButton(true);
       
    }
    if (up_btn.justPressed()) {
        up_btn.drawButton(true);
        
        if (spd < 9){
          spd = spd + 1;
          tft.setCursor(240,110);
          tft.setTextColor(WHITE,BLACK);
          tft.setTextSize(6);
          tft.print(spd);
        }
    }
    if (dwn_btn.justPressed()) {
        dwn_btn.drawButton(true);
        if (spd > 0){
          spd = spd - 1;
          tft.setCursor(240,110);
          tft.setTextColor(WHITE,BLACK);
          tft.setTextSize(6);
          tft.print(spd);
        }
    }
}

#endif

I will guess the coordinate values of these two are swapped. Either during the reading of the press/release, or the drawing of the button.

I agree I have chopped and changed the numbers multiple times and not got anywhere. I have been unable to find which value relates to which location on a landscape 320*480 screen. Hopefully someone can point me in the right direction.

Draw and label the screen you want on paper. Verify your x, y, l, w locations fit. Post your drawing.

Have you tried writing a simplified program that just prints the x,y locations of where you touch and then relating that to where you have your buttons placed?

I have a vague memory of one of these touch libraries using a coordinate system that's the opposite (or at least one axis swapped) of the drawing library. However, I've used so many of either that I can't remember exactly which library it was or how it was different.

This is roughly what I want anything close to this will do I can tweak it if necessary after it is working correctly. Thanks

When you make symmetric button displays, this can hide itself right in front of you.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.