0Pricing
Arduino & IoT Academy · درس

‏if/else: مصباح LED يتبع الزر

إضاءة مصباح LED ما دام الزر مضغوطًا

‏if/else: مصباح LED يتبع الزر درس مجاني في Arduino & IoT Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Arduino & IoT Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Arduino & IoT Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Make a Decision

Reading a button is great, but the magic is reacting to it. An if statement lets your board choose what to do based on the reading.

if Runs Conditionally

The code inside if only runs when its test is true. So you can light an LED only when the button is actually pressed.

if (state == LOW) {
  // button is pressed
}

Test for Pressed

Remember the inversion: with INPUT_PULLUP, pressed is LOW. So your condition checks whether the stored state equals LOW.

int state = digitalRead(2);
if (state == LOW) {
  digitalWrite(13, HIGH);
}

Add an else Branch

What about when it isn't pressed? The else block runs whenever the if test is false, giving you the other half of the behavior.

if (state == LOW) {
  digitalWrite(13, HIGH);
} else {
  digitalWrite(13, LOW);
}

LED Follows the Button

Together if and else make the LED mirror the button: on while held, off when released. That is your first real input-to-output link. 💡

Set Up Both Pins

The button pin is an input and the LED pin is an output. Declare both modes in setup() so each pin knows its job.

void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(13, OUTPUT);
}

One Equals vs Two

A classic trap: use == to compare, not a single =. One equals assigns a value, while two equals asks if they match.

Loop Keeps It Live

Put the whole thing in loop() so it checks the button thousands of times a second. The LED reacts the moment you press.

Read Then Decide

The pattern is always the same: read the input, then decide with if/else. Get comfortable with it; you'll reuse it everywhere.

Compare Right in the if

You can skip the variable and test the read directly. It reads cleanly and does exactly the same comparison inside the parentheses.

if (digitalRead(2) == LOW) {
  digitalWrite(13, HIGH);
}

From Sensing to Acting

You just closed the loop from input to output with pure logic. Swap the LED for a buzzer or motor and the same idea still drives it.

Quick Check

Pick the condition that lights the LED while the button is held.

Recap

You used if/else to make an LED follow a button: LOW lights it, HIGH turns it off. Remember == for comparison. Next you'll wire it all up. 🔌

الأسئلة الشائعة

هل درس «‏if/else: مصباح LED يتبع الزر» مجاني؟

نعم — نص درس «‏if/else: مصباح LED يتبع الزر» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Arduino & IoT Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Arduino & IoT Academy 4 دروس في المجموع.

ماذا ستتعلم في «‏if/else: مصباح LED يتبع الزر»؟

إضاءة مصباح LED ما دام الزر مضغوطًا تتمرن على Arduino & IoT Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Arduino & IoT Academy؟

لا تُشترط خبرة سابقة. Arduino & IoT Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «‏if/else: مصباح LED يتبع الزر»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Arduino & IoT Academy هذا؟

نعم. كل درس في Arduino & IoT Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. ‏INPUT مقابل INPUT_PULLUP
  2. قراءة حالة زر
  3. ‏if/else: مصباح LED يتبع الزر
  4. توصيل زر على لوحة تجارب
← العودة إلى Arduino & IoT Academy