switch case inside arduino loop function

Dear Sir/Mam,

I am trying to use switch case inside arduino void loop() function and trying to get the input through serial monitor. The code is posted below.

But i do not see anything in the serial monitor. If i use delay it prints the below statement continuously.
According to this code this should print the lines only once and it should wait till I enter the choice through serial monitor.

please help me finding the solution.

int ch;

void setup()
{
   Serial.begin(9600);
}

void loop()
{
   Serial.println("Please select the option:");
    Serial.println("1. Channel Details,        2. Modify Channel Parameters");
    Serial.println("3. Start Frequency Sweep,  4. Exit                     ");
    
    if(Serial.available())
    {
     int ch = serial.read();
 
        switch(ch)
       {

       }
   }
   //delay(1000);
}

But i do not see anything in the serial monitor.

Well, duh. Don't you think a call to Serial.begin(), in setup(), might be a good idea?

Hi,

I did use serial.begin(9600); in the code but still; its not working. It was my mistake not to mention serial.begin() while posting the code.

sorry for the mistake.

Thanks

anurag2508:
But i do not see anything in the serial monitor. If i use delay it prints the below statement continuously.
According to this code this should print the lines only once and it should wait till I enter the choice through serial monitor.

No it should not. There is nothing in your code to prevent the lines repeating.

If you want to wait until the user enters a character then you need

while (Serial.available() == 0) {
}

...R

Hi,

THank you for your reply. Well initially I want to print three lines as it is in the serial monitor.
and yes I want to wait till user enters the choice through serial monitor and that condition is already mentioned in the code.

The thing it is not printing anything on serial monitor till i put delay in the code.

void setup()
{
  Serial.begin(9600);
}
void loop()
{
// This three lines should be printed as it is in the serial monitor//

   Serial.print("Please select the option:");
   Serial.print("1. Channel Details,        2. Modify Channel Parameters");
   Serial.print("3. Start Frequency Sweep,  4. Exit                     ");

// then it should wait for user to enter through monitor

   if(Serial.available())
   {
       int ch = serial.read();
       switch (ch)
       {
       }
   }
}

sorry for the mistake.

You've made several mistakes. The first was posting some crap that was NOT the code you were actually executing.

The second was replacing the code in your original post AFTER I pointed out that it was wrong.

I'm done trying to help you.

Sir,

Its a misunderstanding and my mistake, Thats the only code which i am executing.
Actually i directly typed the code here and i did not copy paste it directly from my arduino IDE.
It s not a crap code its the real code on which i am working.

I forgot to type serial.begin(9600) while typing the code in the forum. I noticed it when u pointed that mistake in that code. In my arduino ide code serial.begin(9600); is already initialized. because i typed it i did not notice it.

I really need the problem to be identified thats y i typed the code in the forum in hurry.

I did not do it intentionally and I also apologized for that. If that was the real mistake I would have already corrected it in the code and I would have never corrected the code which i have posted. so please let me know the problem.

Thank you

Put the line that was posted in reply#3 just before the if statement.

Be aware that that will block forever if you never send anything which might interfere with other things that you want to do.

Robin wrote a 'serial input basics' tutorial that might give you some ideas; do a search on this forum, I don't have the link at hand.

I suggest that you post a copy of the actual code that you are using. Auto Format it in the IDE before posting to make it more readable.