#include <Adafruit_GFX.h> // Core graphics library
#include <SPI.h> // Needed for display
#include <Adafruit_ILI9341.h>
#include <Wire.h> // Needed for I2C
#include <Adafruit_FT6206.h>
// Define pins for the TFT
#define TFT_DC 2
#define TFT_CS 15
#define TFT_RST -1 // You can set this to the pin you're using for reset, or -1 if you are not using a reset pin
// Initialize Adafruit ILI9341 TFT
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// Initialize the FT6206 capacitive touch screen
Adafruit_FT6206 ctp = Adafruit_FT6206();
// Define LED pin
#define LED_PIN 6
int timerValue = 0; // Initial timer value in seconds
bool counting = false; // Flag to indicate if the timer is counting down
bool hiddenMenu = false; // Flag to indicate if the hidden menu is enabled
// PWM value (example)
int pwmValue = 50; // Initial PWM value, assuming 50%
// Define the buttons
#define BUTTON_WIDTH 80
#define BUTTON_HEIGHT 40
#define BUTTON_PADDING 20
// Button coordinates
int buttonPlusX, buttonPlusY;
int buttonMinusX, buttonMinusY;
int buttonStartX, buttonStartY;
// Function prototypes
void drawButton(int x, int y, int w, int h, uint16_t color, const char *label);
void displayTimer();
bool isButtonPressed(TS_Point p, int x, int y, int w, int h);
void startCountdown();
void showHiddenMenu();
void adjustPWMBy(int increment);
void displayPWMValue();
void turnOnLED();
void turnOffLED();
void setup(void) {
Serial.begin(115200);
Serial.println(F("Cap Touch Timer!"));
Wire.setPins(10, 8); // Redefine first I2C port to be on pins 10/8
tft.begin();
if (!ctp.begin(40)) { // Pass in 'sensitivity' coefficient
Serial.println("Couldn't start FT6206 touchscreen controller");
while (1);
}
Serial.println("Capacitive touchscreen started");
// Set LED pin as output
pinMode(LED_PIN, OUTPUT);
// Display initial screen with "Bonjour" for 5 seconds
tft.fillScreen(ILI9341_WHITE);
tft.setCursor(10, 10);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
tft.println("Bonjour");
// Check for hidden menu activation during "Bonjour" display
unsigned long startMillis = millis();
bool longPressDetected = false;
while (millis() - startMillis < 5000) { // Display "Bonjour" for 5 seconds
if (ctp.touched()) {
TS_Point p = ctp.getPoint();
p.x = map(p.x, 0, 240, 240, 0);
p.y = map(p.y, 0, 320, 320, 0);
// Check if touch is within the hidden menu activation area
if (p.x > 200 && p.y < 50) {
unsigned long pressStart = millis();
while (ctp.touched()) {
if (millis() - pressStart > 2000) { // Long press detected
longPressDetected = true;
break;
}
}
break; // Exit the loop after detecting a long press or end of touch
}
}
}
if (longPressDetected) {
hiddenMenu = true;
showHiddenMenu();
} else {
// Draw the regular buttons
buttonMinusX = BUTTON_PADDING;
buttonMinusY = tft.height() - BUTTON_HEIGHT - BUTTON_PADDING;
drawButton(buttonMinusX, buttonMinusY, BUTTON_WIDTH, BUTTON_HEIGHT, ILI9341_RED, "-");
buttonPlusX = buttonMinusX + BUTTON_WIDTH + BUTTON_PADDING;
buttonPlusY = tft.height() - BUTTON_HEIGHT - BUTTON_PADDING;
drawButton(buttonPlusX, buttonPlusY, BUTTON_WIDTH, BUTTON_HEIGHT, ILI9341_GREEN, "+");
buttonStartX = tft.width() - BUTTON_WIDTH - BUTTON_PADDING;
buttonStartY = tft.height() / 2 - BUTTON_HEIGHT / 2;
drawButton(buttonStartX, buttonStartY, BUTTON_WIDTH, BUTTON_HEIGHT, ILI9341_BLUE, "Start");
// Initial display of the timer
displayTimer();
}
}
void loop() {
delay(10);
if (!ctp.touched()) {
return;
}
TS_Point p = ctp.getPoint();
p.x = map(p.x, 0, 240, 240, 0);
p.y = map(p.y, 0, 320, 320, 0);
Serial.print("("); Serial.print(p.x);
Serial.print(", "); Serial.print(p.y);
Serial.println(")");
if (hiddenMenu) {
// Handle hidden menu interactions here if needed
if (isButtonPressed(p, buttonPlusX, buttonPlusY, BUTTON_WIDTH, BUTTON_HEIGHT)) {
adjustPWMBy(10);
} else if (isButtonPressed(p, buttonMinusX, buttonMinusY, BUTTON_WIDTH, BUTTON_HEIGHT)) {
adjustPWMBy(-10);
}
return;
}
if (isButtonPressed(p, buttonPlusX, buttonPlusY, BUTTON_WIDTH, BUTTON_HEIGHT)) {
if (!counting) {
timerValue++;
displayTimer();
}
} else if (isButtonPressed(p, buttonMinusX, buttonMinusY, BUTTON_WIDTH, BUTTON_HEIGHT)) {
if (!counting && timerValue > 0) {
timerValue--;
displayTimer();
}
} else if (isButtonPressed(p, buttonStartX, buttonStartY, BUTTON_WIDTH, BUTTON_HEIGHT)) {
if (!counting) {
counting = true;
turnOnLED(); // Turn on LED when countdown starts
startCountdown();
}
}
}
// Function to draw a button
void drawButton(int x, int y, int w, int h, uint16_t color, const char *label) {
tft.fillRect(x, y, w, h, color);
tft.drawRect(x, y, w, h, ILI9341_BLACK);
tft.setCursor(x + (w / 2) - (strlen(label) * 3 * 2), y + (h / 2) - 8);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print(label);
}
// Function to display the timer
void displayTimer() {
tft.fillRect(0, 0, tft.width(), 50, ILI9341_WHITE);
tft.setCursor(10, 10);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(3);
tft.print("Timer: ");
tft.print(timerValue);
tft.print(" s");
}
// Function to check if a button is pressed
bool isButtonPressed(TS_Point p, int x, int y, int w, int h) {
return (p.x > x && p.x < (x + w) && p.y > y && p.y < (y + h));
}
// Function to start the countdown
void startCountdown() {
while (timerValue > 0 && counting) {
delay(1000);
timerValue--;
displayTimer();
}
counting = false;
turnOffLED(); // Turn off LED when countdown ends
}
// Function to turn on the LED
void turnOnLED() {
digitalWrite(LED_PIN, HIGH);
}
// Function to turn off the LED
void turnOffLED() {
digitalWrite(LED_PIN, LOW);
}
// Function to show the hidden menu
void showHiddenMenu() {
tft.fillScreen(ILI9341_WHITE);
tft.setCursor(10, 50); // Adjust the coordinates as needed
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
tft.println("Menu PWM");
// Draw buttons to adjust PWM
buttonPlusX = 10;
buttonPlusY = 100;
drawButton(buttonPlusX, buttonPlusY, BUTTON_WIDTH, BUTTON_HEIGHT, ILI9341_GREEN, "+10%");
buttonMinusX = buttonPlusX + BUTTON_WIDTH + BUTTON_PADDING;
buttonMinusY = buttonPlusY;
drawButton(buttonMinusX, buttonMinusY, BUTTON_WIDTH, BUTTON_HEIGHT, ILI9341_RED, "-10%");
// Display current PWM value
displayPWMValue();
}
// Function to adjust PWM value by increment
void adjustPWMBy(int increment) {
// Adjust PWM value (example)
pwmValue += increment;
if (pwmValue < 0) {
pwmValue = 0;
} else if (pwmValue > 100) {
pwmValue = 100;
}
// Update PWM output or perform other actions as needed
// Example: analogWrite(PWM_PIN, map(pwmValue, 0, 100, 0, 255));
// Display updated PWM value
displayPWMValue();
}
// Function to display PWM value
void displayPWMValue() {
tft.fillRect(10, 150, tft.width() - 20, 20, ILI9341_WHITE); // Clear previous value
tft.setCursor(10, 150); // Adjust coordinates as needed
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
tft.print("PWM Value: ");
tft.print(pwmValue);
tft.print("%");
}