استخدام attachInterrupt على منفذ
شغّل التعليمات البرمجية فور تغيّر الإشارة.
استخدام attachInterrupt على منفذ درس مجاني في Arduino & IoT Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Arduino & IoT Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Arduino & IoT Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Wiring Code to a Pin
The attachInterrupt function tells your board: watch this pin, and the moment its signal changes, run my function right away.
Only Special Pins
Not every pin supports interrupts. On an Uno only pins 2 and 3 do, so plug your signal into one of those.
Translate Pin to Number
Wrap the pin in digitalPinToInterrupt so the right interrupt number is used. It keeps your code portable across boards.
digitalPinToInterrupt(2);Three Pieces
You give attachInterrupt three things: which pin to watch, the function to run, and the kind of change that should trigger it.
attachInterrupt(digitalPinToInterrupt(2), myISR, RISING);The Handler Function
The function you name is your handler, also called an ISR. The board calls it automatically; you never call it yourself.
void myISR() {
flag = true;
}RISING Edge
Pass RISING to fire when the pin goes from LOW to HIGH, like the moment a signal first switches on. 📈
FALLING Edge
Pass FALLING to fire when the pin drops from HIGH to LOW. A pull-up button often triggers here, since pressing pulls the pin down.
CHANGE Mode
Pass CHANGE to fire on either edge, both up and down. It is handy when you care about every transition of the signal.
Set It Up in setup()
Call attachInterrupt once inside setup, right after you set the pin mode. From then on the watching happens on its own.
pinMode(2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2), myISR, FALLING);The Loop Stays Light
With the interrupt attached, your loop no longer polls that pin. The handler runs only when the chosen edge actually occurs.
Turning It Off
If you ever need to stop watching, detachInterrupt removes the link. Most projects attach once and never detach.
detachInterrupt(digitalPinToInterrupt(2));Quick Check
Which trigger mode fires only when a pin goes from LOW up to HIGH?
Recap
You learned to call attachInterrupt with a pin, a handler, and a mode like RISING. Now hardware runs your code the instant a signal changes. 🎉
الأسئلة الشائعة
هل درس «استخدام attachInterrupt على منفذ» مجاني؟
نعم — نص درس «استخدام attachInterrupt على منفذ» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Arduino & IoT Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Arduino & IoT Academy 4 دروس في المجموع.
ماذا ستتعلم في «استخدام attachInterrupt على منفذ»؟
شغّل التعليمات البرمجية فور تغيّر الإشارة. تتمرن على Arduino & IoT Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Arduino & IoT Academy؟
لا تُشترط خبرة سابقة. Arduino & IoT Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «استخدام attachInterrupt على منفذ»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Arduino & IoT Academy هذا؟
نعم. كل درس في Arduino & IoT Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- Polling مقابل Interrupts
- استخدام attachInterrupt على منفذ
- كتابة ISR آمن
- عدّ النبضات من Encoder