This is my issue, I have a loop inside of another loop/CASE option. Arduino consistently keeps skipping/entering the loop with or with out inputs. I listed two codes because I tried to solve it in a different way but either code still skips the inner loop or doesn't execute correctly. I have searched and searched for good loop examples and can't seem to find one similar or close to what I'am trying to do. Any comments/help is greatly appreciated.
Program #1
int Red_LED = 3;
int Green_LED = 5;
void setup()
{
Serial.begin(9600);
pinMode(Red_LED,OUTPUT);
pinMode(Green_LED,OUTPUT);
Serial.println("Is the sensor placed? ");
Serial.println("type y or n ");
}
void loop()
{
if(Serial.available() > 0)
{
char Input = Serial.read();
switch(Input)
{
case'y': // User selected yes
digitalWrite(Green_LED, HIGH); // Green LED lights up saying its ready
Serial.println("Sensor is ready for use ");
Serial.println("Proceed (1)Yes or (2)NO? "); // User inputs another selection, 1 for yes and 2 for no
{
if(Input == 1) // User selected option 1
{
Serial.println("How deep would you like to dig" ); // This will eventually be another user input
}
else if(Input == 2) // User selected option 2, exiting out of system
{
Serial.println("Shutting Down");
}
else // User didnt enter 1 or 2
{
Serial.println("Incorrect Key");
}
}
break;
case'n': // User selected no for first question
digitalWrite(Red_LED, HIGH); // LED flashes
delay(100);
digitalWrite(Red_LED, LOW);
Serial.println("System Shutting Down "); // System then shuts down
break;
default: // If user did not enter correct command for first question.
Serial.println("Please enter the letter y or n ");
break;
}
}
}
OUTPUT:
Is the sensor placed? // typed y
type y or n
Sensor is ready for use // typed 1
Proceed (1)Yes or (2)NO?
Incorrect Key
Please enter the letter y or n // displays end choice for both loops at once
Program #2
int Red_LED = 3;
int Green_LED = 5;
void setup()
{
Serial.begin(9600);
pinMode(Red_LED,OUTPUT);
pinMode(Green_LED,OUTPUT);
Serial.println("Is the sensor placed? "); // First question asked for user to answer
Serial.println("y for YES");
Serial.println("n for NO");
}
void loop()
{
if(Serial.available() > 0)
{
char Input = Serial.read();
{
if(Input == 'y') // User inputs y for yes
{
Serial.println("Sensor is ready for use "); // Another user input question
Serial.println("Proceed (1)Yes or (2)NO? "); // 1 for yes, 2 for no
int Input2 = Serial.read(); // this now picks up new input
switch(Input2) // uses a switch case for options
{
case '1': // user entered 1
digitalWrite(Green_LED, HIGH); // Green LED say its ready
Serial.println("Here ..........."); // This was for a refference to see if it worked but here i want another future input
break;
case'2': // user entered 2
digitalWrite(Red_LED, HIGH); // Red means system shutdown
Serial.println("Canceled ");
break;
}
}
else if(Input == 'n') // question one, user selected no
{
Serial.println("System Shutting Down ");
}
else if(Input != 'y' || 'n' ) // user didnt input char correctly
{
Serial.println("Please enter a y for yes or n for no ");
}
}
}
}
OUTPUT: (----- lines separate the input steps)
Is the sensor placed? // input y
y for YES
n for NO
Sensor is ready for use
Proceed (1)Yes or (2)NO? // input the number 1
Please enter a y for yes or n for no // never enters second loop
The bigger problem in the first code is that when you get a 'y' and go into case 'y' you then start testing to see if the input is 1 or 2, but you haven't read any new input, so input is still 'y'. So the code skips all of that. Then the next time through loop you will read the 1 or the 2 but you won't have the 'y' to get you back into that case so the whole thing gets skipped because '1' is neither 'y' nor 'n'. So i suspect you would hit that default case there.
So I played around with it more, made the changes that Users pointed out, now I can enter the second IF statement but loops here over and over but will not enter the third IF statement. Is there a way to reset the Serial.available() so it works each time I want a input? Should I use a Serial.flush() to reset it?
Here is my code below with my output:
int Red_LED = 3;
int Green_LED = 5;
void setup()
{
Serial.begin(9600);
pinMode(Red_LED,OUTPUT);
pinMode(Green_LED,OUTPUT);
Serial.println("Is the sensor placed? ");
Serial.println("type y or n ");
}
void loop()
{
if(Serial.available() > 0)
{
char Input = Serial.read();
Serial.println(Serial.available()); //////////////////
{
// User selected yes
if(Input == 'y')
{
digitalWrite(Green_LED, HIGH); // Green LED lights up saying its ready
Serial.println("Sensor is ready for use ");
Serial.println("Proceed (y)Yes or (n)NO? "); // User inputs another selection, 1 for yes and 2 for no
Serial.println(Serial.available()); ////////////////
{
Serial.println(Serial.available()); ////////////
if(Serial.available() > 0 )
{
char Input = Serial.read();
Serial.println(Serial.available()); ///////////////////
if(Input == 'y') // User selected option 1
{
Serial.println("How deep would you like to dig" ); // This will eventually be another user input
}
else if(Input == 'n') // User selected option 2, exiting out of system
{
Serial.println("Shutting Down");
}
else if(Input != 'y' || Input != 'n')// User didnt enter 1 or 2
{
Serial.println("Incorrect Key");
}
}
}
}
else if(Input == 'n')
{
// User selected no for first question
digitalWrite(Red_LED, HIGH); // LED flashes
delay(100);
digitalWrite(Red_LED, LOW);
Serial.println("System Shutting Down "); // System then shuts down
}
else if(Input != 'y' || Input != 'n')
{
Serial.println("Please enter the letter y or n "); // If user did not enter correct command for first question.
}
}
}
}
I placed Serial.println(Serial.available()); to see the output and its always zero.
OUTPUT:
Is the sensor placed? //Pressed y
type y or n
0 // value is 0
Sensor is ready for use
Proceed (y)Yes or (n)NO? // Pressed y
0
0
0
Sensor is ready for use // this pops up again every time i hit y
Proceed (y)Yes or (n)NO?
0
0
0
Sensor is ready for use
Proceed (y)Yes or (n)NO?
0
0
So either wait for it or keep some variables so you can remember what has already been entered.
I will try the while() loop as you have stated. Now I get what your saying now, that makes perfect sense, so how would I "wait" for it? is there a command in the library for that?
That was it, Thank you so much for the crystal clear understanding.
Here's the working code
int Red_LED = 3;
int Green_LED = 5;
void setup()
{
Serial.begin(9600);
pinMode(Red_LED,OUTPUT);
pinMode(Green_LED,OUTPUT);
Serial.println("Is the sensor placed? ");
Serial.println("type y or n ");
}
void loop()
{
if(Serial.available() > 0)
{
char Input = Serial.read();
if(Input == 'y')
{
digitalWrite(Green_LED, HIGH); // Green LED lights up saying its ready
Serial.println("Sensor is ready for use ");
Serial.println("Proceed (y)Yes or (n)NO? "); // User inputs another selection, 1 for yes and 2 for no
while(Serial.available() == 0 );
{
char Input = Serial.read();
if(Input == 'y') // User selected option 1
{
Serial.println("How deep would you like to dig" ); // This will eventually be another user input
}
else if(Input == 'n') // User selected option 2, exiting out of system
{
Serial.println("Shutting Down");
}
else if(Input != 'y' || Input != 'n')// User didnt enter 1 or 2
{
Serial.println("Incorrect Key");
}
}
}
else if(Input == 'n')
{
//case'n': // User selected no for first question
Serial.print("just after else statement \n");
digitalWrite(Red_LED, HIGH); // LED flashes
delay(100);
digitalWrite(Red_LED, LOW);
Serial.println("System Shutting Down "); // System then shuts down
}
else if(Input != 'y' || Input != 'n')
{
Serial.println("Please enter the letter y or n ");
}
}
}