#include<Arduino.h> // this is needed for FT6206
#include<U8g2_for_Adafruit_GFX.h>
#include <Wire.h>
#include<Adafruit_ILI9341.h>
#include<Adafruit_FT6206.h>
#include<SPI.h>
// // The display also uses hardware SPI, plus #9 & #10
/*#define TFT_CS 10
#define TFT_DC 9*/
#define TFT_DC 2
#define TFT_CS 15
#define TFT_RST 4
//Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC, TFT_RST);
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
Adafruit_FT6206 ts = Adafruit_FT6206();
int hour = 12; // Initial hour
int minute = 30; // Initial minute
void setup() {
Serial.begin(115200);
// Initialize TFT
tft.begin();
//tft.setRotation(1);
// Initialize Touchscreen
if (!ts.begin(40)) { // Sensitivity threshold
Serial.println("Touchscreen not found!");
while (1);
}
drawUI(); // Initial display of UI
}
void loop() {
//if (ts.touched()) {
TS_Point p = ts.getPoint();
// Map touch coordinates to screen dimensions
p.x = map(p.x, 0, 240, 0, tft.width());
p.y = map(p.y, 0, 320, 0, tft.height());
// Debug: Print touch coordinates to Serial Monitor
Serial.print("Touch X: "); Serial.print(p.x);
Serial.print(" | Touch Y: "); Serial.println(p.y);
// Handle touch
handleTouch(p.x, p.y);
//}
}
// Draw the initial UI for hour and minute
void drawUI() {
tft.fillScreen(ILI9341_BLACK);
// Display "Hour + Minute" label
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(20, 40);
tft.print("Hour + Minute");
// Draw + and - buttons for hour
drawButton(20, 80, "+ Hour", ILI9341_GREEN);
drawButton(20, 160, "- Hour", ILI9341_RED);
// Draw + and - buttons for minute
drawButton(150, 80, "+ Min", ILI9341_GREEN);
drawButton(150, 160, "- Min", ILI9341_RED);
// Show current time values
updateTimeDisplay();
}
// Draw buttons for + and -
void drawButton(int x, int y, const char* label, uint16_t color) {
tft.fillRoundRect(x, y, 100, 60, 10, color); // Button background with rounded corners
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(x + 20, y + 15); // Adjust cursor position for centering
tft.print(label);
}
// Update the display for hour and minute
void updateTimeDisplay() {
// Clear previous time values
tft.fillRect(100, 240, 120, 40, ILI9341_BLACK); // Clear time display area
tft.setTextColor(ILI9341_CYAN); // Change text color for current time display
tft.setTextSize(3);
// Display current hour
tft.setCursor(100, 240);
tft.print(hour);
// Display colon
tft.setCursor(140, 240);
tft.print(":");
// Display current minute
tft.setCursor(160, 240);
if (minute < 10) {
tft.print("0"); // Add leading zero if needed
}
tft.print(minute);
}
// Handle touch input and adjust hour and minute
void handleTouch(int x, int y) {
// Increase hour when + Hour is pressed
if (x > 20 && x < 120 && y > 80 && y < 140) {
incrementHour();
updateTimeDisplay();
drawUI(); // Redraw the UI after incrementing hour
}
// Decrease hour when - Hour is pressed
if (x > 20 && x < 120 && y > 160 && y < 220) {
decrementHour();
updateTimeDisplay();
drawUI(); // Redraw the UI after decrementing hour
}
// Increase minute when + Min is pressed
if (x > 150 && x < 250 && y > 80 && y < 140) {
incrementMinute();
updateTimeDisplay();
drawUI(); // Redraw the UI after incrementing minute
}
// Decrease minute when - Min is pressed
if (x > 150 && x < 250 && y > 160 && y < 220) {
decrementMinute();
updateTimeDisplay();
drawUI(); // Redraw the UI after decrementing minute
}
}
// Function to increment hour
void incrementHour() {
hour++;
if (hour >= 24) {
hour = 0; // Reset to 0 after 23
}
}
// Function to decrement hour
void decrementHour() {
if (hour == 0) {
hour = 23; // Reset to 23 if going below 0
} else {
hour--;
}
}
// Function to increment minute
void incrementMinute() {
minute++;
if (minute >= 60) {
minute = 0; // Reset to 0 after 59
incrementHour(); // Increment hour if minute resets
}
}
// Function to decrement minute
void decrementMinute() {
if (minute == 0) {
minute = 59; // Reset to 59 if going below 0
decrementHour(); // Decrement hour if minute resets
} else {
minute--;
}
}
Loading
esp32-s3-devkitc-1
esp32-s3-devkitc-1
Loading
ili9341-cap-touch
ili9341-cap-touch