All works fine, but if I erase the Serial.print comands from While sentence...
void setup()
{
Serial.begin(9600);
xfuntion();
}
void loop(){
}
void xfuntion(){
uint16_t nodes [10][2]={0};
uint16_t bms = 0;
uint16_t bls = 0;
bms = atResponse.getValue()[6] << 8 | atResponse.getValue()[7]; //These vars get uint8_t values
bls = atResponse.getValue()[8] << 8 | atResponse.getValue()[9];
boolean flg = true; //Example
int cnt =0;
While ("someting"){
if(cnt==0 || flg){
nodes [cnt][0] = bms;
nodes [cnt][1] = bls;
cnt ++;
}
}
}
//Trace all array to see if data was saved correctly
char aBuf [2];
for(int i=0; i<10; i++){
sprintf(aBuf,"Found : %8x %8x ",nodes [i][0],nodes [i][1]);
Serial.print(aBuf);
Serial.println();
}
When parse the array, these show me strange values that no match with the data that I save. Anyone know what´s going on?
I try to use delay(); and Serial.flush(); but I can't fix it...
I try to use delay(); and Serial.flush(); but I can't fix it...
Maybe you could try a few other irrelevant functions.
Posting code snippets is not the way to get help. What is an atResponse?
Put each { and } on it's own line, and use Tools + Auto format to make your { and } line up, and to properly indent the code. It's very hard to read code with random indenting, and even harder to fix it.
#if defined(ARDUINO) && ARDUINO > 18
#include <SPI.h>
#endif
#include <XBee.h>
XBee xbee = XBee(); //Instance Xbee object
//****** Public Member Functions from Xbee Request and Xbee Response ******//
AtCommandRequest atRequest = AtCommandRequest();
AtCommandResponse atResponse = AtCommandResponse();
void setup()
{
xbee.begin(9600);
Serial.begin(9600);
delay(5000);//Time for the radios are stabilized
Serial.print("#Nodes:");
Serial.print(nodeDiscovery());
}
void loop(){
}
//****************** Find nodes ****************//
//***********************************************//
int nodeDiscovery(){
long nodeDiscoveryTimeout = 6000; //Time to search nodes
uint8_t atCmd[] = {'N','D'};//Comando para descubrir los nodos de la red
long startTime = millis();
atRequest.setCommand(atCmd);
int cnt = 0;
uint16_t nodes [10][2]={0};
while(millis() - startTime < nodeDiscoveryTimeout){
if(sendAtCommand()){
if(atResponse.getValueLength()>=26){
boolean flg=false; //The node exists
uint16_t bms = 0;
uint16_t bls = 0;
bms = atResponse.getValue()[6] << 8 | atResponse.getValue()[7];
bls = atResponse.getValue()[8] << 8 | atResponse.getValue()[9];
if(cnt > 0){
for(int i=0; i<9; i++){
if(bms == nodes [i][0]){
flg=false;
if(bls == nodes [i][1]){
flg=false;
break;
}else{
flg=true; //The node is diferent
}
}else{
flg=true;//The node is diferent
}
}
}
/*************************************************************************************************************************************************
* Here is Issue
*************************************************************************************************************************************************/
if(cnt==0 || flg){
Serial.print("Save:");
Serial.print(String(bms,HEX));
Serial.print(String(bls,HEX));
nodes [cnt][0] = bms;
nodes [cnt][1] = bls;
//delay(200);
//Serial.flush();
Serial.print(String(nodes [cnt][0],HEX));
Serial.print(String(nodes [cnt][1],HEX));
Serial.println();
cnt ++;
}
}
}
}//End while
char aBuf [2];
for(int i=0; i<10; i++){
sprintf(aBuf,"Found : %8x %8x ",nodes [i][0],nodes [i][1]);
Serial.print(aBuf);
Serial.println();
}
return cnt;
}
/*************************************************************************************************************************************************
* Send AT Command
*************************************************************************************************************************************************/
boolean sendAtCommand() {
// send the command
xbee.send(atRequest);
// wait up max to 5 seconds for the status response
if (xbee.readPacket(2000)) {
// got a response!
// should be an AT command response
if (xbee.getResponse().getApiId() == AT_COMMAND_RESPONSE) {
xbee.getResponse().getAtCommandResponse(atResponse);
if (atResponse.isOk()){
Serial.print("response OK");
Serial.println();
return true;
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
}
As I said my problem is when I comment all the lines with "Serial.print" command inside the "if" comparison (indicated in the code) of nodeDiscovery() function, when I parse the array to check the data, show strange values, but if I uncomment the lines with "Serial.print" everything displayed correctly .
The Serial.print() function takes time to execute. How long depends on the baud rate. Having it in place allows time for something to happen. Exactly what that something is is not clear. Some Serial.print()s will be needed to see what exactly gets slowed down long enough to have desirable results.
Nothing that I see is dependent on time, though. Sending and receiving complete packets should be a function of the library sending/receiving end of packet markers, not time.
You have aBuf declared as a 2 byte character array, but you try and fill it with a lot more than 2 bytes in the sprintf() call. That means you are overwriting other variables and potentially the stack. You'll need to make aBuf bigger and I recommend that you use snprintf() to be safe from overwriting the end of the array. E.g.