list of products from Amazon (Not mine, i just link them!) Scroll Down For Article! Note: Elegoo boards are not Arduino, but they are compatible with Arduino IDE and are mutch more affordable!
In Arduino, the interrupt
function allows the microcontroller to pause its normal program execution and immediately run a special function (called an Interrupt Service Routine or ISR) in response to a specific event, like a change in a pin's state or the expiration of a timer.
How Interrupts Work:
- Interrupts are like alarms that tell the processor to stop what it's doing and pay attention to something important.
- When an interrupt occurs, the processor stops the current task, saves its state, and executes the ISR.
- Once the ISR completes, the processor resumes its normal tasks.
Key Points About Arduino Interrupts:
Pin-based Interrupts:
- Arduino supports hardware interrupts on specific pins. For example:
- On an Arduino Uno, hardware interrupts are available on pins 2 and 3.
- Other boards may support interrupts on additional or all pins.
- These pins are associated with specific interrupt numbers.
- Arduino supports hardware interrupts on specific pins. For example:
Function to Attach an Interrupt:
- Use the
attachInterrupt()
function to define an interrupt and associate it with a pin and ISR:pin
: The pin you want to monitor for an interrupt.ISR
: The name of the Interrupt Service Routine to execute.mode
: Defines the event that triggers the interrupt:LOW
: Triggers when the pin is LOW.CHANGE
: Triggers when the pin changes state (HIGH to LOW or LOW to HIGH).RISING
: Triggers when the pin changes from LOW to HIGH.FALLING
: Triggers when the pin changes from HIGH to LOW.
- Use the
Disabling and Re-enabling Interrupts:
- Use
detachInterrupt(interruptNumber)
to disable the interrupt.
- Use
Characteristics of an ISR:
- Keep it Short: The ISR should execute quickly to avoid delaying other interrupts.
- No
delay()
or Serial Communication: These functions depend on interrupts themselves and will not work inside an ISR. - Variables in ISRs: Use the
volatile
keyword for variables shared between the ISR and the main program.
0 Comments