I want these 2 Xbee modules communicate with each other continuously.
However, with my codes, they communicate just 2 or 3 times and stop. I really don’t know what is the problem.
Please give me some help…
These are my codes (Actually they are exactly same except for their “ID” and “initial value”).
I’m a totally novice C language programmer so my codes may look dirty. Sorry about that…
Code A
#define Xbee_ID "10"
char initial_val[] = "100"; /*Value has to be less than 4 digits*/
int other_ID[10] = {0, };
int data[10] = {0, };
int number_of_ID = 1;
bool checker(int array_input[], int number);
int reproduce_data(int data[], int number);
void trans_till_get(int value, int ID);
void setup() {
/*Start Serial communication and set initial value*/
other_ID[0] = atoi(Xbee_ID);
data[0] = atol(initial_val);
Serial.begin(9600);
trans_till_get(data[0], other_ID[0]);
}
/*Design of transmitting data : ID(2 digits) is attached behind the float data(4 digits)
* ex) 5164/97
* data/ID
*/
void loop() {
delay(1000);
if (Serial.available()){
/****Data receiving part****/
String answer = Serial.readStringUntil('\r');
if (answer.length() > 6) return; // Filtering false data
char c_answer[6] = {0, };
answer.toCharArray(c_answer, answer.length()+1); /*Making input data char array*/
long i_answer = atol(c_answer); /*Reading data and making it long integer*/
int ID = i_answer % 100; /*Identify ID*/
i_answer = i_answer /100; /*Removing ID*/
/* Identifying ID or Append new ID if it doesn't exist*/
for (int i=0; i < sizeof(other_ID)/sizeof(int); i++){
if(ID == other_ID[i]){
data[i] = i_answer; /*Saving data in data[]*/
break;
}
else if(!other_ID[i]) {
other_ID[i] = ID; /*Append new ID*/
data[i] = i_answer; /*Saving data in data[]*/
number_of_ID = i+1; /*Saving number of ID*/
break;
}
}
/****Data checking part****/
if (checker(data, number_of_ID))
trans_till_get(reproduce_data(data, number_of_ID), other_ID[0]); /*Transmit data if it's checked okay*/
Serial.flush();
}
}
bool checker(int array_input[], int number){ /*Return 0 if one of contents of input_array[] is 0*/
for (int i=0; i < number; i++){
if (array_input[i] == 0) return false; }
return true;
}
int reproduce_data (int input_data[], int number){ /*Prototype: sum it all*/
int result = 0;
/****Data reproducing part****/
for (int i=0; i<number; i++){
result += input_data[i];
}
/*Initializing data set*/
for(int j=0; j<10; j++) input_data[j] = 0;
input_data[0] = result;
return result;
}
void trans_till_get(int value, int ID){
/*Transmitting data*/
Serial.print(String(value) + String(ID));
Serial.print('\r');
return;
}
Code B
#define Xbee_ID "20"
char initial_val[] = "200"; /*Value has to be less than 4 digits*/
int other_ID[10] = {0, };
int data[10] = {0, };
int number_of_ID = 1;
bool checker(int array_input[], int number);
int reproduce_data(int data[], int number);
void trans_till_get(int value, int ID);
void setup() {
/*Start Serial communication and set initial value*/
other_ID[0] = atoi(Xbee_ID);
data[0] = atol(initial_val);
Serial.begin(9600);
trans_till_get(data[0], other_ID[0]);
}
/*Design of transmitting data : ID(2 digits) is attached behind the float data(4 digits)
* ex) 5164/97
* data/ID
*/
void loop() {
if (Serial.available()){
/****Data receiving part****/
String answer = Serial.readStringUntil('\r');
if (answer.length() > 6) return; // Filtering false data
char c_answer[6] = {0, };
answer.toCharArray(c_answer, answer.length()+1); /*Making input data char array*/
long i_answer = atol(c_answer); /*Reading data and making it long integer*/
int ID = i_answer % 100; /*Identify ID*/
i_answer = i_answer /100; /*Removing ID*/
/* Identifying ID or Append new ID if it doesn't exist*/
for (int i=0; i < sizeof(other_ID)/sizeof(int); i++){
if(ID == other_ID[i]){
data[i] = i_answer; /*Saving data in data[]*/
break;
}
else if(!other_ID[i]) {
other_ID[i] = ID; /*Append new ID*/
data[i] = i_answer; /*Saving data in data[]*/
number_of_ID = i+1; /*Saving number of ID*/
break;
}
}
/****Data checking part****/
if (checker(data, number_of_ID))
trans_till_get(reproduce_data(data, number_of_ID), other_ID[0]); /*Transmit data if it's checked okay*/
Serial.flush();
}
}
bool checker(int array_input[], int number){ /*Return 0 if one of contents of input_array[] is 0*/
for (int i=0; i < number; i++){
if (array_input[i] == 0) {
return false;
}
}
return true;
}
int reproduce_data (int input_data[], int number){ /*Prototype: sum it all*/
int result = 0;
/****Data reproducing part****/
for (int i=0; i<number; i++){
result += input_data[i];
}
/*Initializing data set*/
for(int j=0; j<10; j++) input_data[j] = 0;
input_data[0] = result;
return result;
}
void trans_till_get(int value, int ID){
/*Transmitting data*/
Serial.print(String(value) + String(ID));
Serial.print('\r');
return;
}
As you can see in attached results, program quit in the middle of the process.
Expected result
[COM5]
10010
40010
110010
290010
660010
.
.
. (go on)
[COM18]
20020
30020
70020
180020
370020
1030020(an error might occur but not that important now)
.
.
. (go on)
However…
[COM5]
10010
40010
110010
290010 (quit)
[COM18]
20020
30020
70020
180020 (quit)
I’m guessing it’s a problem related to buffer but I’m not sure about this.
What would be a problem?