Touch registering twice

I am using the IDE 2.2.1 to program an M5Paper device, which basically an ESP32 with an E-Ink display that is a GT911 capacitive touch screen.

I am playing with the touch screen and although I got it working and stopped the finger cordinates being repeated many times per touch, they still repeat twice per touch. I think what is happening is that the coordinates are given once for touch and once for "fingerUp". How can I limit this to a single set of coordinates?

Here is the serial output:

M5EPD initializing...OK
Box 1 - X Coordinate: 20, Y Coordinate: 100
Box 2 - X Coordinate: 20, Y Coordinate: 200
Box 3 - X Coordinate: 20, Y Coordinate: 300
Box 4 - X Coordinate: 20, Y Coordinate: 400
0....0
0....0
0....0
326....582
326....582
326....582
16....427
BOX 4
Cabbage 16....427
BOX 4
Cabbage

And here is the code:

#include <M5EPD.h>

M5EPD_Canvas canvas(&M5.EPD);

struct coords {
  String veg;
  int x;
  int y;
};

bool fill = false;

// Define the number of boxes you want plus one
const int numBoxes = 5;

// Create an array of Coordinates structs
coords box[numBoxes];


void makeBoxes() {
  for (int i = 1; i < numBoxes; i++) {
    box[i].x = 20;
    box[i].y = i * 100;
  }
  box[1].veg = "Potato   ";

  box[2].veg = "Broccolli";
  box[3].veg = "Turnip   ";
  box[4].veg = "Cabbage  ";

  // Print the coordinates for each box
  for (int i = 1; i < numBoxes; i++) {
    Serial.print("Box ");
    Serial.print(i);
    Serial.print(" - X Coordinate: ");
    Serial.print(box[i].x);
    Serial.print(", Y Coordinate: ");
    Serial.println(box[i].y);
    //Draw each box
    canvas.drawRect(box[i].x, box[i].y, 50, 50, 15);
    canvas.drawString(box[i].veg, 100, box[i].y);
  }

  /*  // Accessing specific box coordinates
  int boxIndex = 2;
  Serial.print("Box ");
  Serial.print(boxIndex);
  Serial.print(" - X Coordinate: ");
  Serial.print(box[boxIndex].x);
  Serial.print(", Y Coordinate: ");
  Serial.println(box[boxIndex].y);*/
}


void setup() {
  M5.begin();
  M5.EPD.SetRotation(90);
  M5.TP.SetRotation(90);
  M5.EPD.Clear(true);
  canvas.createCanvas(540, 960);
  canvas.setTextSize(4);
  canvas.drawString("Select a vegatable", 20, 20);
  makeBoxes();
  canvas.pushCanvas(0, 0, UPDATE_MODE_DU4);
}

void loop() {
  int col;
  if (M5.TP.available()) {
    if (!M5.TP.isFingerUp()) {
      M5.TP.update();
    } else {
      tp_finger_t FingerItem = M5.TP.readFinger(0);
      int x = FingerItem.x;
      int y = FingerItem.y;
      M5.TP.update();
      Serial.print(x);
      Serial.print("....");
      Serial.println(y);

      for (int i = 1; i < numBoxes; i++) {
        if ((y >= box[i].y) && (y <= box[i].y + 50)) {
          Serial.print("BOX ");
          Serial.println(i);
          canvas.fillRect(20, box[i].y, 50, 50, 15);
          Serial.print(box[i].veg);
          canvas.drawString(box[i].veg, 20, 800);
          canvas.pushCanvas(0, 0, UPDATE_MODE_DU4);
        }
      }
    }
  }
}

I can't easily check your code just now, have you tried using an unmodified examples sketch that comes with the library you are using?

And look carefully to be sure you are doing exactly what it does, assuming the example works for you.

If you can't make the example work, post it here if you made any changes to it for your own circumstances.

a7

Is not your issue but just interesting - why did you created five boxes, but use only four?

Nvm

Take the note, that the example code takes into account only touches that has a coordinates, different from the previous touch.

1 Like

I'm just testing the touch functionality. the box[0] was situated where my title was so I removed it, that's all. This is by no means a final sketch, it's a work in progress.

Ah, that might be a way around my problem.

Yes.

@steveinaustria - you can use the distance from a previously detected touch, like the examlle carefully does, you can also use time.

You can try variations of debouncing, whose primary objective is to reject things that look like presses but are not. I think a straight ahead debouncing scheme might only need the debounce constant adjusted, maybe even not.

a7

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