prova così
praticamente nel loop legge fintanto che ci sono caratteri nel buffer (while (!ts.bufferEmpty()))
prima di proseguire
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_STMPE610.h>
#define TS_MINX 150
#define TS_MINY 130
#define TS_MAXX 3800
#define TS_MAXY 4000
#define MINPRESSURE 10
#define MAXPRESSURE 1000
#define STMPE_CS 8
Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);
#define TFT_CS 10
#define TFT_DC 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
boolean RecordOn = false;
#define FRAME_X 10
#define FRAME_Y 20
#define FRAME_W 100
#define FRAME_H 100
#define IN_BUTTON_X FRAME_X
#define IN_BUTTON_Y FRAME_Y
#define IN_BUTTON_W (FRAME_W)
#define IN_BUTTON_H FRAME_H
#define OUT_BUTTON_X (IN_BUTTON_X + IN_BUTTON_W + 15)
#define OUT_BUTTON_Y FRAME_Y
#define OUT_BUTTON_W (FRAME_W)
#define OUT_BUTTON_H FRAME_H
void drawFrame() {
tft.drawRect(FRAME_X, FRAME_Y, FRAME_W, FRAME_H, ILI9341_BLACK);
}
void pressed_Btn() {
tft.fillRect(IN_BUTTON_X, IN_BUTTON_Y, IN_BUTTON_W, IN_BUTTON_H, ILI9341_BLUE);
drawFrame();
tft.setCursor(OUT_BUTTON_X , OUT_BUTTON_Y + (OUT_BUTTON_H / 2));
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
tft.println("ON");
RecordOn = false;
}
void released_Btn() {
tft.fillRect(IN_BUTTON_X, IN_BUTTON_Y, IN_BUTTON_W, IN_BUTTON_H, ILI9341_WHITE);
drawFrame();
tft.setCursor(OUT_BUTTON_X , OUT_BUTTON_Y + (OUT_BUTTON_H / 2));
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
tft.println("ON");
RecordOn = true;
}
void setup(void) {
Serial.begin(9600);
tft.begin();
if (!ts.begin()) {
Serial.println("Unable to start touchscreen.");
} else {
Serial.println("Touchscreen started.");
}
tft.fillScreen(ILI9341_BLACK);
// origin = left,top landscape (USB left upper)
tft.setRotation(2);
released_Btn();
}
void loop() {
// See if there's any touch data for us
TS_Point p ;
while (!ts.bufferEmpty()) {
// Retrieve a point
p = ts.getPoint();
// Scale using the calibration #'s
// and rotate coordinate system
}
p.x = map(p.x, 130, 4000, tft.width(), 0);
p.y = map(p.y, 150, 3800, tft.height(), 0);
int y = p.y;
int x = p.x;
if (RecordOn) {
if ((x > IN_BUTTON_X) && (x < (IN_BUTTON_X + IN_BUTTON_W))) {
if ((y > IN_BUTTON_Y) && (y <= (IN_BUTTON_Y + IN_BUTTON_H))) {
Serial.println("Red btn hit");
pressed_Btn();
}
}
} else { //Record is off (RecordOn == false)
if ((x > IN_BUTTON_X) && (x < (IN_BUTTON_X + IN_BUTTON_W))) {
if ((y > IN_BUTTON_Y) && (y <= (IN_BUTTON_Y + IN_BUTTON_H))) {
Serial.println("Green btn hit");
released_Btn();
}
}
}
Serial.println(RecordOn);
}