Consider this Arduino code that puts the microcontroller to sleep and wakes it up using an external interrupt. What will be printed to the serial monitor after the interrupt triggers?
volatile bool wokeUp = false; void wakeUp() { wokeUp = true; } void setup() { Serial.begin(9600); pinMode(2, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(2), wakeUp, FALLING); Serial.println("Going to sleep..."); delay(1000); set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_enable(); sleep_cpu(); sleep_disable(); if (wokeUp) { Serial.println("Woke up from interrupt!"); } else { Serial.println("Did not wake up."); } } void loop() { // Empty loop }
Think about whether FALLING mode wakes from SLEEP_MODE_PWR_DOWN.
The program prints "Going to sleep..." first. Then it goes to sleep. FALLING cannot wake from SLEEP_MODE_PWR_DOWN; only LOW can. So wokeUp remains false, printing "Did not wake up."
When using attachInterrupt() to wake an Arduino from sleep, which interrupt mode will successfully wake the device from SLEEP_MODE_PWR_DOWN?
Only certain interrupt modes can wake the microcontroller from the deepest sleep mode.
In SLEEP_MODE_PWR_DOWN, only interrupts triggered by a LOW level on the interrupt pin can wake the Arduino. Modes like RISING, FALLING, or CHANGE do not wake it from this sleep mode.
Examine the code below. The Arduino never wakes up after going to sleep. What is the cause?
volatile bool wokeUp = false; void wakeUp() { wokeUp = true; } void setup() { Serial.begin(9600); pinMode(2, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(2), wakeUp, FALLING); Serial.println("Going to sleep..."); delay(1000); set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_enable(); sleep_cpu(); sleep_disable(); if (wokeUp) { Serial.println("Woke up from interrupt!"); } else { Serial.println("Did not wake up."); } } void loop() {}
Check the interrupt mode used with SLEEP_MODE_PWR_DOWN.
In SLEEP_MODE_PWR_DOWN, only LOW mode wakes the Arduino. FALLING does not.
Which option contains the correct syntax to put the Arduino into power-down sleep mode and wake on an interrupt?
Look for missing semicolons or missing parentheses in function calls.
Option B correctly uses semicolons and calls sleep_cpu() as a function. Options A, B, and C have missing semicolons or missing parentheses causing syntax errors.
Given the code below, how many times will the Arduino print "Woke up!" if the button connected to pin 2 is pressed 3 times?
volatile int wakeCount = 0; void wakeUp() { wakeCount++; } void setup() { Serial.begin(9600); pinMode(2, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(2), wakeUp, LOW); } void loop() { set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_enable(); sleep_cpu(); sleep_disable(); if (wakeCount > 0) { Serial.println("Woke up!"); wakeCount = 0; } delay(500); }
Consider how the LOW interrupt mode behaves and how the wakeCount variable is reset.
The interrupt mode LOW triggers repeatedly as long as the pin is held LOW. However, the code resets wakeCount to 0 after waking once. So if the button is pressed and released 3 times, the Arduino wakes once per loop iteration, but since the loop resets wakeCount, it only prints "Woke up!" once per loop. Because the button presses happen quickly, the Arduino will print "Woke up!" once per loop cycle, not three times separately.