반응형
알리에서 부품 조달.
ESP32 관련 부품을 샀다. 이번에는 6일 만에 배송이 왔다. 엄청 빠르네 요새. ESP32 칩 개발용 보드인데, 개발보드를 직접 드론에 싣는 것보다 ESP32 칩만 온보드 하는 게 좋을 것 같
fermium.tistory.com
요때 산 T-Display ESP32로 뽀모도로 타이머를 만들면 좋지 않을까 생각했는데,
챗GPT에 물어보니 순식간에 만들어준다.
#include <TFT_eSPI.h>
#include <SPI.h>
// =================== 설정 ===================
#define WORK_MINUTES 25
#define BREAK_MINUTES 5
#define BUTTON_START 0 // 왼쪽 버튼 (시작/일시정지)
#define BUTTON_RESET 35 // 오른쪽 버튼 (리셋)
// ================== 전역 변수 ==================
TFT_eSPI tft = TFT_eSPI();
unsigned long previousMillis = 0;
int totalSeconds = WORK_MINUTES * 60;
int remainingSeconds = totalSeconds;
bool isRunning = false;
bool isWorkMode = true;
void setup() {
pinMode(BUTTON_START, INPUT_PULLUP);
pinMode(BUTTON_RESET, INPUT);
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
tft.setTextDatum(MC_DATUM);
drawScreen();
}
void loop() {
static bool lastStartBtn = HIGH;
static bool lastResetBtn = HIGH;
bool currentStartBtn = digitalRead(BUTTON_START);
bool currentResetBtn = digitalRead(BUTTON_RESET);
// ===== START/PAUSE 버튼 처리 =====
if (lastStartBtn == HIGH && currentStartBtn == LOW) {
isRunning = !isRunning;
}
lastStartBtn = currentStartBtn;
// ===== RESET 버튼 처리 =====
if (lastResetBtn == HIGH && currentResetBtn == LOW) {
resetTimer();
drawScreen();
}
lastResetBtn = currentResetBtn;
// ===== 타이머 작동 =====
if (isRunning) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 1000) {
previousMillis = currentMillis;
remainingSeconds--;
if (remainingSeconds <= 0) {
isWorkMode = !isWorkMode;
totalSeconds = (isWorkMode ? WORK_MINUTES : BREAK_MINUTES) * 60;
remainingSeconds = totalSeconds;
}
drawScreen();
}
}
}
// ================== 리셋 함수 ==================
void resetTimer() {
isRunning = false;
isWorkMode = true;
totalSeconds = WORK_MINUTES * 60;
remainingSeconds = totalSeconds;
}
// ================== 화면 그리기 ==================
void drawScreen() {
tft.fillScreen(isWorkMode ? TFT_DARKGREEN : TFT_NAVY);
drawIcon();
drawTime();
drawProgressBar();
}
// 아이콘 (집중 / 휴식)
void drawIcon() {
tft.setTextColor(TFT_WHITE, isWorkMode ? TFT_DARKGREEN : TFT_NAVY);
tft.setTextSize(3);
tft.setCursor(40, 10);
if (isWorkMode) {
tft.print("Focus");
} else {
tft.print("Break");
}
}
// 남은 시간 출력
void drawTime() {
int minutes = remainingSeconds / 60;
int seconds = remainingSeconds % 60;
char buffer[6];
sprintf(buffer, "%02d:%02d", minutes, seconds);
tft.setTextColor(TFT_YELLOW, isWorkMode ? TFT_DARKGREEN : TFT_NAVY);
tft.setTextSize(6);
tft.setCursor(50, 50);
tft.print(buffer);
}
// 진행 바
void drawProgressBar() {
float progress = 1.0 - (float)remainingSeconds / totalSeconds;
int barWidth = 200;
int filled = progress * barWidth;
int barX = 20;
int barY = 130;
int barHeight = 10;
tft.drawRect(barX, barY, barWidth, barHeight, TFT_WHITE);
tft.fillRect(barX + 1, barY + 1, filled - 2, barHeight - 2, TFT_ORANGE);
}
나는 토씨 하나도 건드린 게 없다.
이걸 빌드에서 업로드해 주니까 놀랍도록 실행이 잘 된다.
25분 집중하고 5분 쉬는 타이밍을 재 주는 어플로,
윗 버튼이 리셋이고, 아랫버튼이 일시정지 버튼으로 활용하고 있다.
집중시간에서 휴식시간으로 잘 바뀐다.
LiPo 배터리를 구매하면 휴대용으로도 사용할 수 있을 것 같다.
반응형