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
}
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.
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.