0
0
Arduinoprogramming~20 mins

Rising, falling, and change triggers in Arduino - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Edge Trigger Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
}
AChange detected
BFalling edge detected
CRising edge detected
DNo output
Attempts:
2 left
💡 Hint
Think about when the button state changes from LOW to HIGH.
Predict Output
intermediate
2: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);
}
ANo output
BRising edge detected
CChange detected
DFalling edge detected
Attempts:
2 left
💡 Hint
Focus on when the button state changes from HIGH to LOW.
Predict Output
advanced
2: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);
}
AChange detected
BNo output
CFalling edge detected
DRising edge detected
Attempts:
2 left
💡 Hint
Think about any difference between current and last button state.
🧠 Conceptual
advanced
2: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?
AattachInterrupt(digitalPinToInterrupt(2), ISR, RISING);
BattachInterrupt(digitalPinToInterrupt(2), ISR, FALLING);
CattachInterrupt(2, ISR, CHANGE);
DattachInterrupt(digitalPinToInterrupt(2), ISR, HIGH);
Attempts:
2 left
💡 Hint
Look for the correct interrupt mode for falling edge detection.
Predict Output
expert
2: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;
  }
}
AInterrupt count: 2
BInterrupt count: 1
CInterrupt count: 0
DNo output
Attempts:
2 left
💡 Hint
Each press and release triggers one interrupt with CHANGE mode.