1

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
2
  • because readString returns "Yes\n" – Juraj Oct 21 '20 at 12:11
  • tip: lcd.setCursor(0,0); is always executed ... it should be placed before the first if statement .... same with the Serial.print("The room is "); – jsotola Oct 21 '20 at 16:36
2

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

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.