# Help with and(&&) function and digitalRead

else if ((strID.indexOf("00:D8:5B:4D") >= 0) && digitalRead(LED_PIN) == HIGH) digitalWrite(LED_PIN, LOW); digitalWrite(LED_PIN1, LOW);

Here is the code. strID is for determing if the Serial Key of the RFID is correct AND LED_PIN is HIGH so i don't know why i can't get both statements satisfied. the LED_PIN is HIGH and i'm also using the correct RFID, i think it is something to do with "&&"

Help thanks

i'm a beginner sorry.

You need to post all of your code with code tags and use the <> to enter it properly.

My first thought though is that (strID.indexOf(“00:D8:5B:4D”) >= 0) is also going to be true because it will always be equal to or greater than 0 unless you are using an integer that can go to -1 or less.

``````else if ((strID.indexOf("00:D8:5B:4D") >= 0) && digitalRead(LED_PIN) == HIGH)
``````

might get seen as

``````else if (( (strID.indexOf("00:D8:5B:4D") >= 0) && digitalRead(LED_PIN) ) == HIGH)
``````

where you want

``````else if ( (strID.indexOf("00:D8:5B:4D") >= 0) && (digitalRead(LED_PIN) == HIGH) )
``````

I suggest that you save the value from digitalRead() into a variable prior to the IF and use the variable in the IF clause because that will allow you to print the value for debugging purposes. The way your code is written it is impossible to know what value is being produced by the digitalRead()

...R

To get both statements satisfied they'll need some curly brackets

``````  {  digitalWrite(LED_PIN, LOW);    digitalWrite(LED_PIN1, LOW); }
``````

but I guess you meant both comparisons giving 'true'

My first thought though is that (strID.indexOf("00:D8:5B:4D") >= 0) is also going to be true

If the searched for string does not exist in the String, indexOf() returns -1.

``````  if (strID.indexOf("00:D8:5B:4D") >= 0)
{
digitalWrite(LED_PIN, HIGH);
digitalWrite(LED_PIN1, LOW);
Serial.println("RIGHT RFID AND ON KO SIYA");

for (pos = 90; pos <= 180; pos += 1)
{                                  // goes from 90 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos);              // tell servo to go to position in variable 'pos'
delay(1);                       // waits 1ms for the servo to reach the position
}
}
//DI KO MAPAGANA TONG PART NA TO PUTNAGINA ANONG KULANG GRR
else if ((strID.indexOf("00:D8:5B:4D") >= 0) && (digitalRead(LED_PIN) == HIGH))
{
digitalWrite(LED_PIN, LOW);
digitalWrite(LED_PIN1, LOW);
Serial.println("NAKA ON SO IF OFF KO");
}
else
{
digitalWrite(LED_PIN1, HIGH);
digitalWrite(LED_PIN, LOW);
Serial.println("WRONG RFID BOY");

for (pos = 180; pos >= 90; pos -= 1)
{                                   // goes from 180 degrees to 90 degrees
myservo.write(pos);               // tell servo to go to position in variable 'pos'
delay(1);                        // waits 1ms for the servo to reach the position
}
}
``````

Here is the whole code for comparison reasons. still now working even with the parenthesis changes

EDIT:

So i change the sequence of the code, where i put the 2 conditions(that needs to be met) first then IF ELSE, will read the 1 condition code, it works now but i dont know why, can someone explain thanks

Please describe exactly how it does not work.

I think you have a flaw in your thinking

``````  if (strID.indexOf("00:D8:5B:4D") >= 0)
{
...
...
}
//DI KO MAPAGANA TONG PART NA TO PUTNAGINA ANONG KULANG GRR
else if ((strID.indexOf("00:D8:5B:4D") >= 0) && (digitalRead(LED_PIN) == HIGH))
{
...
...
}
else
{
...
...
}
}
``````

If the 'if' evaluates to true, the 'else if' will never be reached and hence the code in the 'else if' block of statements will never be executed (nothing wrong with that, that's what one expects). If the 'if' evaluates to false, the 'else if' will also evaluate to false so again the 'else if' block of statements will never be executed; that might not be what you want though.