Multiple If statments problem...I'm stuck :(

The code I wrote is quite simple: I want to have different text in the Serial communication depending on which LEDs are HIGH and LOW.
In this code I specified that LED2 and LED3 have to be LOW and in effect the light is off but the Serial communication still shows "text 1" , "text 2", text3" and the same time. Why!? How can it read a LED LOW and HIGH at the same time ? Thank you :frowning:

#define LED1 53               // LED1 collegato al pin digitale 53
#define LED2 51               // LED2 collegato al pin digitale 51  
#define LED3 49               // LED3 collegato al pin digitale 49
#define BUTTON 7              // pin di input dove è collegato il pulsante  
int  val = 0;                 // si userà val per conservare lo stato del pin di input

void setup() {
  Serial.begin(9600);          //inizializza la porta seriale
  pinMode(LED1, OUTPUT);       // imposta il pin digitale come output
  pinMode(LED2, OUTPUT);       // imposta il pin digitale come output
  pinMode(LED3, OUTPUT);       // imposta il pin digitale come output
  pinMode(BUTTON, INPUT);     // imposta il pin digitale come input
  pinMode(LED3, LOW);
  pinMode(LED2, LOW);
}

void loop() {
  val = digitalRead(BUTTON);  // legge il valore dell'input e lo conserva


  if ((val == HIGH) && digitalRead(LED1 == HIGH) && digitalRead(LED2 == LOW)  && digitalRead(LED3 == LOW)) {
    Serial.println("text1");    //scrive l'aiuto 1
    delay(1000);
  }
  if ((val == HIGH) && digitalRead(LED1 == HIGH) && digitalRead(LED2 == HIGH)  && digitalRead(LED3 == LOW)) {
    Serial.println("text2");   //scrive l'aiuto 2
    delay(1000);
  }
  if ((val == HIGH) && digitalRead(LED1 == HIGH) && digitalRead(LED2 == HIGH) && digitalRead(LED3 == HIGH)) {
    Serial.println("text3");   //scrive l'aiuto 3
    delay(1000);
  }
}

Instead of this:

digitalRead(LED1 == HIGH )

try this:

digitalRead(LED1)  == HIGH

6v6gt:
Instead of this:

digitalRead(LED1 == HIGH )

try this:

digitalRead(LED1)  == HIGH

Now the Serial communication stop talking, meaning that it doesn't recognize the three "if conditions". This sounds strange, the code seems quite easy but something is wrong.
Maybe saying I'm working on Arduino MEGA can say something more ?

Maybe if you posted your code. . . ?

Have you corrected all of the problems with the use of digitalRead() or only the one that was pointed out ?

Please post your complete program as it is now.

This is the full code, like it's written on IDE.

#define LED1 53               // LED1 collegato al pin digitale 53
#define LED2 51               // LED2 collegato al pin digitale 51  
#define LED3 49               // LED3 collegato al pin digitale 49
#define BUTTON 7              // pin di input dove è collegato il pulsante  
int  val = 0;                 // si userà val per conservare lo stato del pin di input

void setup() {
  Serial.begin(9600);          //inizializza la porta seriale
  pinMode(LED1, OUTPUT);       // imposta il pin digitale come output
  pinMode(LED2, OUTPUT);       // imposta il pin digitale come output
  pinMode(LED3, OUTPUT);       // imposta il pin digitale come output
  pinMode(BUTTON, INPUT);     // imposta il pin digitale come input
  pinMode(LED2,HIGH);
  pinMode(LED1,HIGH);
  pinMode(LED3, LOW);
}

void loop() {
  val = digitalRead(BUTTON);  // legge il valore dell'input e lo conserva


  if ((val == HIGH) && digitalRead(LED1 == HIGH) && digitalRead(LED2 == LOW)  && digitalRead(LED3 == LOW)) {
    Serial.println("text1");    //write text 1
   
 
  }
  if ((val == HIGH) && digitalRead(LED1 == HIGH) && digitalRead(LED2 == HIGH)  && digitalRead(LED3 == LOW)) {
    Serial.println("text2");   //write text 2
  
  
  }
  if ((val == HIGH) && digitalRead(LED1 == HIGH) && digitalRead(LED2 == HIGH) && digitalRead(LED3 == HIGH)) {
    Serial.println("text3");   //write text 3
  
  }
}

digitalRead(LED2 == LOW) && digitalRead(LED3 == LOW)

You still have these all over the place.

Extrapolate a bit :wink:
.

larryd:
digitalRead(LED2 == LOW) && digitalRead(LED3 == LOW)

You still have these all over the place.

Extrapolate a bit :wink:
.

With this one the Serial doesn't communicate

#define LED1 53               // LED1 collegato al pin digitale 53
#define LED2 51               // LED2 collegato al pin digitale 51  
#define LED3 49               // LED3 collegato al pin digitale 49
#define BUTTON 7              // pin di input dove è collegato il pulsante  
int  val = 0;                 // si userà val per conservare lo stato del pin di input

void setup() {
  Serial.begin(9600);          //inizializza la porta seriale
  pinMode(LED1, OUTPUT);       // imposta il pin digitale come output
  pinMode(LED2, OUTPUT);       // imposta il pin digitale come output
  pinMode(LED3, OUTPUT);       // imposta il pin digitale come output
  pinMode(BUTTON, INPUT);     // imposta il pin digitale come input
  pinMode(LED2,HIGH);
  pinMode(LED1,HIGH);
  pinMode(LED3, LOW);
}

void loop() {
  val = digitalRead(BUTTON);  // legge il valore dell'input e lo conserva


  if ((val) == HIGH && digitalRead(LED1) == HIGH && digitalRead(LED2) == LOW  && digitalRead(LED3) == LOW) {
    Serial.println("aiuto1");    //scrive l'aiuto 1
   
 
  }
  if ((val) == HIGH && digitalRead(LED1) == HIGH && digitalRead(LED2) == HIGH  && digitalRead(LED3) == LOW) {
    Serial.println("aiuto2");   //scrive l'aiuto 2
  
  
  }
  if ((val) == HIGH && digitalRead(LED1) == HIGH && digitalRead(LED2) == HIGH && digitalRead(LED3) == HIGH) {
    Serial.println("aiuto3");   //scrive l'aiuto 3
  
  }
}

When do you write to the LED pins?(which are outputs)

(the forum has a very active Italian section)

AWOL:
When do you write to the LED pins?(which are outputs)

What do you mean with this ?

  pinMode(LED1, OUTPUT);       // imposta il pin digitale come output
  pinMode(LED2, OUTPUT);       // imposta il pin digitale come output
  pinMode(LED3, OUTPUT);       // imposta il pin digitale come output
  pinMode(BUTTON, INPUT);     // imposta il pin digitale come input
  pinMode(LED2,HIGH);
  pinMode(LED1,HIGH);
  pinMode(LED3, LOW);
}

I wrote this: so for the logic I have to receive a ‚Äútext2‚ÄĚ in Serial but nothing happens.

This is will not do what you appear to want:
pinMode() sets a direction for a pin (INPUT, OUTPUT) only and will not assign a value HIGH to a pin.

  pinMode(LED2,HIGH);
  pinMode(LED1,HIGH);
  pinMode(LED3, LOW);

What I guess you mean is something like:

digitalWrite( LED2, HIGH ) ;
digitalWrite( LED1, HIGH ) ;
digitalWrite( LED3, LOW ) ;

Yes it is so, but

6v6gt:
This is will not do what you appear to want:
pinMode() sets a direction for a pin (INPUT, OUTPUT) only and will not assign a value HIGH to a pin.

  pinMode(LED2,HIGH);

pinMode(LED1,HIGH);
  pinMode(LED3, LOW);




What I guess you mean is something like:




digitalWrite( LED2, HIGH ) ;
digitalWrite( LED1, HIGH ) ;
digitalWrite( LED3, LOW ) ;

Yes you are right but the result doesn't change.
Can the problem be based on the fact that LED1/LED2/LED3 are OUTPUT and I'm using them for being like an INPUT condition to have a text result ????????

Maybe this?

if ((val == HIGH) && (digitalRead(LED1) == HIGH) && (digitalRead(LED2) == LOW)  && (digitalRead(LED3) == LOW))