How to use and logic with digitalRead?

i want to use and logic with 2 digitalRead output.

if(digitalRead(8)== LOW)and(digitalRead(7)== LOW){
  lcd.setCursor(2,3);
  lcd.print (" open ");}
else {
  lcd.setCursor(2,3);
  lcd.print ("closed");

any clues?

i used this

{
  if (digitalRead(8)== LOW && digitalRead(7)== LOW) {
  lcd.setCursor(2,3);
  lcd.print (" open ");}
else {
  lcd.setCursor(2,3);
  lcd.print ("closed");
  }
}

is this true? no error message after all. only trial and error

if((test1) and (test2)){
   do something;
}

You miss one pair of brackets around both tests...

You don't say what is wrong so we have to guess? I bet you have a compile error.

if(digitalRead(8)== LOW)and(digitalRead(7)== LOW)
                       ^
                       |

That bracket closes the if condition. Below how it should be

if (digitalRead(8) == LOW and digitalRead(7) == LOW)

Note:
Don't use magic numbers; give your pins names (that describe their function) and use those names. If you ever want to use another pin for one of those inputs, you only have to change it in one place and you don't have to dig through your code to find where it's used and have the risk that you miss one and your code no longer works as expected.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.