#include <Wire.h>
#include <U8g2lib.h>
// Khai báo màn hình OLED
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
// Khai báo Encoder
#define ENCODER_CLK 2 // Chân A (CLK)
#define ENCODER_DT 3 // Chân B (DT)
#define ENCODER_SW 5 // Chân nhấn (SW)
// Trạng thái menu
enum MenuState { MAIN_MENU, SET_ROUNDS, SET_SPEED };
MenuState currentMenu = MAIN_MENU;
// Biến giá trị menu
int rounds = 12; // Số vòng hiện tại
int maxRounds = 50;
int speed = 5; // Tốc độ hiện tại
int selectedItem = 0; // 0: Rounds, 1: Speed
// Biến trạng thái Encoder
int lastEncoderState = 0;
void setup() {
// Khởi tạo màn hình OLED
u8g2.begin();
// Cấu hình Encoder
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
pinMode(ENCODER_SW, INPUT_PULLUP);
lastEncoderState = digitalRead(ENCODER_CLK);
}
// Vẽ menu chính
void drawMainMenu() {
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_6x13_tf); // Font tiếng Anh
// Hiển thị số vòng
if (selectedItem == 0) u8g2.drawStr(0, 20, "> Rounds:");
else u8g2.drawStr(0, 20, " Rounds:");
char roundText[15];
sprintf(roundText, "%d/%d", rounds, maxRounds);
u8g2.drawStr(80, 20, roundText);
// Hiển thị tốc độ
if (selectedItem == 1) u8g2.drawStr(0, 40, "> Speed:");
else u8g2.drawStr(0, 40, " Speed:");
char speedText[5];
sprintf(speedText, "%d", speed);
u8g2.drawStr(80, 40, speedText);
u8g2.sendBuffer();
}
// Vẽ giao diện chỉnh số vòng
void drawSetRounds() {
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_6x13_tf);
u8g2.drawStr(10, 20, "Set Rounds:");
char roundText[10];
sprintf(roundText, "%d/%d", rounds, maxRounds);
u8g2.drawStr(50, 40, roundText);
u8g2.drawStr(10, 60, "Press to back");
u8g2.sendBuffer();
}
// Vẽ giao diện chỉnh tốc độ
void drawSetSpeed() {
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_6x13_tf);
u8g2.drawStr(10, 20, "Set Speed:");
char speedText[5];
sprintf(speedText, "%d", speed);
u8g2.drawStr(50, 40, speedText);
u8g2.drawStr(10, 60, "Press to back");
u8g2.sendBuffer();
}
// Xử lý điều hướng bằng Encoder
void handleEncoder() {
int currentState = digitalRead(ENCODER_CLK);
if (currentState != lastEncoderState) {
if (digitalRead(ENCODER_DT) != currentState) {
// Quay phải
if (currentMenu == MAIN_MENU) {
selectedItem = (selectedItem == 0) ? 1 : 0;
} else if (currentMenu == SET_ROUNDS && rounds < maxRounds) {
rounds++;
} else if (currentMenu == SET_SPEED && speed < 10) {
speed++;
}
} else {
// Quay trái
if (currentMenu == MAIN_MENU) {
selectedItem = (selectedItem == 0) ? 1 : 0;
} else if (currentMenu == SET_ROUNDS && rounds > 0) {
rounds--;
} else if (currentMenu == SET_SPEED && speed > 0) {
speed--;
}
}
lastEncoderState = currentState;
delay(50);
}
}
// Xử lý khi nhấn nút trên Encoder
void handleButton() {
if (digitalRead(ENCODER_SW) == LOW) {
if (currentMenu == MAIN_MENU) {
if (selectedItem == 0) currentMenu = SET_ROUNDS;
else currentMenu = SET_SPEED;
} else {
currentMenu = MAIN_MENU;
}
delay(200); // Tránh bấm nhầm nhiều lần
}
}
void loop() {
handleEncoder();
handleButton();
if (currentMenu == MAIN_MENU) drawMainMenu();
else if (currentMenu == SET_ROUNDS) drawSetRounds();
else if (currentMenu == SET_SPEED) drawSetSpeed();
}