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