Break out of do/While loop if value found

I have the following code that reads the signal strength from a SIM800L GSM modem.
The code works and receives the data from the SIM800L.
However, I must break from the do/While loop if the signal strength (value) is greater than 0.

void getSignal() {

  unsigned long start = millis ();
  uint8_t signal = sim800l->getSignal();
  do {
    delay(1000);
    signal = sim800l->getSignal();
    mySignal = signal;
//    if (mySignal > 0) {
//      break;
//    }
  }  while (millis () - start <= 10000);// && (mySignal <= 0));

  Serial.println("CSQ Timeout");
  if (mySignal <= 0) mySignal = 21;
  sprintf(txSignal, "%02d", mySignal);
  Serial.print(F("txSignal: "));
  Serial.println(txSignal);
}

The commented section of code does not work:

//    if (mySignal > 0) {
//      break;
//    }

If the value is <= 0, I then insert a value of 21.
However, the code always times out and inserts the value of 21, even though a valid value has been received from the SIM800L modem.

The Log from the above code illustrates this:

SIM800L : Send "AT+CSQ"
SIM800L : End of transmission
SIM800L : Receive "
+CSQ: 0,0
"
SIM800L : Send "AT+CSQ"
SIM800L : End of transmission
SIM800L : Receive "
+CSQ: 29,0
"
SIM800L : Send "AT+CSQ"
SIM800L : End of transmission
SIM800L : Receive "
+CSQ: 29,0
"
SIM800L : Send "AT+CSQ"
SIM800L : End of transmission
SIM800L : Receive "
+CSQ: 29,0
"
SIM800L : Send "AT+CSQ"
SIM800L : End of transmission
SIM800L : Receive "
+CSQ: 30,0
"
SIM800L : Send "AT+CSQ"
SIM800L : End of transmission
SIM800L : Receive "
+CSQ: 30,0
"
SIM800L : Send "AT+CSQ"
SIM800L : End of transmission
SIM800L : Receive "
+CSQ: 30,0
"
SIM800L : Send "AT+CSQ"
SIM800L : End of transmission
SIM800L : Receive "
+CSQ: 30,0
"
SIM800L : Send "AT+CSQ"
SIM800L : End of transmission
SIM800L : Receive "
+CSQ: 30,0
"
SIM800L : Send "AT+CSQ"
SIM800L : End of transmission
SIM800L : Receive "
+CSQ: 30,0
"
SIM800L : Send "AT+CSQ"
SIM800L : End of transmission
SIM800L : Receive "
+CSQ: 30,0
"
CSQ Timeout
txSignal: 21

I cannot see what I am doing incorrectly.

Since signal is unsigned, it can never be less than zero.
(can't see what mysignal is, but uint* or just "int" will never be less than zero either.)

This is what I have:

int mySignal;

Right. So if "signal" is 255 (-1), mySignal will also end up 255 (not less than zero.)

Try:

   if ((int8_t)signal < 0)
     break;

(or - why do you have "signal" as uint8_t in the first place?)

if you want to reinterpret "signal" as signed.

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