i was looking for a way to synchronize 3 arduino with HC-12 radio module so i came across this way :
i have a main arduino that collect the sensors data from the other 2 arduino
1)the main arduino first send a byte contain the address of the arduino that should send it is sensors data
2)both of the two arduino recieve that message and compare it with its address. then the one with matching address send a string containing sensors data finished with '\n'
3)the main arduino recieve data and read it until '\n' character.
4)when a '\n' char is recieved main arduino send a byte contain the address of the other arduino that should send it is sensors data
5)both of the two arduino recieve that message and compare it with its address. then the one with matching address send a string containing sensors data finished with '\n'
6)the main arduino recieve data and read it until '\n' character.
finally repeat the process during the loop().
i translate my approach to arduino program.
this is for the main arduino
#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin
void setup() {
Serial.begin(9600); // Serial port to computer
HC12.begin(9600); // Serial port to HC12
}
void loop() {
String node1="";
String node2="";
byte empty,c;
//emptying the hc12 buffer
while(HC12.available()){
HC12.read();
}
HC12.write((byte)1);//send a byte contain the address of the first arduino that we want it to return its sensors data
do{
if(HC12.available()){
c=HC12.read();
if(c!='\n'){node1+=(char)c;}
}
}while(c!='\n');//i think this do{...} while(...) is too mutch to explain in comment but with a little patient you can understand
Serial.println(node1);//here we get our node1 variable containing result from the arduino with address 1
c=0;
HC12.write(2);//send the address of the second arduino
do{
if(HC12.available()){
c=HC12.read();
if(c!='\n'){node2+=(char)c;}
}
}while(c!='\n');//i think this is do{...} while(...) is too mutch to explain in comment but with a little patient you can understand
Serial.println(node2);;//here we get our node1 variable containing result from the arduino with address 1
//delay(2000);//i dont use delay() intentionlly but i plan to use it in the future
}
for the other arduino
#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin
void setup() {
Serial.begin(9600); // Serial port to computer
HC12.begin(9600); // Serial port to HC12
}
void loop() {
byte addr;
if(HC12.available()){
addr = HC12.read();
if(addr==1){
HC12.write("temperature:20 humidity:40 co2:3 \n");//i just return a string to the main arduino for testing purpose
}
}
//delay(10);
}
and for the last arduino the same, just replace if(addr==1) by if(addr==2)
so i tested the code and it gives me a good and proper result(the result i want and expected it) on the serial and i am not using delay() and synchronisation is good. so my question is how to improve my code or approach? is there a better approach?what are the changes required to make the approach respectively the code better?