Complete the code to put the Arduino into sleep mode.
void setup() {
pinMode(2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2), wakeUp, [1]);
}
void loop() {
sleep_enable();
sleep_cpu();
sleep_disable();
}
void wakeUp() {
// This function will be called when interrupt occurs
}The interrupt is set to trigger on the FALLING edge, which means when the button connected to pin 2 is pressed (goes from HIGH to LOW).
Complete the code to enable sleep mode using the correct sleep mode constant.
#include <avr/sleep.h> void setup() { set_sleep_mode([1]); } void loop() { sleep_enable(); sleep_cpu(); sleep_disable(); }
SLEEP_MODE_PWR_DOWN is the deepest sleep mode that turns off most of the Arduino's functions to save power.
Fix the error in the interrupt service routine to correctly wake the Arduino from sleep.
void wakeUp() {
[1]();
}The wakeUp function should disable sleep mode by calling sleep_disable() to resume normal operation.
Fill both blanks to create a dictionary comprehension that maps pin numbers to their sleep modes if the pin number is greater than 3.
const int pins[] = {2, 3, 4, 5};
std::map<int, int> pinSleepModes = [1];
// Example: pinSleepModes = {4: SLEEP_MODE_PWR_DOWN, 5: SLEEP_MODE_IDLE}
// Fill the blanks below
for (int pin : pins) {
if (pin [2] 3) {
pinSleepModes[pin] = SLEEP_MODE_PWR_DOWN;
}
}We initialize an empty map with std::map
Fill all three blanks to create a dictionary comprehension that maps pin names to their sleep modes only if the mode is SLEEP_MODE_PWR_DOWN.
std::map<std::string, int> pinModes = {
{"pin2", SLEEP_MODE_IDLE},
{"pin3", SLEEP_MODE_PWR_DOWN},
{"pin4", SLEEP_MODE_PWR_DOWN}
};
std::map<std::string, int> filteredModes = {
[1]: [2] for (const auto& [[3], mode] : pinModes) if (mode == SLEEP_MODE_PWR_DOWN)
};We use 'key' as the variable for the pin name and 'mode' for the sleep mode. The comprehension maps key to mode for modes equal to SLEEP_MODE_PWR_DOWN.