1

I have this function in my code and works fine (it returns a valid value, I put this value into a int variable outside function), but when end the function (or continue the code), my sketch stop/die...

///FUNCION PARA RSSI XBEE///
byte rss() {  ///PARA RSSI
union {byte B; char C;} atCmd[3];
AtCommandRequest atCmdReq;
AtCommandResponse atResp;
byte respLen, *resp, dBm;
strcpy(&atCmd[0].C, "DB");
atCmdReq = AtCommandRequest(&atCmd[0].B);
atResp = AtCommandResponse();
xbee.send(atCmdReq);
if (xbee.readPacket(5000)) {
   if (xbee.getResponse().getApiId() == AT_COMMAND_RESPONSE) {
       xbee.getResponse().getAtCommandResponse(atResp);
       if (atResp.isOk()) {
           respLen = atResp.getValueLength();
           if (respLen == 1) {
               resp = atResp.getValue();
               dBm = resp[0];
                  return dBm; 
           }
           else {
             //  Serial.println("Unexpected response");
           }
       }
       else {
         //  Serial.println("ERROR");
       }
   }
   else {
     //  Serial.println("Unknown response");
   }
}
else {
 //  Serial.println("No response");
}
}///fin rss()

¿Any tips?

1 Answer 1

1

Your function is only returning a value when everything works OK. If an error occurs, the problem is reported to serial but it doesn't return anything. It seems likely that this could cause stack corruption which could make the program stop.

A simple solution would be to put return 0; (or some other number) just before the end of the function.

5
  • Thank you Peter, I try to end the function with "return 0", but doesn't work... Mar 9, 2015 at 18:05
  • I need to erase the first "return" ? Mar 9, 2015 at 18:08
  • @xavi No, you still need that return statement where it is. The additional return statement is to handle situations where the first one doesn't get executed. In C++, functions must execute a return statement unless their return type is void. Mar 9, 2015 at 21:05
  • Wow! Nice explication! I try to make the function void, but still not working... thanks for four time! ...I can´t vote up (I have insuficient range), but +10 for you! Mar 9, 2015 at 21:31
  • Yeah! Now I can vote up for you! Thanks, you rule! Aug 17, 2015 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.