Complete the code to read the state of a button connected to pin 7.
int buttonState = digitalRead([1]);The button is connected to pin 7, so digitalRead(7) reads its state.
Complete the code to check if the button on pin 4 is pressed (HIGH).
if (digitalRead([1]) == HIGH) { // button pressed }
We check pin 4 because the button is connected there. digitalRead(4) reads its state.
Fix the error in reading the button state on pin 9.
int state = digitalRead([1]);digitalRead() requires the pin number as an integer, so 9 is correct.
Fill both blanks to read a button on pin 5 and check if it is LOW.
if (digitalRead([1]) == [2]) { // button not pressed }
Pin 5 is where the button is connected. Checking if digitalRead(5) == LOW means button is not pressed if wired with pull-up.
Fill all three blanks to create a dictionary-like structure mapping pin names to their digitalRead values, only for pins with HIGH state.
const int pins[] = {2, 3, 4};
void setup() {
Serial.begin(9600);
}
void loop() {
for (int i = 0; i < 3; i++) {
int pin = pins[i];
if (digitalRead(pin) == HIGH) {
Serial.print("Pin ");
Serial.print(pin);
Serial.println(" is HIGH");
}
}
delay(1000);
}
// Create a map-like structure:
// { [1]: [2] for [3] in pins if digitalRead([3]) == HIGH }This comprehension-like structure maps each pin to its digitalRead value if that value is HIGH.
