Hey all,
I've an TFT LCD shield touchscreen plugged into my Arduino MEGA, and I uploaded this following code
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include <TouchScreen.h>
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
MCUFRIEND_kbv tft;
#define BUTTON_SIZE 80
#define BUTTON_COLOR 0x07E0
#define TEXT_COLOR YELLOW
#define SCREEN_COLOR BLACK
#define TS_MINX 254
#define TS_MINY 127
#define TS_MAXX 870
#define TS_MAXY 911
#define XP 8
#define YP A3
#define XM A2
#define YM 9
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
enum ScreenState {
TELA_INICIAL,
MENU
};
ScreenState currentState = TELA_INICIAL;
void drawButton(int x, int y, int size, uint16_t color, const char *label) {
tft.fillRect(x, y, size, size, color);
tft.drawRect(x, y, size, size, WHITE);
tft.setTextSize(2);
tft.setTextColor(TEXT_COLOR);
tft.setCursor(x + size / 4, y + size / 4);
tft.print(label);
}
void displayTelaInicial() {
tft.fillScreen(SCREEN_COLOR);
tft.setTextSize(2);
tft.setTextColor(WHITE);
tft.println("Toque para iniciar");
tft.println("Touch to start");
}
void displayMenu() {
tft.fillScreen(SCREEN_COLOR);
// Botões do lado esquerdo
for (int i = 0; i < 3; i++) {
drawButton(10, i * (BUTTON_SIZE + 10), BUTTON_SIZE, BLACK, ("Btn" + String(i + 1)).c_str());
}
// Botões do lado direito
for (int i = 0; i < 3; i++) {
drawButton(tft.width() - BUTTON_SIZE - 10, i * (BUTTON_SIZE + 10), BUTTON_SIZE, BLACK, ("Btn" + String(i + 4)).c_str());
}
// Botões no topo
drawButton(tft.width() / 2 - BUTTON_SIZE / 2, 10, BUTTON_SIZE, BLACK, "Btn7");
drawButton(tft.width() / 2 - BUTTON_SIZE / 2, 80, BUTTON_SIZE, BLACK, "Btn8");
// Adicionando um botão personalizado
int customButtonX = tft.width() / 2 - BUTTON_SIZE / 2;
int customButtonY = tft.height() / 2 - BUTTON_SIZE / 2;
drawButton(customButtonX, customButtonY, BUTTON_SIZE, BUTTON_COLOR, "CustomBtn");
tft.setCursor(customButtonX + BUTTON_SIZE / 4, customButtonY + BUTTON_SIZE / 4);
tft.print("CustomBtn");
tft.drawRect(customButtonX, customButtonY, BUTTON_SIZE, BUTTON_SIZE, WHITE);
}
void setup() {
Serial.begin(9600);
uint32_t when = millis();
if (!Serial) delay(5000);
tft.begin(tft.readID());
tft.setRotation(0);
displayTelaInicial();
}
void loop() {
TSPoint touch = ts.getPoint();
if (currentState == TELA_INICIAL && touch.z > 100 && touch.z < 1000) {
currentState = MENU;
displayMenu();
delay(200); // Evita múltiplos toques rápidos
} else if (currentState == MENU && touch.z > 50 && touch.z < 1000) {
// Adicione lógica para verificar qual botão foi tocado e execute a ação desejada
int buttonPressed = checkButtonPressed(touch.x, touch.y);
handleButtonAction(buttonPressed);
// Aguarde até que o toque seja liberado antes de considerar outros toques
while (ts.getPoint().z > 1) {
delay(10);
}
currentState = TELA_INICIAL;
displayTelaInicial();
}
}
int checkButtonPressed(int x, int y) {
// Verifique qual botão foi pressionado com base nas coordenadas (x, y)
// Adapte esta lógica conforme necessário para atender à disposição dos seus botões
if (x >= 10 && x <= BUTTON_SIZE + 10 && y >= 0 && y <= BUTTON_SIZE) {
return 1;
} else if (x >= tft.width() - BUTTON_SIZE - 10 && x <= tft.width() - 10 && y >= 0 && y <= BUTTON_SIZE) {
return 4;
} else if (x >= tft.width() / 2 - BUTTON_SIZE / 2 && x <= tft.width() / 2 + BUTTON_SIZE / 2 && y >= 10 && y <= 10 + BUTTON_SIZE) {
return 7;
} else if (x >= tft.width() / 2 - BUTTON_SIZE / 2 && x <= tft.width() / 2 + BUTTON_SIZE / 2 && y >= 80 && y <= 80 + BUTTON_SIZE) {
return 8;
} else if (x >= tft.width() / 2 - BUTTON_SIZE / 2 && x <= tft.width() / 2 + BUTTON_SIZE / 2 && y >= tft.height() / 2 - BUTTON_SIZE / 2 && y <= tft.height() / 2 + BUTTON_SIZE / 2) {
return 9; // Botão personalizado
}
return 0; // Nenhum botão pressionado
}
void handleButtonAction(int button) {
// Adicione a lógica para a ação de cada botão aqui
// Atualmente, apenas imprime a ação associada ao botão
Serial.println("Botão pressionado: " + String(button));
}
The first screen (screen touch to start) is working perfectly, but when I touch the screen, the following "menu" shows one blank white screen, and if I touch something, nothing changes.
Can someone help me please?