[solved] Can't get touchscreen debounce to work

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
trialanderror
 
Posts: 10
Joined: Tue Sep 23, 2014 6:24 am

[solved] Can't get touchscreen debounce to work

Post by trialanderror »

I have been messing with this for quite a while now. My code works exactly for one button press if I comment out the line

Code: Select all

if (p.z < MINPRESSURE || p.z >MAXPRESSURE) touched = LOW;
If I upload the code as I posted it, it does not recognize a button press at all. I absolutely don't understand why. I tried for hours, but I can't find my mistake. Can anyone tell me where I am doing wrong?

Code: Select all

boolean touchState;
boolean lastTouchState = LOW;
long lastDebounceTime = 0;
long debounceDelay = 50; 
boolean touched;

void setup() {
}

void loop() {
  touchscreen();
}

void touchscreen(){
  TSPoint p = ts.getPoint();
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);

  if (p.z < MINPRESSURE || p.z >MAXPRESSURE) touched = LOW;
  if (p.z > MINPRESSURE && p.z < MAXPRESSURE) touched = HIGH;

  if (touched != lastTouchState) {
    lastDebounceTime = millis();
  } 
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (touched != touchState) {
      touchState = touched;
      if (touchState == HIGH) {
        
        p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.height());
        p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.width());
        switch( tft.getRotation() ) {
          case 3:
          swap(p.x, p.y);
          p.x = (480 - p.x);
          break;
        }
        
        if (p.y < 183 && p.y > 55) {
          if (p.x < 48) { 
            if(mute[0] == false) {
              tft.fillRect(0, 40, 48, 12, WHITE);
              tft.setCursor(4, 41);
              tft.print("MUTED");
              mute[0] = true;
            } 
            else if(mute[0] == true) {
              tft.fillRect(0, 40, 48, 12, COLOR1);
              mute[0] = false;
            }
          } 
          else if (p.x < 96) { // some more areas follow, truncated them for posting...
            if(mute[1] == false) {
              tft.fillRect(48, 40, 48, 12, WHITE);
              tft.setCursor(52, 41);
              tft.print("MUTED");
              mute[1] = true;
            } 
            else if(mute[1] == true) {
              tft.fillRect(48, 40, 48, 12, COLOR2);
              mute[1] = false;
            }
          } 
        } 
      }
    }
  }
  lastTouchState = touched;
}
Last edited by trialanderror on Fri Nov 07, 2014 9:08 am, edited 1 time in total.

User avatar
trialanderror
 
Posts: 10
Joined: Tue Sep 23, 2014 6:24 am

Re: Can't get touchscreen debounce to work

Post by trialanderror »

Solved it.

Moved the line detecting "no touch" to the end of the function and changed

Code: Select all

#define NUMSAMPLES 2
in the TouchScreen.cpp to

Code: Select all

#define NUMSAMPLES 5
because of p.z returning lots of zero values even if the screen is touched continously.

This is the working function in case somebody has the the same problem.

Code: Select all

boolean touchState;
boolean lastTouchState = false;
long lastDebounceTime = 0;
long debounceDelay = 50; 
boolean touched;

void touchscreenLiveMode(){
  TSPoint p = ts.getPoint();
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);

  if (p.z > MINPRESSURE && p.z < MAXPRESSURE) { touched = true; }

  if (touched != lastTouchState) {
    lastDebounceTime = millis();
  } 
  
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (touched != touchState) {
      touchState = touched;
      if (touchState == true) {

    // scale from 0->1023 to tft.width
    p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.height());
    p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.width());

    switch( tft.getRotation() ) // perform screen rotation on touchscreen, too
    {
    case 3:
      swap(p.x, p.y);
      p.x = (480 - p.x);
      break;
    }
    if (p.y < 183 && p.y > 55) {
      if (p.x < 48) { 
          if(mute[0] == false) {
            tft.fillRect(0, 40, 48, 12, WHITE);
            tft.setCursor(4, 41);
            tft.print("MUTED");
            mute[0] = true;
          } 
        else if(mute[0] == true) {
          tft.fillRect(0, 40, 48, 12, COLOR1);
          mute[0] = false;
        }
      } 
      else if (p.x < 96) {
        if(mute[1] == false) {
          tft.fillRect(48, 40, 48, 12, WHITE);
          tft.setCursor(52, 41);
          tft.print("MUTED");
          mute[1] = true;
        } 
        else if(mute[1] == true) {
          tft.fillRect(48, 40, 48, 12, COLOR2);
          mute[1] = false;
        }
      } 
        }
      } 
    }
  }
  }
  }
  lastTouchState = touched;
  if (p.z < MINPRESSURE) { touched = false; }
}
Cheers

User avatar
maxausbayern
 
Posts: 8
Joined: Wed Aug 14, 2013 8:22 am

Re: [solved] Can't get touchscreen debounce to work

Post by maxausbayern »

Hello trialanderror,

this is Dieter from Bavaria in good old Germany. It's not so easy to find any posts about the Adafruit touchscreens. I am working on controlling a mediaplayer by a 3.5" TFT incl. touchscreen. The controlling itself is no problem at all, however the reading of the touchscreen "keys" makes me some headaches because that's completely new for me and not yet well documented by Ladyada.
I have already changed the touchscreen library as described by you.
In my sketch there will be toggle keys (like for instance Channel 1, ON_OFF, Filters ON_OFF etc.) and normal keys without toggle function (like Vol+ / Vol-, Backlight + /- etc.). I already made this a few times with hardware keys successfully but now it will be time to change to more modern options.
If possible please supply me with a bit more of your sketch so that I can find out what's useful by myself and adapt it to my needs.

Thanks
Dieter

User avatar
trialanderror
 
Posts: 10
Joined: Tue Sep 23, 2014 6:24 am

Re: [solved] Can't get touchscreen debounce to work

Post by trialanderror »

Hello Dieter,

I am from Germany, too. Ruhr area. :) As it is impossible to send private messages here, please drop me a short message via the contact form on my website:

http://www.limelabs.eu

and I am going to email you the sketch. It is pretty big ( ~3900 lines), so it probably will take some time to extract the information you need.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”