Here is my loop() function. The sender (a Bluetooth app on my tablet) sends a single character (any character) to tell the Arduino that serial data is to follow. That character and any others that happen to be in the serial buffer are discarded and the mode set to serial receive mode. The serial data is read, parsed and acted upon then the mode is set back to 0 to enable the show function.
void loop()
{
if (mode == 0) // show mode
{
showMiles();
showDP(dpColor);
if (bt.available()) // if a character has come in
{
// clear serial input buffer
while (bt.available())
{
// read and throw away the alert character(s)
bt.read();
}
mode = 1; // set the serial input mode
}
}
else if (mode == 1) // read serial mode
{
if (setupMsg == 0) // send setup message if it hasn't been sent
{
Serial.println("Entered set up mode");
bt.println("Entered set up mode");
setupMsg = 1; // setup message sent
}
recvWithStartEndMarkers(); // get the serial data
if (newData == true)
{
parseData(); // parse and act upon serial data
// the parseData function will set mode back to 0
// when all serial is received and processed
}
}
}
//==================== loop
For the methods that I use to read the serial data see the serial input basics tutorial.