> 1) areas outside of the button are detected as button hits.
Thanks for the feedback. New code is near bottom; nested 'ifs' could likely be rewritten to be more elegant, but it works and I could not get more 'elegant' code to work.
Code: Select all
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_STMPE610.h>
// This is calibration data for the raw touch data to the screen coordinates
#define TS_MINX 150
#define TS_MINY 130
#define TS_MAXX 3800
#define TS_MAXY 4000
#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;
void frame()
{
tft.drawRect(210, 180, 100, 50, ILI9341_BLACK);
}
void redBtn()
{
tft.fillRect(212, 182, 45, 45, ILI9341_RED);
tft.fillRect(260, 180, 50, 50, ILI9341_BLUE);
frame();
tft.setCursor(tft.width() - 50, tft.height() - 42);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println("ON");
RecordOn = false;
}
void greenBtn()
{
tft.fillRect(262, 182, 45, 45, ILI9341_GREEN);
tft.fillRect(210, 180, 50, 50, ILI9341_BLUE);
frame();
tft.setCursor(tft.width() - 100, tft.height() - 42);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println("OFF");
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_BLUE);
// origin = left,top landscape (USB left upper)
tft.setRotation(1);
redBtn();
frame();
}
void loop()
{
// See if there's any touch data for us
if (!ts.bufferEmpty())
{
// Retrieve a point
TS_Point p = ts.getPoint();
// Scale from ~0->4000 to tft.width using the calibration #'s
p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
// Compensate for rotation
int x = tft.height() - p.x;
int y = p.y;
Serial.print("("); Serial.print(x);
Serial.print(", "); Serial.print(y);
Serial.println(")");
if(x > 165 && x < 200) {
if (y > 150 && y < 180) {
Serial.println("Red btn hit"); redBtn(); frame();}
}
if(x > 165 && x < 200) {
if (y > 180 && y < 215) {
Serial.println("Green btn hit"); greenBtn(); frame();}
}
Serial.println(RecordOn);
return;
}
}
This code will work fine for my project, but I have concerns about widespread applicability.
I'm not happy with the mapping coordinates; they appear to have little correlation with the actual display coordinates. e.g., sometimes the x and y coordinates appear to be reversed. In general, I am unable to pinpoint the problem. Furthermore, the rotation compensation that I had to apply is counter-intuitive; I would have expected to use
int x = p.x;
int y = tft.height() - p.y;
instead of
int x = tft.height() - p.x;
int y = p.y;
which I wound up using to obtain semi-believable numbers.
Note that in the posted changes:
if(x > 165 && x < 200) {
if (y > 150 && y < 180) {
Serial.println("Red btn hit"); redBtn(); frame();}
}
if(x > 165 && x < 200) {
if (y > 180 && y < 215) {
Serial.println("Green btn hit"); greenBtn(); frame();}
}
the x values are in the same range and the y values increase from one side of the button to the other. I would have expected the opposite, the y values should have been the same and the x values should have increased. I did what I had to do to make it work, but none of it makes sense to me.
> 2) I'm still seeing multiple detection events of single touch points...Weren't you trying to eliminate that?
I don't have a clue how to fix this. If we knew when the touch 'event' started and stopped, then perhaps we could save these values to an array and then average them to get a single value. The way it is now, there is some 'flash' due to multiple redrawings of the buttons. The STMPE610 manual has an interesting section which states:
"The STMPE610 touchscreen controller can be configured to oversample by 2/4/8 times and
provide the averaged value as final output. This feature helps to reduce the effect of surrounding noise."
There are also registers to change the origin: 0x42-0x48, but I am uncertain if these would help.
I have tried to be constructively critical. Overall, I am thrilled with this lcd and touch screen and hope to use it extensively in upcoming projects. I greatly appreciate your time and efforts.