#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// إعداد شاشة LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
// تعيين دبابيس الأزرار
const int button1Pin = 2;
const int button2Pin = 3;
const int button3Pin = 4;
// تعيين دبابيس LED
const int greenLED = 8;
const int redLED = 9;
// تعيين دبوس الطنان
const int buzzerPin = 10;
// المتغيرات
int correctAnswer = 0;
const char* items[3] = {“Notebook”, “Pen”, “Cup”};
const char* questions[3] = {“What is this?”, “Find the item:”, “Identify this:”};
// نغمة الفوز المعقدة
void playWinTone() {
int melody[] = {262, 294, 330, 349, 392, 440, 494, 523}; // نغمات في الموسيقى الغربية
int duration = 300; // مدة النغمة
for (int i = 0; i < 8; i++) {
tone(buzzerPin, melody[i], duration);
delay(duration);
noTone(buzzerPin);
delay(50); // فاصل بين النغمات
}
}
// نغمة الخطأ المعقدة
void playErrorTone() {
int melody[] = {523, 440, 349, 294, 220}; // نغمات مختلفة للتنبيه
int duration = 300; // مدة النغمة
for (int i = 0; i < 5; i++) {
tone(buzzerPin, melody[i], duration);
delay(duration);
noTone(buzzerPin);
delay(100); // فاصل بين النغمات
}
}
void setup() {
// إعداد شاشة LCD
lcd.init();
lcd.backlight();
// إعداد الأزرار
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
pinMode(button3Pin, INPUT_PULLUP);
// إعداد LED و Buzzer
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// عرض السؤال الأول
showQuestion();
}
void loop() {
if (digitalRead(button1Pin) == LOW) {
checkAnswer(0);
} else if (digitalRead(button2Pin) == LOW) {
checkAnswer(1);
} else if (digitalRead(button3Pin) == LOW) {
checkAnswer(2);
}
}
void showQuestion() {
int index = random(3);
correctAnswer = index;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(questions[index]);
lcd.setCursor(0, 1);
lcd.print(items[index]);
}
void checkAnswer(int answer) {
if (answer == correctAnswer) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Hero!”);
digitalWrite(greenLED, HIGH);
playWinTone(); // تشغيل نغمة الفوز المعقدة
digitalWrite(greenLED, LOW);
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Try again”);
digitalWrite(redLED, HIGH);
playErrorTone(); // تشغيل نغمة الخطأ المعقدة
digitalWrite(redLED, LOW);
}
delay(2000); // الانتظار لبضع ثوانٍ
showQuestion(); // عرض سؤال جديد
}