Do....while loop not working

I am using the do/while loop in my Bluetooth parsing routine.
I read the Serial for incoming BT data, parse this data to obtain the Beacon MAC address.
BT device is set to Scan for 5 seconds (I know this is a long interval, but will be refined later).
If a valid MAC address is obtained I set "scanSuccess = true" and break to stop scanning.
If a valid MAC address is NOT obtained, I rescan 3 times "MAX_scanCOUNT".
However, this is not working and I have been banging my head for days.
Can anyone please look at my code and advise as to what I am doing incorrectly.
Code:

void bluetoothScan() {

  digitalWrite(PINS.BT_RST, HIGH); // Reset Bluetooth
  delay(100);// Delay for BT module to settle
  digitalWrite(PINS.BT_INT, HIGH); // Wake-up module - Scan does NOT start automatically
  delay(100);// Delay for BT module to settle
  BT.print("AT+SCAN=1,5"); // Set single scan duration to X second (Default 1 second)
  BT.println();
  delay(100);// Delay for BT module to settle

  debugMsg("[bluetoothScan]");
  char btResponse[18] = {0}; // 17 chars + null terminator
  while (BT.available()) BT.read(); // Clear all ACK logs


  const unsigned char MAX_scanCOUNT = 3;
  unsigned char scanCount = 0;
  bool scanSuccess = false;
  do {
    for (int s = 0; s < MAXIMUM_SCAN_DURATION_SEC; s++) {
      do {
        while (BT.available()) btResponse[0] = BT.read();
      }
      while (btResponse[0] != '+'); // Wait for response message
      for (unsigned char c = 0; c < sizeof(btResponse) - 1; c++) {
        while (!BT.available());
        btResponse[c] = BT.read(); // Capture response character
        if (btResponse[c] == '\r') btResponse[c] = 0x00; // Ignore CR
        if (btResponse[c] == '\n') break; // LF means the end of one line of response
      }

      // Following Parses to find "SCAN="
      const char* foundSCAN = "SCAN=";
      char* p;
      p = strstr (btResponse, foundSCAN);
      if (p) {
        strncpy(macBeacon, btResponse + 5, 12);
        macBeacon[12] = '\0';
        debugMsg(macBeacon);
        // We have found a Beacon
        scanSuccess = true;
        debugMsg("Beacon Found");
        break; // Do Not continue scanning if found a beacon
      } else {
        // We have NOT found a Beacon
        scanSuccess = false;
      }
    }
  } while (++scanCount < MAX_scanCOUNT && !scanSuccess);

  if(!scanSuccess) debugMsg("Beacon NOT Found");

  debugMsg("BT sleep...");
  digitalWrite(PINS.BT_INT, LOW); // Shut-down module and end scan automatically
}

Are you intending for everything to run once before the condition is checked and did you intend to have a do/for first?

do {
    for (int s = 0; s < MAXIMUM_SCAN_DURATION_SEC; s++) {
      do {
        while (BT.available()) btResponse[0] = BT.read();
      }

Sorry, I don't understand?

Look at the example of the do why loop on the reference. It doesn’t go
Do
For
Do
While

@pmagowan is telling you that your "do...while" statement is not correct.
You should carefully check every "while" and every "do...while". They are also two different things, you may not combine them.

while(hungry())
{
  eat();
}

do
{
  drink();
} while(thirsty());

If you take away the body (the functions "eat" and "drink"), then you must be very careful to make clear what is going on.

[ADDED] Oops, sorry pmagowan, you already explained it 5 minutes ago.

Hello Declan
Take a view here for a practical example:

Have a nice day and enjoy programming in C++ and learning.
Errors and omissions excepted.
Дайте миру шанс!

Thanks all, I am now getting very confused.
If I look at:

  const unsigned char MAX_COUNT = 3;
  unsigned char scanCount = 0;
  bool scanSuccess = false;
  do {
    /*
      SCAN ROUTINE
"scan_success_condition" is the condition of receiving a valid MAC address
If scan_success_condition = true then we break to stop scanning.
    */
    if (scan_success_condition) scanSuccess = true;
if (!scan_success_condition) scanSuccess = false;
    } while (++count < MAX_COUNT && !scanSuccess);
  /*
    Post-scan processing routine
    (We can determine if it was successful from the "scanSuccess" variable)
If scan_success_condition = false and we have rescanned "MAX_COUNT"
we do something......
  */

I need to get my scan routing which includes the "MAXIMUM_SCAN_DURATION_SEC" in the do...while loop.

Look at the examples for loops in the arduino Reference (as above). You need to read through your code taking the examples as a guide. Make the syntax the same.

Try writing out what you want to do in plain human language before converting it to code

Eg
I want it to do “whatever”
But only if “this happens”

Or I want it to do “whatever”
Over and over again
While “this other thing” is true

Read code with the idea that it is linear and repeats over and over again very fast. A for or while loop will just make it repeat a small section until a condition is met

MAX_scanCOUNT = 3
MAXIMUM_SCAN_DURATION_SEC 5
unsigned char scanCount = 0;
const char* foundSCAN = "SCAN=";
bool scanSuccess = false;

What I need to achieve:
Start scanCount (MAX_scanCOUNT = 3)
Start scan duration (MAXIMUM_SCAN_DURATION_SEC 5)
Read Serial - while scan duration has not expired
If foundScan=true during scan duration > set scanSuccess=true > break to stop scan
If foundScan=false and scan duration has expired > increment scanCount
Rescan
If after 3 scanCount and foundScan is still false > set scanSuccess=false
Once scanCount has reached MAX_scanCOUNT
if (scanSuccess) debugMsg("Beacon Found);
do something.......
if (!scanSuccess) debugMsg("Beacon NOT Found);
do something.......

Happy to buy someone a beer :person_cartwheeling:

if by "scan" you mean check for input from the Bluetooth serial interface, why limit when/how long to check for input from that interface?

void loop ()
{
    if (BT.available())  {
        int n = BT.readBytesUntil ('\n', buf, sizeof(buf));

        // process BT input
        .
        .
        .
    }

    // other things to do in loop
    .
    .
    .
}

input received outside some window can be ignored

I must limit the scan period - if I don't, and never receive a valid scan, it will scan forever

what is the harm in checking if something is "available" from bluetooth? is there any harm in reading that input and ignoring it?

sound like something must be received within some period of time? this doesn't mean you shouldn't monitor for input (i.e. "scan")

many communication protocols expect a response within some period of time, an acknowledgement and take some action when it "times out"

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.