Hello,
i want to make a couple of menus, so if you press the touchscreen button it will change to another menu.
Each menu will have its own "image.bmp". Problem is that when i push this button the screen record a multiple number of pushes. I tried doing delay, but found out that the screen is saving all bounce and send it 1 after 1 back to arduino. This ends up refreshing the screen multiple times, only if i press very quickly than it works fine. But that happens only 1 - 10 times.
there are 3 touch inputs which will activate > menuDraw1 ,2 ,3
this is the screen: Overview | Adafruit 2.8" TFT Touch Shield v2 - Capacitive or Resistive | Adafruit Learning System
So how to debounce this TFT ?
code:
//This example implements a simple sliding On/Off button. The example
// demonstrates drawing and touch operations.
//
//Thanks to Adafruit forums member Asteroid for the original sketch!
//
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.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);
byte RecordOn = 0;
#define FRAME_X 210
#define FRAME_Y 180
#define FRAME_W 100
#define FRAME_H 50
#define REDBUTTON_X FRAME_X
#define REDBUTTON_Y FRAME_Y
#define REDBUTTON_W (FRAME_W/2)
#define REDBUTTON_H FRAME_H
#define GREENBUTTON_X (REDBUTTON_X + REDBUTTON_W)
#define GREENBUTTON_Y FRAME_Y
#define GREENBUTTON_W (FRAME_W/2)
#define GREENBUTTON_H FRAME_H
#define SD_CS 4
byte menuCounter = 0;
boolean menuDraw1 = LOW;
boolean menuDraw2 = LOW;
boolean menuDraw3 = LOW;
int y = 0;
int x = 0;
void drawFrame()
{
tft.drawRect(120, 120, FRAME_W, FRAME_H, ILI9341_BLACK);
}
void redBtn()
{
tft.fillRect(120, 120, REDBUTTON_W, REDBUTTON_H, ILI9341_RED);
tft.fillRect(170, 120, GREENBUTTON_W, GREENBUTTON_H, ILI9341_BLACK);
drawFrame();
tft.setCursor(GREENBUTTON_X + 6 , GREENBUTTON_Y + (GREENBUTTON_H / 2));
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println("ON");
}
void greenBtn()
{
tft.fillRect(120, 120, GREENBUTTON_W, GREENBUTTON_H, ILI9341_GREEN);
tft.fillRect(170, 120, REDBUTTON_W, REDBUTTON_H, ILI9341_BLACK);
//tft.fillRect(GREENBUTTON_X, GREENBUTTON_Y, GREENBUTTON_W, GREENBUTTON_H, ILI9341_GREEN);
//tft.fillRect(REDBUTTON_X, REDBUTTON_Y, REDBUTTON_W, REDBUTTON_H, ILI9341_BLUE);
drawFrame();
tft.setCursor(REDBUTTON_X + 6 , REDBUTTON_Y + (REDBUTTON_H / 2));
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println("OFF");
}
void setup(void)
{
Serial.begin(9600);
tft.begin();
yield();
Serial.print("Initializing SD card...");
if (!SD.begin(SD_CS)) {
Serial.println("failed!");
}
Serial.println("OK!");
tft.setRotation(1);
tft.fillScreen(ILI9341_BLUE);
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)
//redBtn();
//bmpDraw("mainmenu.bmp", 0, 0);
menuDraw1 = HIGH;
}
void loop()
{
// tft.drawBitmap(0, 0, "menu.bmp", 320, 240, ILI9341_BLACK);
// See if there's any touch data for us
if (!ts.bufferEmpty())
{
// Retrieve a point
TS_Point p = ts.getPoint();
delay(200);
// Scale using the calibration #'s
// and rotate coordinate system
p.x = map(p.x, TS_MINY, TS_MAXY, 0, tft.height());
p.y = map(p.y, TS_MINX, TS_MAXX, 0, tft.width());
y = tft.height() - p.x;
x = p.y;
//bmpDraw("menu.bmp",0,0);
if ((x > 0) && (x < 30)) {
if ((y > 200) && (y < 240)) {
Serial.println("R = 1");
menuDraw1 = HIGH;
y = 0;
x = 0;
}
}
if ((x > 290) && (x < 320)) {
if ((y > 200) && (y < 240)) {
Serial.println("R = 2");
menuDraw2 = HIGH;
y = 0;
x = 0;
}
}
if ((x > 150) && (x < 230)) {
if ((y > 200) && (y < 240)) {
Serial.println("R = 3");
menuDraw3 = HIGH;
y = 0;
x = 0;
}
}
if (menuDraw3 == HIGH) {
bmpDraw("mainmenu.bmp", 0, 0);
tft.setCursor(5, 55); /////////// -->, ^^
tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);
tft.setTextSize(1);
tft.print("Temperatuur =");
tft.print(menuDraw1);
tft.print(" ");
tft.print(menuDraw2);
tft.print(" ");
tft.println(menuDraw3);
tft.setCursor(5, 45); /////////// -->, ^^
tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);
tft.setTextSize(1);
tft.print("Vochtigheid=");
tft.println("46 ");
menuDraw3=LOW;
}
if (menuDraw1 == HIGH) {
tft.fillScreen(ILI9341_BLUE);
tft.setCursor(5, 55); /////////// -->, ^^
tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);
tft.setTextSize(1);
tft.print("Temperatuur =");
tft.print(menuDraw1);
tft.print(" ");
tft.print(menuDraw2);
tft.print(" ");
tft.println(menuDraw3);
tft.setCursor(5, 45); /////////// -->, ^^
tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);
tft.setTextSize(1);
tft.print("Vochtigheid=");
tft.println("46 ");
tft.setCursor(5, 35); /////////// -->, ^^
tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);
tft.setTextSize(1);
tft.print("Afzuiging=");
tft.println("100 ");
tft.setCursor(5, 25); /////////// -->, ^^
tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);
tft.setTextSize(1);
tft.print("NachtMode=");
tft.println(" nee");
menuDraw1=LOW;
}
if (menuDraw2 == HIGH) {
tft.fillScreen(ILI9341_GREEN);
tft.setCursor(5, 55); /////////// -->, ^^
tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);
tft.setTextSize(1);
tft.print("Temperatuur =");
tft.print(menuDraw1);
tft.print(" ");
tft.print(menuDraw2);
tft.print(" ");
tft.println(menuDraw3);
tft.setCursor(5, 45); /////////// -->, ^^
tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);
tft.setTextSize(1);
tft.print("Vochtigheid=");
tft.println("46 ");
menuDraw2=LOW;
}
}
}