// تعريف أرقام التوصيل
const int redLEDPin = 13; // دبوس LED الأحمر
const int greenLEDPin = 7; // دبوس LED الأخضر
const int pushbuttonPin = 4; // دبوس زر الضغط
// تعريف المتغيرات
int previousState = LOW; // الحالة السابقة لزر الضغط
int currentState = LOW; // الحالة الحالية لزر الضغط
bool isBlinkingRed = true; // علامة لتحديد أي LED يومض (أحمر أو أخضر)
unsigned long lastDebounceTime = 0; // آخر مرة تم فيها إزالة الكذبة من زر الضغط
const long debounceTime = 50; // وقت إزالة الكذبة بالمللي ثانية
void setup() {
// ضبط دبوس LED الأحمر كمخرج
pinMode(redLEDPin, OUTPUT);
// ضبط دبوس LED الأخضر كمخرج
pinMode(greenLEDPin, OUTPUT);
// ضبط دبوس زر الضغط كمدخل مع المقاوم السحب
pinMode(pushbuttonPin, INPUT_PULLUP);
}
void loop() {
// قراءة الحالة الحالية لزر الضغط
currentState = digitalRead(pushbuttonPin);
// إزالة الكذبة من زر الضغط للقضاء على تأثير الكذبة
if (currentState != previousState) {
unsigned long currentTime = millis();
if (currentTime - lastDebounceTime >= debounceTime) {
lastDebounceTime = currentTime;
if (currentState == HIGH) { // إذا تم الضغط على زر الضغط
if (isBlinkingRed) { // إذا كان LED الأحمر يومضًا ، فتحول إلى وميض أخضر
isBlinkingRed = false;
} else { // إذا كان LED الأخضر يومضًا ، فتحول إلى وميض أحمر
isBlinkingRed = true;
}
}
}
}
// تحديث الحالة السابقة
previousState = currentState;
// تحديث المصابيح LED بناءً على حالة الوميض
if (isBlinkingRed) {
// تشغيل LED الأحمر
digitalWrite(redLEDPin, HIGH);
// إيقاف تشغيل LED الأخضر
digitalWrite(greenLEDPin, LOW);
} else {
// إيقاف تشغيل LED الأحمر
digitalWrite(redLEDPin, LOW);
// تشغيل LED الأخضر
digitalWrite(greenLEDPin, HIGH);
}
// وميض LED النشط
if (isBlinkingRed) {
// وميض LED الأحمر بسرعة 0.5 هرتز
delay(debounceTime);
digitalWrite(redLEDPin, LOW);
} else {
// وميض LED الأخضر بسرعة 0.5 هرتز
delay(debounceTime);
digitalWrite(greenLEDPin, LOW);
}
}