Given the following Arduino code controlling two LEDs and two buttons, what will be the state of the LEDs after pressing button 1 once?
const int buttonPin1 = 2; const int buttonPin2 = 3; const int ledPin1 = 9; const int ledPin2 = 10; int buttonState1 = 0; int buttonState2 = 0; void setup() { pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(buttonPin1, INPUT_PULLUP); pinMode(buttonPin2, INPUT_PULLUP); digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, LOW); } void loop() { buttonState1 = digitalRead(buttonPin1); buttonState2 = digitalRead(buttonPin2); if (buttonState1 == LOW) { digitalWrite(ledPin1, HIGH); } else { digitalWrite(ledPin1, LOW); } if (buttonState2 == LOW) { digitalWrite(ledPin2, HIGH); } else { digitalWrite(ledPin2, LOW); } }
Remember that the buttons use INPUT_PULLUP, so LOW means pressed.
Button 1 pressed (LOW) turns LED1 ON. Button 2 is not pressed (HIGH), so LED2 stays OFF.
Using the same code as before, what will be the state of LED1 and LED2 if both buttons are pressed at the same time?
const int buttonPin1 = 2; const int buttonPin2 = 3; const int ledPin1 = 9; const int ledPin2 = 10; int buttonState1 = 0; int buttonState2 = 0; void setup() { pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(buttonPin1, INPUT_PULLUP); pinMode(buttonPin2, INPUT_PULLUP); digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, LOW); } void loop() { buttonState1 = digitalRead(buttonPin1); buttonState2 = digitalRead(buttonPin2); if (buttonState1 == LOW) { digitalWrite(ledPin1, HIGH); } else { digitalWrite(ledPin1, LOW); } if (buttonState2 == LOW) { digitalWrite(ledPin2, HIGH); } else { digitalWrite(ledPin2, LOW); } }
Check the conditions for each LED separately.
Both buttons pressed means both buttonState1 and buttonState2 are LOW, so both LEDs turn ON.
Consider this Arduino code snippet. LED1 works fine, but LED2 never turns ON even when button 2 is pressed. What is the cause?
const int buttonPin1 = 2; const int buttonPin2 = 3; const int ledPin1 = 9; const int ledPin2 = 10; void setup() { pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(buttonPin1, INPUT_PULLUP); // pinMode(buttonPin2, INPUT_PULLUP); // Missing this line } void loop() { int buttonState1 = digitalRead(buttonPin1); int buttonState2 = digitalRead(buttonPin2); if (buttonState1 == LOW) { digitalWrite(ledPin1, HIGH); } else { digitalWrite(ledPin1, LOW); } if (buttonState2 == LOW) { digitalWrite(ledPin2, HIGH); } else { digitalWrite(ledPin2, LOW); } }
Check the setup function for pin modes.
Without setting buttonPin2 as INPUT_PULLUP, its input is floating and may never read LOW reliably.
Which of the following Arduino code snippets will cause a syntax error?
Check the syntax of the if statement in Arduino (C++).
Option A misses parentheses around the condition, causing a syntax error.
This Arduino code toggles LED1 each time button 1 is pressed. How many times will LED1 change state after pressing button 1 exactly 5 times?
const int buttonPin1 = 2; const int ledPin1 = 9; int ledState = LOW; int lastButtonState = HIGH; void setup() { pinMode(ledPin1, OUTPUT); pinMode(buttonPin1, INPUT_PULLUP); digitalWrite(ledPin1, ledState); } void loop() { int buttonState = digitalRead(buttonPin1); if (lastButtonState == HIGH && buttonState == LOW) { ledState = !ledState; digitalWrite(ledPin1, ledState); } lastButtonState = buttonState; }
Consider how the code detects button presses using state changes.
The code toggles LED1 only on the transition from HIGH to LOW (button press). Each press toggles once, so 5 presses toggle 5 times.
