Simple problem with serial loop

Below I have a really simple code that is supposed to loop and delay while no data is sent. Once data is sent however, the code should leave the while loop and print. However, this does not happen at all!

What did I do wrong? Thanks!

void setup() 
{ 
  Serial.begin(9600); 
  
  // prints title with ending line break 
  Serial.println("ASCII Table ~ Character Map"); 

  // wait for the long string to be sent 
  delay(100); 
} 

int number = 33; // first visible character '!' is #33 

void loop() 
{ 
   // prints value unaltered, first will be '!' 
  while(Serial.available()==0){
    delay(100);
  }
Serial.print("AT+BTRNM=123456789ABC");   
  delay(100); // allow some time for the Serial data to be sent 
}

The fact that there is something inside:

while ( Serial.available() == 0 ) { delay(100); }

means that it will delay(100); then leave the while loop. So you need to just leave it blank and it should just loop nothing while no serial data is available.

/me

lzhang--

Your logic seems quite reasonable. Do you see the "ASCII Table ..." message?

If you are connecting using the Arduino serial monitor, be advised that it does not actually send anything until you press Enter.

Good luck.

Mikal

If I understand it right, the Serial.available () returns -1 if no data has been received, not 0. If data is received it returns a number greater than zero. So you should put

while(Serial.available()==-1){ delay(100); }

Another note, I think once it leaves the loop it will not enter it again, because it still has the incoming data in memory. I don't know if that is what you are looking for, but if you want it to enter the loop again you should put a serial flush below it to erase the old incoming data.

If I understand it right, the Serial.available () returns -1 if no data has been received,

Wrong.

[

Serial.available()

](http://arduino.cc/en/Serial/Available) returns the number of bytes available in the buffer. If the buffer is empty, that would be 0.

-j

http://arduino.cc/en/Serial/Available