Pages: [1]   Go Down
Author Topic: Simple problem with serial loop  (Read 459 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 28
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Code:
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
}
Logged

Keynsham, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 224
I like for loops and walks on the beach,
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

"C++ : Where friends have access to your private members." - Gavin Russell Baker

Austin, TX USA
Offline Offline
God Member
*****
Karma: 5
Posts: 997
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: April 19, 2009, 08:05:58 am by Juanelm » Logged

0
Offline Offline
Faraday Member
**
Karma: 8
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Wrong.

Serial.available() returns the number of bytes available in the buffer.  If the buffer is empty,  that would be 0.

-j

Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: