//TFT-1 顯示文字
/*
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8
Adafruit_ILI9341 tft(TFT_CS, TFT_DC, TFT_RST);
void setup() {
tft.begin();
tft.setRotation(3);
tft.fillScreen(ILI9341_BLACK);
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
int textWidth = 15 * 6 * 3; // 15字元x6像素xsize3
int textX = (320 - textWidth) / 2;
int textY = (240 - 24) / 2;
tft.setCursor(textX, textY);
tft.println("Hello ILI9341!");
}
void loop() {
// 無需重複執行
}
*/
//TFT-2 初始畫面顯示一顆按鈕觸發進入主畫面
/*
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8
#define BTN_START 2 // 「START」按鈕
Adafruit_ILI9341 tft(TFT_CS, TFT_DC, TFT_RST);
// -------- 防彈跳參數 --------
bool lastBtn = HIGH;
const unsigned long debounceDelay = 50;
unsigned long lastChange = 0;
void setup() {
pinMode(BTN_START, INPUT_PULLUP);
tft.begin();
tft.setRotation(3);
splashScreen(); // 顯示「Press START」畫面
}
void loop() {
// 等待按鈕被按下
if (buttonPressed()) {
showMainPage(); // 進入主畫面
while (1); // 停在此,不再刷新
}
}
// ---------- 畫面函式 ----------
void splashScreen() {
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_YELLOW);
tft.setTextSize(2);
const char* msg1 = "ILI9341 DEMO";
const char* msg2 = "Press START";
centerPrint(msg1, 80);
centerPrint(msg2, 120);
}
void showMainPage() {
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(3);
centerPrint("Hello ILI9341!", 100);
}
// ---------- 工具函式 ----------
bool buttonPressed() {
bool reading = digitalRead(BTN_START);
if (reading != lastBtn) {
lastChange = millis();
lastBtn = reading;
}
if ((millis() - lastChange) > debounceDelay) {
// 按下 (LOW) 才回傳 true
if (reading == LOW) return true;
}
return false;
}
// 文字水平置中輸出
void centerPrint(const char* txt, int y) {
int16_t x1, y1;
uint16_t w, h;
tft.getTextBounds((char*)txt, 0, 0, &x1, &y1, &w, &h);
int x = (320 - w) / 2; // 320 = 寬度
tft.setCursor(x, y);
tft.println(txt);
}
*/
//TFT-3
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
/* ── TFT SPI 腳位 ───────────────── */
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8
/* ── START 按鈕 ─────────────────── */
#define BTN_START 2 // 一腳接 D2,另一腳接 GND(內建上拉)
/* ── 物件實體 ───────────────────── */
Adafruit_ILI9341 tft(TFT_CS, TFT_DC, TFT_RST);
/* ── 顏色表(16-bit 565 常數)────── */
const uint16_t colors[] = {
ILI9341_RED,
ILI9341_GREEN,
ILI9341_BLUE,
ILI9341_BLACK
};
const char* colorNames[] = {
"RED",
"GREEN",
"BLUE",
"BLACK"
};
const uint8_t COLOR_COUNT = sizeof(colors) / sizeof(colors[0]);
/* ── 防彈跳變數 ─────────────────── */
bool lastBtnState = HIGH;
const unsigned long debounceDelay = 50;
unsigned long lastChange = 0;
/* ── 當前顏色索引 ───────────────── */
int8_t colorIndex = -1; // 開機顯示空白,按第一次變 0
/* ──────────────────────────────── */
void setup() {
pinMode(BTN_START, INPUT_PULLUP);
tft.begin();
tft.setRotation(3); // 橫向
tft.fillScreen(ILI9341_BLACK); // 先清屏
tft.setTextSize(3);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
centerPrint("Press START", 110); // 初始提示
}
/* ──────────────────────────────── */
void loop() {
bool reading = digitalRead(BTN_START);
// 如果按鈕狀態變化,更新時間戳
if (reading != lastBtnState) {
lastChange = millis();
lastBtnState = reading;
}
// 穩定超過 debounceDelay 且按下時(LOW)
if ((millis() - lastChange) > debounceDelay && reading == LOW) {
cycleColor(); // 切換顏色
while (digitalRead(BTN_START) == LOW); // 等按鍵放開,避免重複觸發
}
}
/* ---------- 功能函式 ---------- */
void cycleColor() {
colorIndex = (colorIndex + 1) % COLOR_COUNT;
tft.fillScreen(colors[colorIndex]);
// 文字顏色遇黑底改成白,其餘改黑字
uint16_t txtColor = (colors[colorIndex] == ILI9341_BLACK) ? ILI9341_WHITE : ILI9341_BLACK;
tft.setTextColor(txtColor, colors[colorIndex]);
centerPrint(colorNames[colorIndex], 110);
}
void centerPrint(const char* txt, int y) {
int16_t x1, y1;
uint16_t w, h;
tft.getTextBounds((char*)txt, 0, 0, &x1, &y1, &w, &h);
int x = (320 - w) / 2;
tft.setCursor(x, y);
tft.println(txt);
}