Debouncing Touch Screen

I'm tying to press a touch screen 'button', have it print something, and not do anything until after the button has been released. I tried this, but if you hold the button down, it keeps printing to the touch screen regardless. I tried a few different ways and changed the syntax, but to no avail. Any insight?

//Libraries from Adafruit
#include <Adafruit_GFX.h>    // Core graphics library
#include "Adafruit_HX8357.h"
#include <SPI.h>
#include <SD.h>
#include "TouchScreen.h"

// These are the four touchscreen analog pins
#define YP A2  // must be an analog pin, use "An" notation!
#define XM A3  // must be an analog pin, use "An" notation!
#define YM 7   // can be a digital pin
#define XP 8   // can be a digital pin
// This is calibration data for the raw touch data to the screen coordinates
#define TS_MINX 110
#define TS_MINY 80
#define TS_MAXX 900
#define TS_MAXY 940
#define MINPRESSURE 10
#define MAXPRESSURE 1000

#define boxWidth 150
#define boxHeight 50
#define columnOne 10
#define columnTwo 200
#define rowOne 10
#define rowTwo 100
int toggle=1; //Use to wait until 'button' is released to do something

// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
#define TFT_DC 9
#define TFT_CS 10
// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC);

void setup(void) {
  Serial.begin(9600);
  tft.begin(HX8357D);

  tft.fillScreen(HX8357_BLACK);
  tft.setRotation(0);
  tft.drawRect(rowOne, columnOne, boxHeight, boxWidth, HX8357_WHITE);// _____
  tft.drawRect(rowTwo, columnOne, boxHeight, boxWidth, HX8357_RED);//  |_____|
  tft.drawRect(rowOne, columnTwo, boxHeight, boxWidth, HX8357_GREEN);
  tft.drawRect(rowTwo, columnTwo, boxHeight, boxWidth, HX8357_CYAN);
}

void loop() {
  // Retrieve a point  
  TSPoint p = ts.getPoint();

  //Go back to begginning of loop if no pressure and change 'toggle'
  if (p.z < ts.pressureThreshhold) {
    if (toggle==0){
      toggle=1; 
    }
    return;
  }

  //If pressure is detected, evaluate the following
  p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
  p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());

  //If pressed between x-coordinates, print 'White'
  if (p.x >=10 && p.x<=boxWidth+columnOne){
    if(toggle==1){
    tft.setRotation(1);
    tft.setTextSize(3);
    tft.println("White");
    tft.setRotation(0);
    toggle=0; //
    delay(200); //Just to absolutely make sure arduino has enough time to find next press.
    }
  }
}
  if (p.z < ts.pressureThreshhold) {
    if (toggle==0){
      toggle=1; 
    }
    return;
  }

It is generally better to deal with a "something happened" situation than the "nothing happened" situation.

What does toggle mean to you? What does setting it to 1 mean? People generally use booleans, set to true or false for this.

  if (p.x >=10 && p.x<=boxWidth+columnOne){\

Magic numbers suck. Where do 10 come from?

  // Retrieve a point  
  TSPoint p = ts.getPoint();
[code]   
         
Can you replace this piece with  something like 

while ( no pressure ) // wait for pressure ;
pressure                      // pressure detected        
while( pressure)         // wait for no pressure 
!pressure                    // thumb up  
process

[/code]

To debounce a touch screen you take the average of however many samples you want. The more samples you take that more precise the touch point will be, but the slower it will be too.

I followed Vaclav's advice with the psuedo code (thank you by the way). It seems to change the behavior of the screen, but still has the problem where it keeps reading no touch detected even through the screen is still held down. Increasing from 5 samples to 15 samples did show an improvement, printing "white" less frequently, but it still kept reading no touch detected and moving through. Another strange occurance is that I had to use
TSPoint p = ts.getPoint() in the beginning of loop(), but then had to drop TSPoint later in the loop or else it wouldn't work.

I don't know if perhaps there's an idiosyncrasy in the touchscreen library that conflicts with how my code is constructed. Any suggestions on how to overcome this problem? Thanks.

//Libraries from Adafruit
#include <Adafruit_GFX.h>    // Core graphics library
#include "Adafruit_HX8357.h"
#include <SPI.h>
#include <SD.h>
#include "TouchScreen.h"

// These are the four touchscreen analog pins
#define YP A2  // must be an analog pin, use "An" notation!
#define XM A3  // must be an analog pin, use "An" notation!
#define YM 7   // can be a digital pin
#define XP 8   // can be a digital pin
// This is calibration data for the raw touch data to the screen coordinates
#define TS_MINX 110
#define TS_MINY 80
#define TS_MAXX 900
#define TS_MAXY 940
#define MINPRESSURE 10
#define MAXPRESSURE 1000

#define boxWidth 150
#define boxHeight 50
#define columnOne 10
#define columnTwo 200
#define rowOne 10
#define rowTwo 100

// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
#define TFT_DC 9
#define TFT_CS 10
// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC);

int zCoord=ts.pressureThreshhold+1;
int xCoord=0;
int yCoord=0;
int den=1;

void setup(void) {
  Serial.begin(9600);
  tft.begin(HX8357D);

  tft.fillScreen(HX8357_BLACK);
  tft.setRotation(0);
  //Create Boxes
  tft.drawRect(rowOne, columnOne, boxHeight, boxWidth, HX8357_WHITE);// _____
  tft.drawRect(rowTwo, columnOne, boxHeight, boxWidth, HX8357_RED);//  |_____|
  tft.drawRect(rowOne, columnTwo, boxHeight, boxWidth, HX8357_GREEN);
  tft.drawRect(rowTwo, columnTwo, boxHeight, boxWidth, HX8357_CYAN);
}

void loop() {
  //Retrieve a point  
  TSPoint p = ts.getPoint();
  int zCoord=ts.pressureThreshhold+1;

  while (p.z < ts.pressureThreshhold) {
    p = ts.getPoint(); //For some reason this only works when I get rid of "TSPoint"
  }
  Serial.println("Touch detected");

  den=1;
  xCoord=0;
  yCoord=0;
  Serial.print("zCoord before while loop is "); Serial.println(zCoord);
  while  (zCoord > ts.pressureThreshhold) {
    zCoord=0;
    for (int x=0; x<5; x++){
      TSPoint p = ts.getPoint();
      if (p.z > ts.pressureThreshhold){  //If touch detected, factor new coordinates into the average coordinates
        zCoord=((den*zCoord)+p.z)/den;   //Find the average z coordinate
        xCoord=((den*xCoord)+p.x)/den;
        yCoord=((den*yCoord)+p.y)/den;
        den++;
      }
      Serial.print("zCoord after if statement is "); Serial.println(zCoord);
    }
    Serial.print("zCoord after for loop is "); Serial.println(zCoord);
  }
  Serial.print("xCoord is "); Serial.println(xCoord);
  Serial.print("yCoord is "); Serial.println(yCoord);
  
  //If pressure is detected, evaluate the following
  p.x = map(xCoord, TS_MINX, TS_MAXX, 0, tft.width());
  p.y = map(yCoord, TS_MINY, TS_MAXY, 0, tft.height());
  Serial.print("p.x="); Serial.println(p.x);
  Serial.print("p.y="); Serial.println(p.y);

  tft.setRotation(1);
  tft.setTextSize(3);
  tft.println("White");
  tft.setRotation(0);
  delay(200); //Just to absolutely make sure arduino has enough time to find next press.
  Serial.print("zCoord at the end is "); Serial.println(zCoord);
}

And this is some of the Serial Monitor read out for when I took 5 samples. I was touching the screen the entire time.

zCoord after if statement is 197
zCoord after if statement is 197
zCoord after if statement is 197
zCoord after if statement is 197
zCoord after if statement is 197
zCoord after for loop is 197
zCoord after if statement is 70
zCoord after if statement is 70
zCoord after if statement is 70
zCoord after if statement is 70
zCoord after if statement is 116
zCoord after for loop is 116
zCoord after if statement is 34
zCoord after if statement is 34
zCoord after if statement is 61
zCoord after if statement is 61
zCoord after if statement is 61
zCoord after for loop is 61
zCoord after if statement is 0
zCoord after if statement is 0
zCoord after if statement is 0
zCoord after if statement is 0
zCoord after if statement is 0
zCoord after for loop is 0
xCoord is 1131
yCoord is 585
p.x=413
p.y=281
zCoord at the end is 0
Touch detected
zCoord before while loop is 11
zCoord after if statement is 135
zCoord after if statement is 135
zCoord after if statement is 135
zCoord after if statement is 135
zCoord after if statement is 135
zCoord after for loop is 135
zCoord after if statement is 0
zCoord after if statement is 0
zCoord after if statement is 0
zCoord after if statement is 0

Another strange occurance is that I had to use TSPoint p = ts.getPoint() in the beginning of loop(), but then had to drop TSPoint later in the loop or else it wouldn't work.

There is NOTHING strange about not being able to declare another variable with the same name in the same function. What IS strange is that you don't recognize what TSPoint is/means, and yet you've taken on a complex project like this.