Looking for a "Waiting for Serial.read()-Input"-Solution

Hi,

i want to establish a menu with selectable funcitons/options. The code below is the first try. But on this code, if I'm not fast enough to send a number from console via serial to the arduino, the program stops. Yeah, I know WHY the program stops, but I'm missing a good solution on "waiting for serial" at the moment.

Does anybody knows a proper solution? Thanks for replies toonlumberjack

#include "Wire.h"
#define bl03_address 0x0B

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

 print_menu();
 menu_choice();
}

void print_menu()
{
 Serial.println("BL03");
 Serial.println("--------------------");
 Serial.println("(1) #work in progress#");
 Serial.println("(2) #work in progress#");
 Serial.println("(3) #work in progress#");
 Serial.println("--------------------");
 Serial.println();
}

int menu_choice()
{
  int serialData=0;
  int menupoint;
  do
  {
     serialData = Serial.read();
    if(isdigit(char(serialData)))
    {
     menupoint=serialData ;
    }
  }while(serialData!=char(13) && (Serial.available() >0));

   delay(2000);
   Serial.println(menupoint);

      switch (menupoint) {
        case '1': Serial.println("1*"); return 0x08; break; 
        case '2': Serial.println("2*");return 0x09; break;
        case '3': Serial.println("3*");return 0x0a; break;
       
         default:
          Serial.println("Das ist keine Option!");
          break;
      }
  

}



void loop()
{
   

 //showdata();
 Wire.endTransmission(bl03_address);
}

You might want to reorganize your sketch.

The setup function is run only once.

If you want to read the serial incoming data at any time, that should be done in the 'loop()' function. If you want to use the menu just once during startup, you should use millis() as a timeout.

You can not read serial data if it is not available. So you should first check "Serial.available()" before "Serial.read()". Your do...while loop does not do that.

Erdin: You might want to reorganize your sketch.

Definitely

The setup function is run only once.

Yeah, that was the point to put this console-menu-thing in it......

If you want to read the serial incoming data at any time, that should be done in the 'loop()' function. If you want to use the menu just once during startup, you should use millis() as a timeout.

I only want to to read the Serial Incoming data once at the start.

You can not read serial data if it is not available.

Sure I can. But it doesn't make sense XD

So you should first check "Serial.available()" before "Serial.read()". Your do...while loop does not do that.

And what is this? :

while(serialData!=char(13) && [b](Serial.available() >0)[/b]);

Thanks for the reply

And what is this? :

It is the termination of a do ... while structure. So the code in the structure is run BEFORE coming to this therefore you are reading data before any has arrived.

Have a read of this:- http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=11425&page=1

Okay, you can read serial data without checking if something is available. But you bring something in your code that is wrong. If there is no reason for that, try to avoid it.

// old code
do
  {
     serialData = Serial.read();
    if(isdigit(char(serialData)))
    {
     menupoint=serialData ;
    }
  }while(serialData!=char(13) && (Serial.available() >0));
// new code, not tested
while (Serial.available() >0)
{
  serialData = Serial.read();

  if( isdigit (serialData))
  {
    menupoint = serialData;
  }
  else if (serialData == '\r' )   // Carriage Return
  {
    break;
  }
}

If you don't like the 'break', than I could rewrite it. The while loop stores the last binary number of a line of text in 'menupoint'. Is that what you want ? If the text contains '0' or '1', or '3', it is not captured, since you accept only binary numbers, are you sure you want to do that ? What if the text has a LineFeed ? Or no CarriageReturn and no LineFeed ?

This is only about the while-loop. Waiting during a few seconds at startup is something else. Use millis() as I wrote. http://arduino.cc/en/Reference/Millis