Challenge - 5 Problems
Pin Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output state of the pin after this code?
Consider this embedded C code snippet that sets a pin high and then low. What is the final state of the pin?
Embedded C
#define PIN 5 #define HIGH 1 #define LOW 0 int pin_state = LOW; void digitalWrite(int pin, int state) { if (pin == PIN) { pin_state = state; } } int main() { digitalWrite(PIN, HIGH); digitalWrite(PIN, LOW); return pin_state; }
Attempts:
2 left
💡 Hint
Think about the last command that sets the pin state.
✗ Incorrect
The pin is first set HIGH (1), then immediately set LOW (0). The final state is LOW (0).
🧠 Conceptual
intermediate1:30remaining
Which statement correctly sets a pin HIGH in embedded C?
You want to set a digital output pin to HIGH. Which of the following code lines correctly does this?
Attempts:
2 left
💡 Hint
The function takes the pin number first, then the state.
✗ Incorrect
The function digitalWrite expects the pin number first, then the state (HIGH or LOW). Only option A follows this.
🔧 Debug
advanced2:30remaining
Why does this code fail to set the pin LOW?
This code is intended to set the pin LOW but it does not work as expected. What is the error?
Embedded C
#define PIN 3 #define HIGH 1 #define LOW 0 int pin_state = HIGH; void digitalWrite(int pin, int state) { if (pin = PIN) { pin_state = state; } } int main() { digitalWrite(PIN, LOW); return pin_state; }
Attempts:
2 left
💡 Hint
Check the if condition inside digitalWrite carefully.
✗ Incorrect
The if condition uses '=' which assigns PIN to pin, always true, so pin_state is set. But this can cause unexpected behavior if pin is changed.
❓ Predict Output
advanced2:00remaining
What is the output of this pin toggle code?
This code toggles a pin state starting from LOW. What is the final pin state after running main?
Embedded C
#define PIN 2 #define HIGH 1 #define LOW 0 int pin_state = LOW; void digitalWrite(int pin, int state) { if (pin == PIN) { pin_state = state; } } void togglePin(int pin) { if (pin_state == LOW) { digitalWrite(pin, HIGH); } else { digitalWrite(pin, LOW); } } int main() { togglePin(PIN); togglePin(PIN); togglePin(PIN); return pin_state; }
Attempts:
2 left
💡 Hint
Count how many times the pin toggles starting from LOW.
✗ Incorrect
Starting LOW, togglePin changes state: LOW->HIGH (1), HIGH->LOW (2), LOW->HIGH (3). Final state is HIGH (1).
🧠 Conceptual
expert1:30remaining
Which option causes a compilation error when writing HIGH or LOW to a pin?
Which of these code snippets will cause a compilation error when trying to write HIGH or LOW to a pin?
Attempts:
2 left
💡 Hint
Consider the data types expected by digitalWrite.
✗ Incorrect
digitalWrite expects an integer for the state. Passing a string like "HIGH" causes a compilation error.