user69621: Hi Im very new to coding and I'm struggling to fix a problem. I want the user to input a yes or no answer to a question and based on the answer I want the right text to show on the serial monitor and a LCD as well as the right LED light to light up. However it seems like the if and else if statements are ignored and no matter what the user input is the last else statement is the only thing that shows up.
My second problem is with the switchLead. The LED that shows the condition of the switchLead does not turn off when the switchLead is open.
This is just school work so it has to be basic. Please let me know where I made a mistake! Also this is my first post so let me know how I could make this more clear. Thanks!
#include <Wire.h>    
#include <LCD.h>   
#include <LiquidCrystal_I2C.h>     
                                          
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  
                                                                                           
                            
String input;                   // Declate string variable to hold user input
                              
const int whiteLead = 2;         
const int switchLED = 13;        
const int RED1 = 9;              
const int YELLOW1 = 8;          
const int GREEN1 = 7;           
                     
int switchLead = 0;    
                       
void setup() {                   
  Serial.begin(9600);          // Serial transmission activated baud rate 9600 bits per sec 
  lcd.begin(16,2);             // Initialise I2C LCD 16 columns with 2 rows
  lcd.backlight();         
                           
pinMode(whiteLead,INPUT);      
pinMode(switchLED,OUTPUT);     
pinMode(RED1,OUTPUT);          
pinMode(YELLOW1,OUTPUT);       
pinMode(GREEN1,OUTPUT);        

switchLead = digitalRead(whiteLead);       // Read the state of the switchLead value
if (switchLead == HIGH) {                  // Begin if; if whiteLead is open do;
  digitalWrite(switchLED, LOW);            
  digitalWrite(RED1,LOW);                 
  digitalWrite(YELLOW1,LOW);              
  digitalWrite(GREEN1,LOW);                

}                                          // End if
else {                                     // Begin else; Check if the switchLead is closed, turn LED ON
  digitalWrite(switchLED, HIGH);           // Turn switch LED ON
}                                          // End else
}                                          // End void setup
                                 
void loop() {                               // Void loop begins
  Serial.println("Is the room in use? ");   //Prompt user to input answer
  while (Serial.available()==0) {}          // Wait for user input
  input = Serial.readString();              // Read user input and store it in variable input

   if (input == "Yes") {               // Begin if: if user input is yes 
    
    lcd.setCursor(0,0);               
    lcd.print("In use");               
    Serial.print("The room is ");      
    Serial.println("in use");          
    digitalWrite(switchLED,HIGH);      
    digitalWrite(RED1,HIGH);         
    digitalWrite(GREEN1,LOW);          
    digitalWrite(YELLOW1,LOW);         
  }
  else if (input == "No") {           
  
    lcd.clear();                      
    lcd.setCursor(0,0);               
    lcd.print("Empty");               
    Serial.print("The room is: ");    
    Serial.println("empty");          
    digitalWrite(switchLED,HIGH);     
    digitalWrite(GREEN1,HIGH);        
    digitalWrite(RED1,LOW);           
    digitalWrite(YELLOW1,LOW);        
  }
    else {                                // Begin else

    lcd.clear();                        // Clear LCD display
    lcd.setCursor(0,0);                 // Set LCD cursor to column 0, row 0
    lcd.print("Preparing");             // Print message enclosed in brackets on LCD
    Serial.print("The room is ");       // Print message enclosed in brackets to computer monitor
    Serial.println("being prepared");   // Print message enclosed in brackets to computer monitor
    digitalWrite(YELLOW1,HIGH);         // Turn YELLOW1 ON
    digitalWrite(GREEN1,LOW);           // Turn GREEN1 OFF
    digitalWrite(RED1,LOW);             // Turn RED1 OFF   
  }                                     // End else
}                                       // End void loop


user69083: Try to use this command
input = Serial.readStringUntil('\n');

Instead of input = Serial.readString();
and also use while (Serial.available() > 0)
Instead of while (Serial.available()==0)
and where you used int switchLead = 0;
Try to use bool switchLead;
I hope these changes in the code will help you

69621: <p>Hi Im very new to coding and I'm struggling to fix a problem. I want the user to input a yes or no answer to a question and based on the answer I want the right text to show on the serial monitor and a LCD as well as the right LED light to light up. However it seems like the if and else if statements are ignored and no matter what the user input is the last else statement is the only thing that shows up.</p>
<p>My second problem is with the switchLead. The LED that shows the condition of the switchLead does not turn off when the switchLead is open.</p>
<p>This is just school work so it has to be basic. Please let me know where I made a mistake! Also this is my first post so let me know how I could make this more clear. Thanks!</p>
<pre><code>#include &lt;Wire.h&gt;    
#include &lt;LCD.h&gt;   
#include &lt;LiquidCrystal_I2C.h&gt;     
                                          
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  
                                                                                           
                            
String input;                   // Declate string variable to hold user input
                              
const int whiteLead = 2;         
const int switchLED = 13;        
const int RED1 = 9;              
const int YELLOW1 = 8;          
const int GREEN1 = 7;           
                     
int switchLead = 0;    
                       
void setup() {                   
  Serial.begin(9600);          // Serial transmission activated baud rate 9600 bits per sec 
  lcd.begin(16,2);             // Initialise I2C LCD 16 columns with 2 rows
  lcd.backlight();         
                           
pinMode(whiteLead,INPUT);      
pinMode(switchLED,OUTPUT);     
pinMode(RED1,OUTPUT);          
pinMode(YELLOW1,OUTPUT);       
pinMode(GREEN1,OUTPUT);        

switchLead = digitalRead(whiteLead);       // Read the state of the switchLead value
if (switchLead == HIGH) {                  // Begin if; if whiteLead is open do;
  digitalWrite(switchLED, LOW);            
  digitalWrite(RED1,LOW);                 
  digitalWrite(YELLOW1,LOW);              
  digitalWrite(GREEN1,LOW);                

}                                          // End if
else {                                     // Begin else; Check if the switchLead is closed, turn LED ON
  digitalWrite(switchLED, HIGH);           // Turn switch LED ON
}                                          // End else
}                                          // End void setup
                                 
void loop() {                               // Void loop begins
  Serial.println(&quot;Is the room in use? &quot;);   //Prompt user to input answer
  while (Serial.available()==0) {}          // Wait for user input
  input = Serial.readString();              // Read user input and store it in variable input

   if (input == &quot;Yes&quot;) {               // Begin if: if user input is yes 
    
    lcd.setCursor(0,0);               
    lcd.print(&quot;In use&quot;);               
    Serial.print(&quot;The room is &quot;);      
    Serial.println(&quot;in use&quot;);          
    digitalWrite(switchLED,HIGH);      
    digitalWrite(RED1,HIGH);         
    digitalWrite(GREEN1,LOW);          
    digitalWrite(YELLOW1,LOW);         
  }
  else if (input == &quot;No&quot;) {           
  
    lcd.clear();                      
    lcd.setCursor(0,0);               
    lcd.print(&quot;Empty&quot;);               
    Serial.print(&quot;The room is: &quot;);    
    Serial.println(&quot;empty&quot;);          
    digitalWrite(switchLED,HIGH);     
    digitalWrite(GREEN1,HIGH);        
    digitalWrite(RED1,LOW);           
    digitalWrite(YELLOW1,LOW);        
  }
    else {                                // Begin else

    lcd.clear();                        // Clear LCD display
    lcd.setCursor(0,0);                 // Set LCD cursor to column 0, row 0
    lcd.print(&quot;Preparing&quot;);             // Print message enclosed in brackets on LCD
    Serial.print(&quot;The room is &quot;);       // Print message enclosed in brackets to computer monitor
    Serial.println(&quot;being prepared&quot;);   // Print message enclosed in brackets to computer monitor
    digitalWrite(YELLOW1,HIGH);         // Turn YELLOW1 ON
    digitalWrite(GREEN1,LOW);           // Turn GREEN1 OFF
    digitalWrite(RED1,LOW);             // Turn RED1 OFF   
  }                                     // End else
}                                       // End void loop
</code></pre>
 69083: <p>Try to use this command</p>
<pre><code>input = Serial.readStringUntil('\n');
</code></pre>
<p>Instead of <code>input = Serial.readString();</code></p>
<p>and also use <code>while (Serial.available() &gt; 0)</code></p>
<p>Instead of <code>while (Serial.available()==0)</code></p>
<p>and where you used <code>int switchLead = 0;</code></p>
<p>Try to use <code>bool switchLead;</code></p>
<p>I hope these changes in the code will help you</p>
