Arduino Giga, can't debounce my first touch button

#include "Arduino_GigaDisplay_GFX.h"
#include "Arduino_GigaDisplayTouch.h"

GigaDisplay_GFX display;
Arduino_GigaDisplayTouch touchDetector;
#define BLACK 0x0000
#define RED 0xD105
#define WHITE 0xffff
#define GREEN 0x67E0
#define BLUE 0x001F
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define WHITE 0xFFFF
#define YELLOW 0xFFE0
#define ORANGE 0XFDC0
bool RecMode = false;
// Button Dimensions


void setup() {
  display.begin();
  touchDetector.begin();
  display.setRotation(1);

  // Draw the button
  display.fillScreen(BLACK);                       // White background
  display.fillRoundRect(655, 5, 140, 70, 7, GREEN);  // x, y, width, height, color
  display.setCursor(680, 30);
  display.setTextColor(BLACK);
  display.setTextSize(4);
  display.print("STBY");
}

void loop() {
  uint8_t contacts;
  GDTpoint_t points[5];

  // Read the touch screen
  contacts = touchDetector.getTouchPoints(points);

  if (contacts > 0) {

    //  Serial.print ("TouchX" + String(points[0].x));
    //Serial.print ("  touchY= ");
    //Serial.println (points[0].y);
    if (points[0].x >= 410 && points[0].x <= 500 && points[0].y >= 660 && points[0].y <= 800) {

      if (!RecMode) {
        display.fillRoundRect(655, 5, 140, 70, 7, RED);  // x, y, width, height, color
        display.setCursor(680, 30);
        display.setTextColor(BLACK);
        display.setTextSize(4);
        display.print("REC");
        RecMode = true;
      } else {
        display.fillRoundRect(655, 5, 140, 70, 7, GREEN);  // x, y, width, height, color
        display.setCursor(680, 30);
        display.setTextColor(BLACK);
        display.setTextSize(4);
        display.print("STBY");
        RecMode = false;
      }

      Serial.println(RecMode);
      delay(100);  // Simple debounce
    } 
  }
}


when I touch it gives me multiple 01010
I tried various delays….

By the way what’s the correct way to debounce without delay?

Thanks a lot

Mitch

Does contacts ever go back to 0 if you stop touching the screen? If so you can have a flag like “isTouched” that basically says “ok the screen was touched, now ignore any other touches”

Then if contacts is equal to 0 then reset the “isTouched” flag.

when is touched, the value goes 0101010
Can you show me an example of “isTouched”?

Thanks

bool isTouched = false; // global variable

then inside “if (contacts > 0) “

isTouched = true;

Now you add your other logic inside

if (isTouched) {

“Your logic here”

}

if(contacts == 0) { isTouched = false; }

Sorry it’s not easy to type all this out on my phone

I tried, but it still gives me lots of changes:

#include "Arduino_GigaDisplay_GFX.h"
#include "Arduino_GigaDisplayTouch.h"

GigaDisplay_GFX display;
Arduino_GigaDisplayTouch touchDetector;
#define BLACK 0x0000
#define RED 0xD105
#define WHITE 0xffff
#define GREEN 0x67E0
#define BLUE 0x001F
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define WHITE 0xFFFF
#define YELLOW 0xFFE0
#define ORANGE 0XFDC0
bool RecMode = false;
// Button Dimensions
bool isTouched = false;

void setup() {
  display.begin();
  touchDetector.begin();
  display.setRotation(1);

  // Draw the button
  display.fillScreen(BLACK);                         // White background
  display.fillRoundRect(655, 5, 140, 70, 7, GREEN);  // x, y, width, height, color
  display.setCursor(680, 30);
  display.setTextColor(BLACK);
  display.setTextSize(4);
  display.print("STBY");
}

void loop() {
  uint8_t contacts;
  GDTpoint_t points[5];

  // Read the touch screen
  contacts = touchDetector.getTouchPoints(points);

  if (contacts > 0) {
    isTouched = true;
    //  Serial.print ("TouchX" + String(points[0].x));
    //Serial.print ("  touchY= ");
    //Serial.println (points[0].y);
    if (isTouched) {
      if (points[0].x >= 410 && points[0].x <= 500 && points[0].y >= 660 && points[0].y <= 800 and isTouched) {

        if (!RecMode) {
          display.fillRoundRect(655, 5, 140, 70, 7, RED);  // x, y, width, height, color
          display.setCursor(680, 30);
          display.setTextColor(BLACK);
          display.setTextSize(4);
          display.print("REC");
          RecMode = true;
        } else {
          display.fillRoundRect(655, 5, 140, 70, 7, GREEN);  // x, y, width, height, color
          display.setCursor(680, 30);
          display.setTextColor(BLACK);
          display.setTextSize(4);
          display.print("STBY");
          RecMode = false;
        }

        Serial.println(RecMode);
        //delay(100);  // Simple debounce
      }
    }
  }
  if (contacts == 0) {
    isTouched = false;
  }
}

Interesting, can you print contacts and see what it returns? Is it what is returning 010101? I am not familiar with the Arduino_GigaDisplayTouch library so I don’t know what flags it has or how best to use it.

It seems you are getting the correct information, but need to ignore multiple identical values. This is NOT the same as debouncing a mechanical switch. Save the initial value and ignore duplicates until a different value is returned.

Try this. Please note it is not 100% optimal as more can be done to improve it.

#include "Arduino_GigaDisplay_GFX.h"
#include "Arduino_GigaDisplayTouch.h"

GigaDisplay_GFX display;
Arduino_GigaDisplayTouch touchDetector;
constexpr uint16_t BLACK = 0x0000;
constexpr uint16_t RED = 0xD105;
constexpr uint16_t WHITE = 0xffff;
constexpr uint16_t GREEN = 0x67E0;
constexpr uint16_t BLUE = 0x001F;
constexpr uint16_t CYAN = 0x07FF;
constexpr uint16_t MAGENTA = 0xF81F;
constexpr uint16_t YELLOW = 0xFFE0;
constexpr uint16_t ORANGE = 0XFDC0;

bool recMode = false;
bool isTouched = false;

struct ButtonAttributes {
  const uint16_t color;
  const char* text;
};

// ----------- Helpers ------------
enum ButtonStates { RECORD, STANDBY };

ButtonAttributes attrs[] = {
  { RED, "REC" },
  { GREEN, "STBY" }
};

bool touchArea(const GDTpoint_t point, const int xLow, const int xHigh, const int yLow, const int yHigh) {
  return (point.x >= xLow && point.x <= xHigh) && (point.y >= yLow && point.y <= yHigh);
}

// You can use this if you want
// void drawButton(uint16_t x, uint16_t y, uint16_t w, uint16_t h, bool state) {
//   const auto& attr = attrs[state];
//   display.fillRoundRect(x, y, w, h, 7, attr.color);
//   display.setCursor(x + 25, y + 25);  // Adjust text positioning
//   display.setTextColor(BLACK);
//   display.setTextSize(4);
//   display.print(attr.text);
// }

// ------------- Main Code -------------
void setup() {
  display.begin();
  touchDetector.begin();
  display.setRotation(1);

  // Clear the screen
  display.fillScreen(BLACK); // Black background unless the screen is set to be inverted?

  // If this is going to be repeated, then you may want to make it a function that you can call rather than constantly rewritting it.
  display.fillRoundRect(655, 5, 140, 70, 7, attrs[RECORD].color);  // x, y, width, height, cornerRadius, color
  display.setCursor(680, 30);
  display.setTextColor(BLACK);
  display.setTextSize(4);
  display.print(attrs[RECORD].text);
}

void loop() {
  static GDTpoint_t points[5];

  // Read the touch screen
  uint8_t contacts = touchDetector.getTouchPoints(points);

  if (contacts > 0) {
    if (touchArea(points[0], 410, 500, 660, 800) && !isTouched) {
      isTouched = true;

      const auto& buttonAttributes = attrs[ recMode ];
      display.fillRoundRect(655, 5, 140, 70, 7, buttonAttributes.color);  // x, y, width, height, cornerRadius, color
      display.setCursor(680, 30);
      display.setTextColor(BLACK);
      display.setTextSize(4);
      display.print(buttonAttributes.text);
      recMode = !recMode;

      Serial.println(recMode);
    }
  } else {
    isTouched = false;
  }
}

That didn’t do it . Thanks for trying
I get 12:23:56.612 -> 0

12:23:56.644 -> 1

12:23:56.675 -> 0

12:23:56.675 -> 1

12:23:56.708 -> 0

I found a debounced method based on recording millis().
It works but if the button is continuously pressed, the state changes at the end of the interval,

Simple_TOUCH_debouncedW_Millis.ino (2.1 KB)

Please post sketches here rather than attaching them

sorry here is the code
#include "Arduino_GigaDisplay_GFX.h"

#include "Arduino_GigaDisplayTouch.h"

GigaDisplay_GFX display;

Arduino_GigaDisplayTouch touchDetector;

#define BLACK 0x0000

#define RED 0xD105

#define WHITE 0xffff

#define GREEN 0x67E0

#define BLUE 0x001F

#define CYAN 0x07FF

#define MAGENTA 0xF81F

#define WHITE 0xFFFF

#define YELLOW 0xFFE0

#define ORANGE 0XFDC0

bool RecMode = false;

bool ReadyForNextTouch=1;

unsigned long lastTouch = 0;

// Button Dimensions

void setup() {

display.begin();

touchDetector.begin();

display.setRotation(1);

// Draw the button

display.fillScreen(BLACK); // White background

display.fillRoundRect(655, 5, 140, 70, 7, GREEN); // x, y, width, height, color

display.setCursor(680, 30);

display.setTextColor(BLACK);

display.setTextSize(4);

display.print("STBY");

}

void loop() {

uint8_t contacts;

GDTpoint_t points[5];

// Read the touch screen

contacts = touchDetector.getTouchPoints(points);

if (contacts ==0)

{ReadyForNextTouch=true;

}

Serial.println (contacts);

Serial.println ("ReadyForNextTouch= "+ String(ReadyForNextTouch));

if (contacts > 0 and millis() - lastTouch > 250 and ReadyForNextTouch) {

//  Serial.print ("TouchX" + String(points\[0\].x));

//Serial.print ("  touchY= ");

//Serial.println (points\[0\].y);

if (points[0].x >= 410 && points[0].x <= 500 && points[0].y >= 660 && points[0].y <= 800) {

if (!RecMode) {

display.fillRoundRect(655, 5, 140, 70, 7, RED); // x, y, width, height, color

display.setCursor(680, 30);

display.setTextColor(BLACK);

display.setTextSize(4);

display.print("REC");

    RecMode = true;

} else {

display.fillRoundRect(655, 5, 140, 70, 7, GREEN); // x, y, width, height, color

display.setCursor(680, 30);

display.setTextColor(BLACK);

display.setTextSize(4);

display.print("STBY");

    RecMode = false;

}

  lastTouch = millis();

  ReadyForNextTouch=false;

Serial.println("RecMode= " + String(RecMode));

}

}

//delay(200);

}

really not hard..
first get the points, then yes a simple millis timer to decide if it should be tossed away..
had to debounce the touch when coding solitaire , check the loop()..

good luck.. ~q