Consider the following Arduino code snippet. What will be printed on the Serial Monitor after pressing the button connected to pin 2?
volatile int count = 0; void setup() { Serial.begin(9600); pinMode(2, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(2), incrementCount, FALLING); } void loop() { Serial.println(count); delay(1000); } void incrementCount() { count++; }
Think about what happens when the button is pressed and how the interrupt updates the variable.
The interrupt triggers on the FALLING edge of pin 2, calling incrementCount() which increases count. The loop prints count every second, so the number increases each time the button is pressed.
Choose the correct statement about the attachInterrupt() function in Arduino.
Think about what interrupts do compared to normal code flow.
attachInterrupt() lets you run a function automatically when a pin changes state, without checking it repeatedly in loop(). Pins vary by board, ISRs should be short and not use delay(), and detachInterrupt() is optional.
Identify the cause of the compile error in this Arduino code snippet:
void setup() {
attachInterrupt(2, blinkLED(), RISING);
}
void blinkLED() {
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
}Look carefully at how the function is passed to attachInterrupt().
The code calls blinkLED() immediately by using parentheses. Instead, the function name without parentheses should be passed to attachInterrupt() to register it as the ISR.
Choose the correct syntax to attach an interrupt on pin 3 that triggers on a falling edge and calls the function handleInterrupt.
Remember how to convert a pin number to an interrupt number and how to pass the function.
Option C correctly uses digitalPinToInterrupt(3) to get the interrupt number and passes the function name handleInterrupt without parentheses.
Given this Arduino code, if a noisy button connected to pin 2 is pressed once causing multiple rapid FALLING edges, how many times will count increment after the press?
volatile int count = 0; void setup() { pinMode(2, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(2), incrementCount, FALLING); } void loop() {} void incrementCount() { count++; }
Think about how mechanical button noise affects interrupts triggered on edges.
Mechanical buttons often cause multiple rapid transitions (bouncing) when pressed. Each FALLING edge triggers the interrupt, so count increments multiple times unless debouncing is implemented.