#include <LiquidCrystal.h>
#include <HX711_ADC.h>
// تعريف الـ LCD والأزرار
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
const int HX711_dout = 2; //mcu > HX711 dout pin
const int HX711_sck = 3; //mcu > HX711 sck pin
HX711_ADC LoadCell(HX711_dout, HX711_sck);
int buttons[] = {A1, A2, A0, 10};
enum {UP, DOWN, ENTER, BACK};
char* menu[] = {"1-TARGET WEIGHT", "2-CALIBRATION", "3-REST", "4-hen"};
// تعريف المتغيرات
int targetWeightTens = 0;
int targetWeightOnes = 0;
int targetWeightDecimals = 0;
int calibrationTens = 0;
int calibrationOnes = 0;
int calibrationDecimals = 0;
int currentAdjusting = 0; // 0 = tens, 1 = ones, 2 = decimals
bool inTargetWeightMenu = false;
bool inCalibrationMenu = false;
bool inMainMenu = true;
int menuIndex = 0;
float currentWeight = 0.0; // متغير لتخزين الوزن الحالي
void setup() {
lcd.begin(16, 2);
LoadCell.begin();
for (int i = 0; i < 4; i++)
pinMode(buttons[i], INPUT_PULLUP);
showHomeScreen(); // عرض الشاشة الرئيسية عند بدء التشغيل
}
void loop() {
currentWeight = LoadCell.getData(); // قراءة الوزن الحالي
if (inMainMenu) {
if (!digitalRead(buttons[ENTER])) {
inMainMenu = false;
showMainMenu();
delay(200);
}
} else {
if (!digitalRead(buttons[UP])) {
if (inTargetWeightMenu || inCalibrationMenu) {
adjustValues(true);
} else {
menuIndex = (menuIndex - 1 + 4) % 4;
updateDisplay();
}
delay(200);
} else if (!digitalRead(buttons[DOWN])) {
if (inTargetWeightMenu || inCalibrationMenu) {
adjustValues(false);
} else {
menuIndex = (menuIndex + 1) % 4;
updateDisplay();
}
delay(200);
} else if (!digitalRead(buttons[ENTER])) {
if (inTargetWeightMenu || inCalibrationMenu) {
currentAdjusting = (currentAdjusting + 1) % 3;
updateDisplay();
} else {
switch (menuIndex) {
case 0:
inTargetWeightMenu = true;
break;
case 1:
inCalibrationMenu = true;
break;
// إضافة الحالات الأخرى حسب الحاجة
}
currentAdjusting = 0;
updateDisplay();
}
delay(200);
} else if (!digitalRead(buttons[BACK])) {
if (inTargetWeightMenu || inCalibrationMenu) {
inTargetWeightMenu = false;
inCalibrationMenu = false;
updateDisplay();
} else {
// إذا كان المستخدم في القائمة الرئيسية وضغط على زر العودة
inMainMenu = true;
showHomeScreen();
}
delay(200);
}
}
}
void adjustValues(bool increase) {
int* value = inTargetWeightMenu ? (currentAdjusting == 0 ? &targetWeightTens : currentAdjusting == 1 ? &targetWeightOnes : &targetWeightDecimals) : (currentAdjusting == 0 ? &calibrationTens : currentAdjusting == 1 ? &calibrationOnes : &calibrationDecimals);
if (increase) {
(*value)++;
if (*value > 9) *value = 0;
} else {
(*value)--;
if (*value < 0) *value = 9;
}
updateDisplay();
}
void showHomeScreen() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Current Weight:");
lcd.setCursor(0, 1);
lcd.print(currentWeight); // عرض الوزن الحالي بدقة عشريتين
}
void showMainMenu() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Main Menu: ");
lcd.setCursor(0, 1);
lcd.print(menu[menuIndex]);
}
void updateDisplay() {
lcd.clear();
if (inTargetWeightMenu) {
lcd.setCursor(0, 0);
lcd.print("Set Target Wt:");
lcd.setCursor(0, 1);
// Display the target weight with a blinking cursor over the adjusting value
lcd.print(targetWeightTens);
lcd.print(targetWeightOnes);
lcd.print(".");
lcd.print(targetWeightDecimals);
// Set the cursor to the adjusting value
if (currentAdjusting == 0) {
lcd.setCursor(0, 1);
} else if (currentAdjusting == 1) {
lcd.setCursor(1, 1);
} else {
lcd.setCursor(3, 1);
}
lcd.blink(); // Start blinking on the adjusting value
} else if (inCalibrationMenu) {
lcd.setCursor(0, 0);
lcd.print("Calibration:");
lcd.setCursor(0, 1);
// Similar logic for calibration menu
lcd.print(calibrationTens);
lcd.print(calibrationOnes);
lcd.print(".");
lcd.print(calibrationDecimals);
// Set the cursor to the adjusting value
if (currentAdjusting == 0) {
lcd.setCursor(0, 1);
} else if (currentAdjusting == 1) {
lcd.setCursor(1, 1);
} else {
lcd.setCursor(3, 1);
}
lcd.blink(); // Start blinking on the adjusting value
} else {
lcd.noBlink(); // Ensure to turn off blinking when not adjusting values
// Add logic for other menus as needed
showMainMenu();
}
}