What are the differences/difficulties between External vs. Pin Change Interrupt for this particular problem?

I want you to rewrite the code by first Using External Interrupt to control the state of the LED. When the LED is
on, pressing the button will turn off the LED and vice-versa.
_ Button_1 controls the LED 1 and Switch 2 controls the LED 2 as shown in Figure 3.
_ LED 1 changes state when the push button is on pressed.
_ LED 2 changes state when the push button is on released.

and then
Repeat the task using Pin Change Interrupt. What are the
differences/difficulties between External vs. Pin Change Interrupt for this particular problem?

for the hardware part, I connected the first led light on number 2 pin on the ardiuno. for the first button, it is connected to number 3 on the ardiuno and I connected the second led light on number 4 pin on the ardiuno. for the second button, it is connected to number 5 on the ardiuno

here is the code

// Define LED pins
const int LED1_PIN = 2;
const int LED2_PIN = 4;

// Define button pins
const int BUTTON1_PIN = 3;
const int BUTTON2_PIN = 5;

// Define debounce delay in milliseconds
const int DEBOUNCE_DELAY = 50;

// Variables to store button state and last button state
int button1State = LOW;
int lastButton1State = LOW;
int button2State = LOW;
int lastButton2State = LOW;

// Variables to store last debounce time
unsigned long lastDebounceTime1 = 0;
unsigned long lastDebounceTime2 = 0;

// Variables to store LED state
int led1State = LOW;
int led2State = LOW;

// Setup function runs once at the beginning
void setup() {
// Set LED pins as output
pinMode(LED1_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);

// Set button pins as input with pull-up resistors
pinMode(BUTTON1_PIN, INPUT_PULLUP);
pinMode(BUTTON2_PIN, INPUT_PULLUP);
}

// Loop function runs repeatedly
void loop() {
// Read button 1 state and debounce
int reading1 = digitalRead(BUTTON1_PIN);
if (reading1 != lastButton1State) {
lastDebounceTime1 = millis();
}
if ((millis() – lastDebounceTime1) > DEBOUNCE_DELAY) {
if (reading1 != button1State) {
button1State = reading1;
if (button1State == LOW) {
// Toggle LED 1 state
led1State = !led1State;
digitalWrite(LED1_PIN, led1State);
}
}
}
lastButton1State = reading1;

// Read button 2 state and debounce
int reading2 = digitalRead(BUTTON2_PIN);
if (reading2 != lastButton2State) {
lastDebounceTime2 = millis();
}
if ((millis() – lastDebounceTime2) > DEBOUNCE_DELAY) {
if (reading2 != button2State) {
button2State = reading2;
if (button2State == LOW) {
// Toggle LED 2 state
led2State = !led2State;
digitalWrite(LED2_PIN, led2State);
}
}
}
lastButton2State = reading2;
}

Answer & Explanation
VerifiedSolved by verified expert
External interrupts and Pin Change interrupts are two different ways to trigger an interrupt in a microcontroller. The choice between them depends on the specific requirements of your project.

External interrupts are triggered by a signal on a dedicated external interrupt pin, while Pin Change interrupts are triggered by a change in the logic level of any of the pins of a specific group.

The main difference between the two is that External interrupts require a dedicated hardware pi

Looking for a similar assignment?

Let Us write for you! We offer custom paper writing services

Place your order

Step-by-step explanation
n, while Pin Change interrupts allow you to use any pin on a specific group. External interrupts are typically faster and have lower latency than Pin Change interrupts since they are handled by dedicated hardware. Pin Change interrupts, on the other hand, can be more flexible, as they allow you to trigger an interrupt based on any pin in a specific group.

In terms of difficulties, using Pin Change interrupts can be more complex because you have to configure the specific group of pins that you want to use, and you have to write more code to handle the interrupts. With External interrupts, you just have to set up the pin and the corresponding interrupt vector, and the rest is handled by the microcontroller’s hardware.

In summary, if you have a specific pin dedicated to triggering an interrupt, you should use External interrupts. If you need more flexibility and want to trigger an interrupt based on any pin in a specific group, you should use Pin Change interrupts.

Download PDF