Complete the code to attach an interrupt to pin 2 that triggers on a falling edge.
attachInterrupt(digitalPinToInterrupt(2), buttonPressed, [1]);
The interrupt should trigger when the button is pressed, which pulls the pin LOW, so FALLING is used.
Complete the code to declare the interrupt service routine named buttonPressed.
void [1]() {
buttonState = !buttonState;
}The interrupt service routine must have the same name as used in attachInterrupt, which is buttonPressed.
Fix the error in the code to correctly declare the buttonState variable as volatile.
volatile [1] buttonState = false;The bool type is used in Arduino to represent true/false values and should be declared volatile for variables shared with interrupts.
Fill both blanks to read the button pin state and debounce it in the loop.
int reading = digitalRead([1]); if (reading != [2]) { delay(50); buttonState = !buttonState; }
We read the buttonPin and compare it to lastButtonState to detect changes for debouncing.
Fill all three blanks to correctly initialize the button pin, attach the interrupt, and set the pin mode.
const int [1] = 2; void setup() { pinMode([2], INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt([3]), buttonPressed, FALLING); }
The button pin is declared as buttonPin with value 2. We set pinMode for buttonPin and attach the interrupt to pin 2.