0
0
Arduinoprogramming~10 mins

Wake-up from sleep with interrupt 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 put the Arduino into sleep mode.

Arduino
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
}
Drag options to blanks, or click blank then click option'
AFALLING
BRISING
CCHANGE
DLOW
Attempts:
3 left
💡 Hint
Common Mistakes
Using RISING instead of FALLING causes the interrupt not to trigger on button press.
Not using digitalPinToInterrupt() for the pin number.
2fill in blank
medium

Complete the code to enable sleep mode using the correct sleep mode constant.

Arduino
#include <avr/sleep.h>

void setup() {
  set_sleep_mode([1]);
}

void loop() {
  sleep_enable();
  sleep_cpu();
  sleep_disable();
}
Drag options to blanks, or click blank then click option'
ASLEEP_MODE_IDLE
BSLEEP_MODE_PWR_DOWN
CSLEEP_MODE_ADC
DSLEEP_MODE_STANDBY
Attempts:
3 left
💡 Hint
Common Mistakes
Using SLEEP_MODE_IDLE which does not save much power.
Forgetting to include .
3fill in blank
hard

Fix the error in the interrupt service routine to correctly wake the Arduino from sleep.

Arduino
void wakeUp() {
  [1]();
}
Drag options to blanks, or click blank then click option'
Asleep_cpu
Bsleep_enable
Csleep_disable
DattachInterrupt
Attempts:
3 left
💡 Hint
Common Mistakes
Calling sleep_enable() inside the ISR causes the Arduino to stay asleep.
Calling attachInterrupt() inside the ISR is incorrect.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps pin numbers to their sleep modes if the pin number is greater than 3.

Arduino
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;
  }
}
Drag options to blanks, or click blank then click option'
Astd::map<int, int>()
B>
C<
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect comparison operators like '<' or '=='.
Not initializing the map before using it.
5fill in blank
hard

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.

Arduino
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)
};
Drag options to blanks, or click blank then click option'
Apin
Bmode
Ckey
Dfirst
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names that don't match the loop unpacking.
Not filtering by the correct sleep mode.