Go Down

Topic: Simple problem with serial loop (Read 739 times) previous topic - next topic


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: [Select]
void setup()
 // prints title with ending line break
 Serial.println("ASCII Table ~ Character Map");

 // wait for the long string to be sent

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

void loop()
  // prints value unaltered, first will be '!'
 delay(100); // allow some time for the Serial data to be sent


The fact that there is something inside:

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

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.

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



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.



Apr 19, 2009, 03:03 pm Last Edit: Apr 19, 2009, 03:05 pm by Juanelm Reason: 1
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


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,


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


Go Up