🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    M5Stack is printing 'touches' more than once on my M5 Paper

    General
    2
    3
    1.0k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B
      bs811
      last edited by bs811

      Hey so I currently got this code of a keypad I built, its still WIP but I'm trying to print out the number I press into the serial monitor. Now the problem comes, the printed number is correct but for whatever reasons it gets printed 3 times in total. I found out that when I touch and hold 1 number gets printed, but as soon as I let go 2 more numbers get printed.

      I really dont get why, tried everything

      #include <M5EPD.h>
      
      M5EPD_Canvas canvas(&M5.EPD);
      
      void setup() {
        M5.begin();
        M5.EPD.SetRotation(90);
        M5.TP.SetRotation(90);
        M5.EPD.Clear(true);
        canvas.createCanvas(540, 960);
        canvas.setTextSize(3);
        Serial.begin(115200);
      }
      
      void loop() {
        drawKeyboard();
        int playerId = getPlayerIdFromTouchscreen();
        Serial.println("Eingegebene Player ID: " + String(playerId));
        delay(2000); // Wartezeit, um die Ausgabe zu sehen
      }
      
      void drawKeyboard() {
        // Zeichne Tastatur
        M5.EPD.Clear(true);
        Serial.println("Geben Sie die Player ID ein:");
      
        M5EPD_Canvas canvas(&M5.EPD);
        canvas.createCanvas(540, 960);
        canvas.setTextSize(3);
      
        // Zentriere die Tastatur
        int xOffset = 0;
        int yOffset = (canvas.height() - 720);
      
        // Buttons zeichnen
        for (int i = 0; i < 3; i++) {
          for (int j = 0; j < 3; j++) {
            int number = i * 3 + j + 1;
            canvas.fillRect(xOffset + j * 180, yOffset + i * 180, 179, 179, 0xFFFF);
            canvas.drawString(String(number), xOffset + j * 180 + 80, yOffset + i * 180 + 76);
          }
        }
      
        // 0 und OK Buttons zeichnen
        canvas.fillRect(xOffset + 360, yOffset + 540, 179, 179, 0xFFFF);
        canvas.fillRect(xOffset + 180, yOffset + 540, 179, 179, 0xFFFF);
        canvas.fillRect(xOffset, yOffset + 540, 179, 179, 0xFFFF);
      
        canvas.drawString("OK", xOffset + 432, yOffset + 620);
        canvas.drawString("0", xOffset + 262, yOffset + 620);
        canvas.drawString("DLT", xOffset + 60, yOffset + 620);
      
        canvas.pushCanvas(0, 0, UPDATE_MODE_GC16);
      }
      
      int getPlayerIdFromTouchscreen() {
        String inputDigits = "";
        bool inputDetected = false;  // Flagge, um mehrfache Erkennungen zu verhindern
      
        while (true) {
          M5.update();
      
          if (M5.TP.available() && !M5.TP.isFingerUp()) {
            M5.TP.update();
            int touchX = M5.TP.readFinger(0).x;
            int touchY = M5.TP.readFinger(0).y;
      
            int xOffset = 0;
            int yOffset = (canvas.height() - 720);
      
            for (int i = 0; i < 3; i++) {
              for (int j = 0; j < 3; j++) {
                int buttonX = xOffset + j * 180;
                int buttonY = yOffset + i * 180;
      
                if (touchX >= buttonX && touchX <= (buttonX + 179) && touchY >= buttonY && touchY <= (buttonY + 179)) {
                  // Die Taste wurde berührt
                  int number = i * 3 + j + 1;
      
                  // Überprüfe, ob diese Taste bereits erkannt wurde
                  if (!inputDetected) {
                    // Setze die Flagge, um mehrfache Erkennungen zu verhindern
                    inputDetected = true;
      
                    // Serial-Ausgabe, um die gedrückte Taste anzuzeigen (optional)
                    Serial.println("Gedrückte Taste: " + String(number));
      
                    // Füge die Ziffer zur Zwischenvariable hinzu
                    inputDigits += String(number);
                  }
                }
              }
            }
      
            // Überprüfen, ob OK-Taste berührt wurde
            if (touchX >= (xOffset + 360) && touchX <= (xOffset + 539) && touchY >= (yOffset + 540) && touchY <= (yOffset + 719)) {
              // Benutzereingabe beenden, wenn OK-Taste gedrückt wurde
              break;
            }
          }
      
          if (M5.TP.isFingerUp()) {
            // Wenn der Finger losgelassen wurde, setze die Flagge zurück
            inputDetected = false;
          }
        }
      
        Serial.println("Eingegebene Player ID: " + inputDigits); // Serial-Ausgabe am Ende
      
        return inputDigits.toInt(); // Konvertiere die Zeichenkette zu einer Ganzzahl
      }
      
      1 Reply Last reply Reply Quote 0
      • ajb2k3A
        ajb2k3
        last edited by

        You are probably seeing what is known as "Key Bounce" and need to look into "debouncing" the touch events.

        UIFlow, so easy an adult can learn it!
        If I don't know it, be patient!
        I've ether not learned it or am too drunk to remember it!
        Author of the WIP UIFlow Handbook!
        M5Black, Go, Stick, Core2, and so much more it cant be fit in here!

        B 1 Reply Last reply Reply Quote 0
        • B
          bs811 @ajb2k3
          last edited by

          @ajb2k3 yeah I fixed it. Currently fixing the delete button bcuz its shows the preivious number very subutle

          1 Reply Last reply Reply Quote 0
          • First post
            Last post