Expected Behavior:
I am attempting to use && or nested if statements to achieve the effect of two switches being required to close prior to an LED light (i.e., flip both switches), the LED turns on for 1 second, and then turns off.
Encountered Behavior:
It appears that no matter how I write the statement, if the SECOND if is true, the LED lights up.
Additional information:
The switches connect directly to ground when closed.
I have tried multiple conditional statements:
- using
&&:if (switch1 == LOW && switch2 == LOW) {LED code} - Nested
if:if (switch1 == LOW) { if (switch2 == LOW) {LED code} }
I have tried multiple variable declarations:
int switch1 = 0; int switch2 = 1;(using integer representation of digispark pinout)#define switch1 PB1
#define switch1 PB1
#define switch2 PB0
void setup()
{
pinMode(2, OUTPUT);
pinMode(switch1, INPUT_PULLUP);
pinMode(switch2, INPUT_PULLUP);
}
void loop() {
if (!digitalRead(switch1)) {
if (!digitalRead(switch2)) {
digitalWrite(2, HIGH);
// turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(2, LOW);
}
}
else {
digitalWrite(2, LOW);
delay(1);
}
}
My Digispark Attiny85 is attached to a breadboard, where it receives power through the 5V and ground pins on the Digispark board. PB0 and PB1 connect directly to ground when closed, otherwise I count on the pull-up resistors. PB2 is connected to an LED -> resistor -> ground. All components share the same 5V power supply and ground.

