Good day, please help me for my school project. I am trying to make a simple user interface screen using 3.2" TFT LCD ILI9341 with arduino mega 2560 and I have written a code, although there seems to be a problem on the case 5 (or page 5) because the text I wrote in the code is not displaying on the screen of the LCD even when I click the "IBUPROFEN" button which should change the page to page 5.
#include "SPI.h"
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <XPT2046_Touchscreen.h>
#include <Math.h>
#define TFT_CS 45
#define TFT_DC 43
#define TFT_RST 41
#define TS_CS 44
#define ILI9341_BLACK 0x0000
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
XPT2046_Touchscreen ts(TS_CS);
int currentPage = 1;
const int totalPages = 7;
int coinsInserted = 0;
// Button dimensions and position
int buttonX = 50;
int buttonY = 100;
int buttonWidth = 200;
int buttonHeight = 40;
void drawPage(int page);
void drawButton1(String buttonText);
void drawButton2(String buttonText);
void drawButton3(String buttonText);
void drawButton4(String buttonText);
void drawButton5(String buttonText);
void drawButton6(String buttonText);
void setup() {
Serial.begin(9600);
pinMode(TS_CS, OUTPUT);
digitalWrite(TS_CS, HIGH);
pinMode(TFT_CS, OUTPUT);
digitalWrite(TFT_CS, HIGH);
tft.begin();
tft.setRotation(3);
tft.fillScreen(ILI9341_BLACK);
ts.begin();
ts.setRotation(3);
drawPage(currentPage);
}
void loop() {
if (ts.touched()) {
TS_Point p = ts.getPoint();
int16_t x = p.x;
int16_t y = p.y;
Serial.print("x = ");
Serial.print(x);
Serial.print(", y = ");
Serial.println(y);
if (currentPage == 1 && x >= 900 && x <= 3300 && y >= 1800 && y <= 2400) {
currentPage = 2; // Change to page 2
drawPage(currentPage);
delay(200); // Add a delay to debounce the touch input
} else if (currentPage == 2 && x >= 1300 && x <= 3000 && y >= 1800 && y <= 2500) {
currentPage = 3; // Change to page 3
drawPage(currentPage);
delay(200); // Add a delay to debounce the touch input
} else if (currentPage == 3 && x >= 200 && x <= 1200 && y >= 500 && y <= 1000) {
currentPage = 2; // Go back to page 2
drawPage(currentPage);
delay(200); // Add a delay to debounce the touch input
} else if (currentPage == 3 && x >= 2500 && x <= 3600 && y >= 500 && y <= 1200) {
currentPage = 4; // Change to page 4
drawPage(currentPage);
delay(200); // Add a delay to debounce the touch input
} else if (currentPage == 2 && x >= 1100 && x <= 2800 && y >= 1000 && y <= 1500) {
currentPage = 5; // Change to page 5
drawPage(currentPage);
delay(200); // Add a delay to debounce the touch input
} else if (currentPage == 1 && x >= 1000 && x <= 3000 && y >= 1000 && y <= 1500) {
currentPage = 7; // Change to page 7
drawPage(currentPage);
delay(200); // Add a delay to debounce the touch input
}
else if (currentPage == 4 && x >= 10 && x <= 210 && y >= 200 && y <= 240) {
coinsInserted++; // Increase number of coins inserted
if (coinsInserted == 3) {
currentPage = 1; // Go back to page 1
coinsInserted = 0; // Reset the coins inserted count
drawPage(currentPage);
delay(200); // Add a delay to debounce the touch input
}
}
}
}
void drawPage(int page) {
Serial.print("Drawing page ");
Serial.println(page);
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(tft.width() / 2 - 75, 20); // Center the text
tft.setTextSize(3);
tft.setTextColor(ILI9341_WHITE);
switch (page) {
case 1:
tft.println("SYMPTOMS");
drawButton1("HEADACHE");
drawButton2("STOMACH PAIN");
break;
case 2:
tft.println("HEADACHE");
drawButton3("BIOGESIC");
drawButton4("IBUPROFEN");
drawButton6("BACK");
break;
case 3:
tft.setTextSize(2);
tft.println("BIOGESIC");
tft.println("Price: 3 PHP");
tft.println("Biogesic (Paracetamol) is used for mild to moderate pain and fever. Safe on an empty stomach.");
tft.println("Dosage: 1-2 500mg tablets every 4-6 hours for adults and children over 12.");
drawButton5("BUY");
drawButton6("BACK");
break;
case 4:
tft.setTextSize(2);
tft.println("INSERT 3 COINS");
int textWidth = tft.width() / 2 - (strlen("COINS INSERTED:") * 6) / 2;
tft.setCursor(80, 100); // Center the text
tft.println("COINS INSERTED:");
tft.setCursor(150, 150); // Center the text
tft.println(coinsInserted); // Print the number of coins inserted
break;
case 5:
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.setRotation(3);
tft.setCursor(50, 50);
tft.println("IBUPROFEN");
tft.println("Price: 5 PHP");
tft.println("Advil (Ibuprofen) reduces inflammation and pain in the body.");
tft.println("For fever, headaches, toothaches, back pain, arthritis, menstrual cramps, and minor injuries.");
drawButton5("BUY");
drawButton6("BACK");
break;
}
}
// headache
void drawButton1(String buttonText) {
tft.fillRect(buttonX, buttonY, buttonWidth, buttonHeight, ILI9341_WHITE);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
int textWidth = buttonText.length() * 6;
tft.setCursor(buttonX + (buttonWidth - textWidth) / 2.5, buttonY + (buttonHeight - 16) / 2);
tft.println(buttonText);
}
//stomach
void drawButton2(String buttonText) {
int button2X = buttonX;
int button2Y = buttonY + buttonHeight + 10;
int button2Width = buttonWidth;
int button2Height = buttonHeight;
tft.fillRect(button2X, button2Y, button2Width, button2Height, ILI9341_WHITE);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
int textWidth = buttonText.length() * 6;
tft.setCursor(button2X + (button2Width - textWidth) / 4, button2Y + (button2Height - 16) / 2);
tft.println(buttonText);
}
//biogesic
void drawButton3(String buttonText) {
int button3X = buttonX;
int button3Y = tft.height() / 2 - buttonHeight / 2; // Center vertically
int button3Width = buttonWidth;
int button3Height = buttonHeight;
tft.fillRect(button3X, button3Y, button3Width, button3Height, ILI9341_WHITE);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
int textWidth = buttonText.length() * 6;
tft.setCursor(button3X + (button3Width - textWidth) / 2.5, button3Y + (button3Height - 16) / 2);
tft.println(buttonText);
}
//ibuprofen
void drawButton4(String buttonText) {
int button4X = buttonX;
int button4Y = buttonY + buttonHeight + 10; // Position above the bottom button
int button4Width = buttonWidth;
int button4Height = buttonHeight;
tft.fillRect(button4X, button4Y, button4Width, button4Height, ILI9341_WHITE);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
int textWidth = buttonText.length() * 6;
tft.setCursor(button4X + (button4Width - textWidth) / 2.7, button4Y + (button4Height - 16) / 2);
tft.println(buttonText);
}
//buy
void drawButton5(String buttonText) {
int button5X = 10; // Position from the left edge
int button5Y = tft.height() - buttonHeight - 10; // Position above the bottom with a small margin
int button5Width = buttonWidth / 3; // Make the button one-third the width
int button5Height = buttonHeight;
tft.fillRect(button5X, button5Y, button5Width, button5Height, ILI9341_WHITE);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
int textWidth = buttonText.length() * 6;
tft.setCursor(button5X + (button5Width - textWidth) / 2.5, button5Y + (button5Height - 16) / 2);
tft.println(buttonText);
}
//back
void drawButton6(String buttonText) {
int button6X = tft.width() - buttonWidth / 3 - 10; // Position from the right edge
int button6Y = tft.height() - buttonHeight - 10; // Position above the bottom with a small margin
int button6Width = buttonWidth / 3; // Make the button one-third the width
int button6Height = buttonHeight;
tft.fillRect(button6X, button6Y, button6Width, button6Height, ILI9341_WHITE);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
int textWidth = buttonText.length() * 6;
tft.setCursor(button6X + (button6Width - textWidth) / 2.5, button6Y + (button6Height - 16) / 2);
tft.println(buttonText);
}