#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Chân nút
#define BTN_MODE 2 // Chuyển chế độ
#define BTN_ACTION 3 // Ghi nhớ hoặc kiểm tra
int mode = 0; // 0: Training | 1: Detection
String modeText[2] = {"Training", "Detection"};
float voc_Ri6 = 0;
float voc_Monthong = 0;
float voc_Khohoaxanh = 0;
int trainingStep = 0; // 0: Ri6, 1: Monthong, 2: Khohoaxanh
// Tạo VOC ngẫu nhiên trong ngưỡng để mô phỏng
float getSimulatedVOC(String type = "") {
if (type == "Ri6") return random(300, 330); // Thơm nhẹ
if (type == "Monthong") return random(400, 440); // Thơm ngát
if (type == "Khohoaxanh") return random(500, 550); // Thơm nồng
return random(300, 550); // Mùi bất kỳ khi kiểm tra
}
void showOLED(String line1, String line2 = "", String line3 = "") {
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.println("MODE: " + modeText[mode]);
display.println(line1);
if (line2 != "") display.println(line2);
if (line3 != "") display.println(line3);
display.display();
}
void setup() {
Serial.begin(9600);
pinMode(BTN_MODE, INPUT_PULLUP);
pinMode(BTN_ACTION, INPUT_PULLUP);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("OLED lỗi!"));
while (true);
}
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
showOLED("He thong khoi dong...");
delay(1000);
showOLED("Nhan MODE de doi che do");
}
void loop() {
static bool lastBtnMode = HIGH;
static bool lastBtnAction = HIGH;
bool btnMode = digitalRead(BTN_MODE);
bool btnAction = digitalRead(BTN_ACTION);
// Chuyển chế độ
if (lastBtnMode == HIGH && btnMode == LOW) {
mode = 1 - mode;
trainingStep = 0;
showOLED("Chuyen sang che do", modeText[mode]);
delay(1000);
}
lastBtnMode = btnMode;
// Thao tác chính: ghi hoặc kiểm
if (lastBtnAction == HIGH && btnAction == LOW) {
if (mode == 0) {
// Ghi nhớ VOC từng loại
String fruitName;
float voc = 0;
if (trainingStep == 0) {
fruitName = "Ri6";
voc = getSimulatedVOC("Ri6");
voc_Ri6 = voc;
} else if (trainingStep == 1) {
fruitName = "Monthong";
voc = getSimulatedVOC("Monthong");
voc_Monthong = voc;
} else if (trainingStep == 2) {
fruitName = "Khohoaxanh";
voc = getSimulatedVOC("Khohoaxanh");
voc_Khohoaxanh = voc;
}
showOLED("Da ghi: " + fruitName, "VOC: " + String(voc, 1));
Serial.println("Ghi " + fruitName + ": " + String(voc));
trainingStep++;
if (trainingStep > 2) {
trainingStep = 0;
showOLED("Hoan tat training!");
}
} else {
// Kiểm tra
float voc = getSimulatedVOC();
String ketqua = "Khong xac dinh";
if (abs(voc - voc_Ri6) / voc_Ri6 < 0.1) ketqua = "Ri6";
else if (abs(voc - voc_Monthong) / voc_Monthong < 0.1) ketqua = "Monthong";
else if (abs(voc - voc_Khohoaxanh) / voc_Khohoaxanh < 0.1) ketqua = "Khohoa xanh";
showOLED("VOC: " + String(voc, 1), "Ket qua: " + ketqua);
Serial.println("VOC: " + String(voc) + " => " + ketqua);
}
delay(500);
}
lastBtnAction = btnAction;
}