I am new to programming and have some issues with the && and || commands. They do not perform as anticipated. The code below aim to do the following: (Work with a combination of switches)
The 3 statements:
If now switches pressed, If switch in pin 2 is press with all the other switches. If if switch in pin 2 is pressed and any of the other switches is not pressed. The last 2 command is not working as intended.
NOTE: the switches used here read HIGH when not pressed.
int switchstate = 0;
int switchstateB = 0;
int switchstateC = 0;
int switchstateD = 0;
void setup(){
pinMode(3,OUTPUT);
pinMode(7,OUTPUT);
pinMode(2,INPUT);
pinMode(4,INPUT);
pinMode(5,INPUT);
pinMode(6,INPUT);
}
void loop(){
switchstate = digitalRead(2);
switchstateB = digitalRead(4);
switchstateC = digitalRead(5);
switchstateD = digitalRead(6);
if (digitalRead(2) == HIGH && digitalRead(4) == HIGH && digitalRead(5) == HIGH && digitalRead(6) == HIGH ) {
digitalWrite (3, HIGH);
digitalWrite (7, HIGH);
}
else if (digitalRead(2) == LOW && digitalRead(4) == LOW && digitalRead(5) == LOW && digitalRead(6) == LOW ) {
digitalWrite(7,LOW);
digitalWrite (3,HIGH);
}
else if (digitalRead(2) == LOW && digitalRead(4) == HIGH || digitalRead(5) == HIGH || digitalRead(6) == HIGH ) {
digitalWrite(7,HIGH);
digitalWrite (3,LOW);
}
}