Interrupt function

Schematic.

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:

  1. 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.
  2. Function to Attach an Interrupt:

    • Use the attachInterrupt() function to define an interrupt and associate it with a pin and ISR:
      attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);
      • 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.
  3. Disabling and Re-enabling Interrupts:

    • Use detachInterrupt(interruptNumber) to disable the interrupt.
  4. 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.
Schematic:

Schematic

Code:

#define LED_PIN 13      // LED connected to pin 13
#define BUTTON_PIN 2    // Button connected to interrupt pin 2

volatile bool ledState = false; // Variable to track LED state

void setup() {
  pinMode(LED_PIN, OUTPUT);               // Set LED pin as output
  pinMode(BUTTON_PIN, INPUT_PULLUP);      // Set button pin as input with pull-up resistor
  attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), toggleLED, FALLING); // Trigger on button press
}

void loop() {
  // Nothing to do here; the LED state is handled by the interrupt
}

// Interrupt Service Routine (ISR)
void toggleLED() {
  ledState = !ledState;          // Toggle the LED state
  digitalWrite(LED_PIN, ledState); // Update the LED
}
Project link:
https://drive.google.com/file/d/1hrUh5jpxibwFkGIcW8VUMIbI_7tpASS6/view?usp=drive_link


Post a Comment

0 Comments