// ======================= Màn hình OLED 0.96inch (I2C) =======================
#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);
// ======================= Buttons =======================
const int Button_Up = 4;
const int Button_Down = 6;
const int Button_Enter = 5;
const int Button_Cancel = 7;
// ======================= Outputs =======================
const int Led_Start = 8;
const int Led_Stop = 9;
// ======================= Sensors =======================
const int Sensor_Back = 2;
const int Sensor_Front = 3;
// ======================= Relay =======================
const int Relay = 10;
// ======================= Variables =======================
int Cycles = 0;
bool Status = true;
bool Back_Sensor_Clicked = false;
bool Front_Sensor_Clicked = false;
bool Prev_Back_Sensor = HIGH;
bool Prev_Front_Sensor = HIGH;
// ======================= Setup =======================
void setup() {
Wire.begin();
Display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
Display.clearDisplay();
Display.display();
Serial.begin(9600);
pinMode(Button_Up, INPUT_PULLUP);
pinMode(Button_Down, INPUT_PULLUP);
pinMode(Button_Enter, INPUT_PULLUP);
pinMode(Button_Cancel, INPUT_PULLUP);
pinMode(Sensor_Back, INPUT_PULLUP);
pinMode(Sensor_Front, INPUT_PULLUP);
pinMode(Led_Start, OUTPUT);
pinMode(Led_Stop, OUTPUT);
pinMode(Relay, OUTPUT);
digitalWrite(Led_Start, LOW);
digitalWrite(Led_Stop, LOW);
digitalWrite(Relay, LOW);
}
// ======================= Loop =======================
void loop() {
bool Back_Sensor = digitalRead(Sensor_Back);
bool Front_Sensor = digitalRead(Sensor_Front);
// ========== XỬ LÝ CLICK CHO SENSOR BACK ==========
if (Back_Sensor == LOW) {
// Được tính là 1 "click"
if (!Back_Sensor_Clicked) {
Cycles++;
Back_Sensor_Clicked = true;
}
} else if (Back_Sensor == HIGH) {
// Khi nhả ra, cho phép click lần tiếp theo
Back_Sensor_Clicked = false;
}
// ========== XỬ LÝ CLICK CHO SENSOR FRONT ==========
if (Prev_Front_Sensor == HIGH && Front_Sensor == LOW) {
if (!Front_Sensor_Clicked) {
// Là một lần nhấn, bạn có thể xử lý gì đó nếu cần
Front_Sensor_Clicked = true;
}
} else if (Front_Sensor == HIGH) {
Front_Sensor_Clicked = false;
}
// Cập nhật trạng thái trước đó
Prev_Back_Sensor = Back_Sensor;
Prev_Front_Sensor = Front_Sensor;
// Gọi hiển thị
Setting(Cycles, Front_Sensor, Back_Sensor, Status);
delay(10); // Nhẹ chống dội
}
// ======================= Hiển thị Tab Start =======================
void Setting(int Cycles, bool Sensor_Front, bool Sensor_Back, bool Status) {
Display.clearDisplay();
// Hiển thị số cycles ở giữa màn hình
Display.setTextSize(3);
Display.setTextColor(SSD1306_WHITE);
String CycleText = String(Cycles);
int16_t X_Center = (Screen_Width - (CycleText.length() * 18)) / 2;
Display.setCursor(X_Center, 20);
Display.println(CycleText);
// Hiển thị Insertion hoặc Retraction bên trái dưới
Display.setTextSize(1);
Display.setCursor(0, 56);
if (Sensor_Front == LOW) {
Display.print(F("Insertion"));
} else if (Sensor_Back == LOW) {
Display.print(F("Retraction"));
}
// Hiển thị trạng thái bên phải dưới
Display.setCursor(84, 56);
if (Status) {
Display.print(F("Running"));
} else {
Display.print(F("Done"));
}
Display.display();
}