0
0
Arduinoprogramming~10 mins

Interrupt-driven button handling in Arduino - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to attach an interrupt to pin 2 that triggers on a falling edge.

Arduino
attachInterrupt(digitalPinToInterrupt(2), buttonPressed, [1]);
Drag options to blanks, or click blank then click option'
ALOW
BFALLING
CCHANGE
DRISING
Attempts:
3 left
💡 Hint
Common Mistakes
Using RISING instead of FALLING causes the interrupt to trigger on button release.
2fill in blank
medium

Complete the code to declare the interrupt service routine named buttonPressed.

Arduino
void [1]() {
  buttonState = !buttonState;
}
Drag options to blanks, or click blank then click option'
AbuttonPressed
Bloop
Csetup
DhandleButton
Attempts:
3 left
💡 Hint
Common Mistakes
Naming the ISR differently than in attachInterrupt causes the interrupt not to work.
3fill in blank
hard

Fix the error in the code to correctly declare the buttonState variable as volatile.

Arduino
volatile [1] buttonState = false;
Drag options to blanks, or click blank then click option'
Aint
Bchar
Cboolean
Dbool
Attempts:
3 left
💡 Hint
Common Mistakes
Using int instead of bool causes confusion in true/false logic.
4fill in blank
hard

Fill both blanks to read the button pin state and debounce it in the loop.

Arduino
int reading = digitalRead([1]);
if (reading != [2]) {
  delay(50);
  buttonState = !buttonState;
}
Drag options to blanks, or click blank then click option'
AbuttonPin
BlastButtonState
CbuttonState
DpinMode
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing reading to buttonState instead of lastButtonState causes incorrect debounce logic.
5fill in blank
hard

Fill all three blanks to correctly initialize the button pin, attach the interrupt, and set the pin mode.

Arduino
const int [1] = 2;

void setup() {
  pinMode([2], INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt([3]), buttonPressed, FALLING);
}
Drag options to blanks, or click blank then click option'
AbuttonPin
C2
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using different pin numbers or variable names causes the interrupt not to work.