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);
}
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
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)
{
}
}
}
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.