ใช้ attachInterrupt กับขา
เรียกใช้โค้ดทันทีที่สัญญาณเปลี่ยนแปลง
ใช้ attachInterrupt กับขา เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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 กับขา” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Arduino & IoT Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ใช้ attachInterrupt กับขา”
เรียกใช้โค้ดทันทีที่สัญญาณเปลี่ยนแปลง คุณปฏิบัติ Arduino & IoT Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Arduino & IoT Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Arduino & IoT Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “ใช้ attachInterrupt กับขา” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Arduino & IoT Academy นี้ได้ไหม
ได้ บทเรียน Arduino & IoT Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การตรวจสอบสถานะเทียบกับอินเทอร์รัปต์
- ใช้ attachInterrupt กับขา
- เขียน ISR ที่ปลอดภัย
- นับพัลส์จากเอ็นโคเดอร์