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.
}
}
}