Challenge - 5 Problems
Edge Trigger Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Detecting a Rising Edge Trigger
What will be the output on the serial monitor when the button connected to pin 2 is pressed (assuming it was previously not pressed)?
Arduino
const int buttonPin = 2; int buttonState = 0; int lastButtonState = 0; void setup() { pinMode(buttonPin, INPUT); Serial.begin(9600); } void loop() { buttonState = digitalRead(buttonPin); if (buttonState == HIGH && lastButtonState == LOW) { Serial.println("Rising edge detected"); } lastButtonState = buttonState; delay(50); }
Attempts:
2 left
💡 Hint
Think about when the button state changes from LOW to HIGH.
✗ Incorrect
The code prints "Rising edge detected" only when the button state changes from LOW to HIGH, which is a rising edge trigger.
❓ Predict Output
intermediate2:00remaining
Detecting a Falling Edge Trigger
What will be printed on the serial monitor when the button connected to pin 3 is pressed (assuming it was previously not pressed)?
Arduino
const int buttonPin = 3; int buttonState = 0; int lastButtonState = HIGH; void setup() { pinMode(buttonPin, INPUT_PULLUP); Serial.begin(9600); } void loop() { buttonState = digitalRead(buttonPin); if (buttonState == LOW && lastButtonState == HIGH) { Serial.println("Falling edge detected"); } lastButtonState = buttonState; delay(50); }
Attempts:
2 left
💡 Hint
Focus on when the button state changes from HIGH to LOW.
✗ Incorrect
The code prints "Falling edge detected" only when the button state changes from HIGH to LOW, which is a falling edge trigger.
❓ Predict Output
advanced2:00remaining
Detecting Any Change Trigger
What will be printed on the serial monitor when the button connected to pin 4 changes state (pressed or released)?
Arduino
const int buttonPin = 4; int buttonState = 0; int lastButtonState = 0; void setup() { pinMode(buttonPin, INPUT); Serial.begin(9600); } void loop() { buttonState = digitalRead(buttonPin); if (buttonState != lastButtonState) { Serial.println("Change detected"); } lastButtonState = buttonState; delay(50); }
Attempts:
2 left
💡 Hint
Think about any difference between current and last button state.
✗ Incorrect
The code prints "Change detected" whenever the button state changes, either from LOW to HIGH or HIGH to LOW.
🧠 Conceptual
advanced2:00remaining
Understanding Interrupts with Rising and Falling Edges
Which statement correctly describes how to use interrupts to detect a falling edge on pin 2 in Arduino?
Attempts:
2 left
💡 Hint
Look for the correct interrupt mode for falling edge detection.
✗ Incorrect
To detect a falling edge, the interrupt mode must be FALLING. The function digitalPinToInterrupt converts the pin number to the interrupt number.
❓ Predict Output
expert2:00remaining
Output of Combined Edge Detection with Interrupts
Given the code below, what will be the output on the serial monitor if the button connected to pin 3 is pressed and then released?
Arduino
volatile int count = 0; void ISR() { count++; } void setup() { pinMode(3, INPUT_PULLUP); Serial.begin(9600); attachInterrupt(digitalPinToInterrupt(3), ISR, CHANGE); } void loop() { static int lastCount = 0; if (count != lastCount) { Serial.print("Interrupt count: "); Serial.println(count); lastCount = count; } }
Attempts:
2 left
💡 Hint
Each press and release triggers one interrupt with CHANGE mode.
✗ Incorrect
The CHANGE mode triggers an interrupt on both rising and falling edges. Pressing and releasing the button causes two interrupts, so count increments twice.