From the task, we understand that,
void loop()
.Detecting the pulse using polling
digitalRead()
takes about 4 µs.digitalRead()
is not possible.Detecting the pulse using an interrupt
The firmware
// Interrupt Service Routine for INT0 (pin 2)
void ISR_INT0() {
digitalWrite(13,!digitalRead(13)); //toggles the LED when pulse detected
}
void setup() {
pinMode(2, INPUT); // Configure interrupt INT0 (pin 2) as input
pinMode(13, OUTPUT); //onboard LED is connected to pin 13
// Attach external interrupts
attachInterrupt(digitalPinToInterrupt(2), ISR_INT0, RISING); // Trigger on rising edge for INT0
}
void loop() {
}
This code uses an external interrupt (INT0) on pin 2 of an Arduino to detect a rising edge signal and toggle an LED on pin 13.
Pin Setup
pinMode(2, INPUT)
: Configures pin 2 as an input.pinMode(13, OUTPUT)
: Configures pin 13 as an output to control the onboard LED.Interrupt Attachment:
attachInterrupt(digitalPinToInterrupt(2), ISR_INT0, RISING)
: Sets up an interrupt on pin 2 to trigger on a rising edge.ISR Functionality:
ISR_INT0()
is executed:digitalWrite(13, !digitalRead(13)).
Main Loop