Hi there,
I have this code that creates a bunch of buttons on my LCD screen. can anyone tell me why the buttons are not working when I pressed them?
#include <TFT_eSPI.h>
#include <XPT2046_Touchscreen.h>
#include <SPI.h>
#include <Wire.h>
#define XPT2046_IRQ 35
#define XPT2046_MOSI 32
#define XPT2046_MISO 34
#define XPT2046_CLK 27
#define XPT2046_CS 5
#define BL 21
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 480
// TFT display and touchscreen instances
TFT_eSPI tft = TFT_eSPI();
SPIClass touchscreenSPI(VSPI);
XPT2046_Touchscreen touchscreen(XPT2046_CS, XPT2046_IRQ);
int buttonWidthBig = 110; // Adjusted for uniform size
int buttonHeightBig = 100; // Adjusted for uniform size
int buttonDiameterMedium = 84;
int buttonWidthSmall = 45;
int buttonHeightSmall = 45;
int buttonWidthWide = 90; // New width for Slow, Medium, Fast, Extreme buttons
uint16_t buttonNormalColor = tft.color565(0, 100, 0); // Dark Green
uint16_t buttonPressedColor = tft.color565(255, 255, 255); // White
uint16_t buttonTextColor = TFT_BLACK; // Black text on white
void setup() {
Serial.begin(115200);
tft.init();
tft.setRotation(1);
touchscreenSPI.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
touchscreen.begin(touchscreenSPI);
touchscreen.setRotation(1);
pinMode(BL, OUTPUT);
digitalWrite(BL, HIGH);
uint16_t darkGreen = tft.color565(34, 139, 34);
uint16_t lightGreen = tft.color565(144, 238, 144);
tft.fillScreen(lightGreen);
tft.setTextColor(TFT_WHITE);
tft.setTextSize(2);
tft.setTextDatum(MC_DATUM);
// Mode section
int modeX = 20, modeY = 40, modeWidth = 160, modeHeight = 420;
tft.fillRoundRect(modeX, modeY, modeWidth, modeHeight, 10, darkGreen);
tft.setTextColor(TFT_WHITE, darkGreen);
tft.drawString("Mode", modeX + modeWidth / 2, modeY + 20);
int modeButtonSpacing = (modeHeight - (buttonHeightBig * 3)) / 4;
drawControlButton(modeX + (modeWidth - buttonWidthBig) / 2, modeY + modeButtonSpacing + 20, buttonWidthBig, buttonHeightBig, "Golf", darkGreen);
drawControlButton(modeX + (modeWidth - buttonWidthBig) / 2, modeY + modeButtonSpacing * 2 + buttonHeightBig + 20, buttonWidthBig, buttonHeightBig, "Game", darkGreen);
drawControlButton(modeX + (modeWidth - buttonWidthBig) / 2, modeY + modeButtonSpacing * 3 + buttonHeightBig * 2 + 20, buttonWidthBig, buttonHeightBig, "Gym", darkGreen);
// Center buttons in the Mode section
// int verticalSpacing = (modeHeight - (buttonHeightBig * 3 + 20)) / 4; // 20px space between buttons
// drawRoundedButton(modeX + 10, modeY + verticalSpacing + 15, buttonWidthBig, buttonHeightBig, "Play Golf", darkGreen);
// drawRoundedButton(modeX + 10, modeY + verticalSpacing + buttonHeightBig + 20 + 15, buttonWidthBig, buttonHeightBig, "Play Game", darkGreen);
// drawRoundedButton(modeX + 10, modeY + verticalSpacing + 2 * (buttonHeightBig + 20) + 15, buttonWidthBig, buttonHeightBig, "Gym", darkGreen);
// Terrain section
int terrainX = 200, terrainY = 40, terrainWidth = 400, terrainHeight = 140;
tft.fillRoundRect(terrainX, terrainY, terrainWidth, terrainHeight, 10, darkGreen);
tft.setTextColor(TFT_WHITE, darkGreen);
tft.drawString("Terrain", terrainX + terrainWidth / 2, terrainY + 20);
drawCircularButton(250, 120, buttonDiameterMedium, "Tee", darkGreen);
drawCircularButton(350, 120, buttonDiameterMedium, "Fairway", darkGreen);
drawCircularButton(450, 120, buttonDiameterMedium, "Rough", darkGreen);
drawCircularButton(550, 120, buttonDiameterMedium, "Bunker", darkGreen);
// Speed section with doubled width for buttons
int speedX = 200, speedY = 200, speedWidth = 400, speedHeight = 120;
tft.fillRoundRect(speedX, speedY, speedWidth, speedHeight, 10, darkGreen);
tft.setTextColor(TFT_WHITE, darkGreen);
tft.drawString("Speed", speedX + speedWidth / 2, speedY + 20);
drawWideButton(205, 250, buttonWidthWide, buttonHeightSmall, "Slow", darkGreen);
drawWideButton(305, 250, buttonWidthWide, buttonHeightSmall, "Medium", darkGreen);
drawWideButton(405, 250, buttonWidthWide, buttonHeightSmall, "Fast", darkGreen);
drawWideButton(505, 250, buttonWidthWide, buttonHeightSmall, "Extreme", darkGreen);
// Number of Balls section with circular buttons (decreased size by 20%)
int ballsX = 200, ballsY = 340, ballsWidth = 400, ballsHeight = 120;
tft.fillRoundRect(ballsX, ballsY, ballsWidth, ballsHeight, 10, darkGreen);
tft.setTextColor(TFT_WHITE, darkGreen);
tft.drawString("Number of Balls", ballsX + ballsWidth / 2, ballsY + 20);
drawCircularButton(250, 410, buttonDiameterMedium * 0.8, "30", darkGreen); // 20% smaller
drawCircularButton(350, 410, buttonDiameterMedium * 0.8, "60", darkGreen); // 20% smaller
drawCircularButton(450, 410, buttonDiameterMedium * 0.8, "80", darkGreen); // 20% smaller
drawCircularButton(550, 410, buttonDiameterMedium * 0.8, "100", darkGreen); // 20% smaller
// Control section with Prime, Play, Stop buttons centered
int controlX = 620, controlY = 40, controlWidth = 160, controlHeight = 420;
tft.fillRoundRect(controlX, controlY, controlWidth, controlHeight, 20, darkGreen);
tft.setTextColor(TFT_WHITE, darkGreen);
tft.drawString("Control", controlX + controlWidth / 2, controlY + 20);
int controlButtonSpacing = (controlHeight - (buttonHeightBig * 3)) / 4;
drawControlButton(controlX + (controlWidth - buttonWidthBig) / 2, controlY + controlButtonSpacing + 20, buttonWidthBig, buttonHeightBig, "Prime", darkGreen);
drawControlButton(controlX + (controlWidth - buttonWidthBig) / 2, controlY + controlButtonSpacing * 2 + buttonHeightBig + 20, buttonWidthBig, buttonHeightBig, "Play", darkGreen);
drawControlButton(controlX + (controlWidth - buttonWidthBig) / 2, controlY + controlButtonSpacing * 3 + buttonHeightBig * 2 + 20, buttonWidthBig, buttonHeightBig, "Stop", darkGreen);
}
void loop() {
if (touchscreen.tirqTouched() && touchscreen.touched()) {
TS_Point p = touchscreen.getPoint();
handleTouch(p.x, p.y);
}
}
void drawRoundedButton(int x, int y, int w, int h, const char *label, uint16_t color) {
tft.fillRoundRect(x, y, w, h, 10, color);
tft.drawRoundRect(x, y, w, h, 10, TFT_WHITE);
tft.setTextColor(TFT_WHITE, color);
tft.setTextDatum(MC_DATUM);
tft.setTextSize(2);
tft.drawString(label, x + w / 2, y + h / 2);
}
void drawCircularButton(int x, int y, int diameter, const char *label, uint16_t color) {
tft.fillCircle(x, y, diameter / 2, color);
tft.drawCircle(x, y, diameter / 2, TFT_WHITE);
tft.setTextColor(TFT_WHITE, color);
tft.setTextDatum(MC_DATUM);
tft.setTextSize(2);
tft.drawString(label, x, y);
}
void drawWideButton(int x, int y, int w, int h, const char *label, uint16_t color) {
tft.fillRoundRect(x, y, w, h, 10, color);
tft.drawRoundRect(x, y, w, h, 10, TFT_WHITE);
tft.setTextColor(TFT_WHITE, color);
tft.setTextDatum(MC_DATUM);
tft.setTextSize(2);
tft.drawString(label, x + w / 2, y + h / 2);
}
void drawControlButton(int x, int y, int w, int h, const char *label, uint16_t color) {
tft.fillRoundRect(x, y, w, h, 10, color);
tft.drawRoundRect(x, y, w, h, 10, TFT_WHITE);
tft.setTextColor(TFT_WHITE, color);
tft.setTextDatum(MC_DATUM);
tft.setTextSize(2);
tft.drawString(label, x + w / 2, y + h / 2);
}
void handleTouch(int touchX, int touchY) {
// Touch detection code for each button
}