عدّ النبضات من Encoder
احسب الحواف السريعة دون فقدان أيٍّ منها.
عدّ النبضات من Encoder درس مجاني في Arduino & IoT Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Arduino & IoT Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Arduino & IoT Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
A Wheel of Pulses
A rotary encoder sends a quick stream of electrical pulses as it spins. Count those pulses and you know how far it has turned.
Too Fast to Poll
Spin an encoder quickly and pulses fly by in microseconds. A normal loop reading the pin would miss many of them.
Interrupts to the Rescue
This is the perfect job for an interrupt. The hardware catches every edge for you, so no pulse slips through unnoticed. ⚡
A Volatile Counter
Make a volatile counter the ISR can increment. Marking it volatile keeps the loop reading the true, up-to-date total.
volatile long pulseCount = 0;Attach the Interrupt
In setup, point attachInterrupt at the encoder pin and fire on RISING so each pulse calls your counting handler.
attachInterrupt(digitalPinToInterrupt(2), countPulse, RISING);A Tiny ISR
The whole handler does one thing: add one to the counter. Short and fast, exactly as a good ISR should be.
void countPulse() {
pulseCount++;
}Read Without Tearing
To copy the count in the loop, briefly pause interrupts so the ISR cannot change it mid-read and leave you a torn value.
noInterrupts();
long c = pulseCount;
interrupts();Use the Count
Now the loop can print the total, compute speed, or react, all while the ISR keeps tallying new pulses in the background.
Serial.println(c);From Pulses to Rotation
Each encoder has a fixed pulses-per-turn. Divide your count by that number to learn how many full revolutions happened.
Measuring Speed
Count pulses over a known time window in the loop, and you can turn that rate into speed or RPM for a motor or wheel.
Reset When Needed
To start a fresh measurement, set the counter back to zero, again pausing interrupts so you do not lose a pulse arriving at that instant.
noInterrupts();
pulseCount = 0;
interrupts();Quick Check
Why do we briefly pause interrupts when reading the pulse counter in the loop?
Recap
You built a pulse counter: a volatile total, a tiny ISR, and safe reads in the loop. Now you can measure rotation and speed reliably. 🎉
الأسئلة الشائعة
هل درس «عدّ النبضات من Encoder» مجاني؟
نعم — نص درس «عدّ النبضات من Encoder» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Arduino & IoT Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Arduino & IoT Academy 4 دروس في المجموع.
ماذا ستتعلم في «عدّ النبضات من Encoder»؟
احسب الحواف السريعة دون فقدان أيٍّ منها. تتمرن على Arduino & IoT Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Arduino & IoT Academy؟
لا تُشترط خبرة سابقة. Arduino & IoT Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «عدّ النبضات من Encoder»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Arduino & IoT Academy هذا؟
نعم. كل درس في Arduino & IoT Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- Polling مقابل Interrupts
- استخدام attachInterrupt على منفذ
- كتابة ISR آمن
- عدّ النبضات من Encoder