wireless communication

Hello Dears…
I tried to use 2 HC-11 modules to achieve a wireless transmission between two channels, but the results were not as I expect, sometimes its work fine but most the time there is no data transferred in between them, that is happening in a random way, can you advice me with a solution for this problem I suffer from.

QAlhawari:
can you advice me with a solution for this problem I suffer from.

As you have not posted the programs that you are having a problem with …

…R

PS … Please post your program using the code button </> so it looks like this. See How to use the Forum. It makes it much easier for people to help you

I used the same simple code for the two parts.

hc-11.ino (605 Bytes)

This is not going to work very well

Serial.write(HC12.read());      // Send the data to Serial monitor
    if(HC12.read()=='a')

because if the first line reads the 'a' it won't be available for the second line.

Try changing those two lines to this

   char newData = HC12.read();
   Serial.write(newData);
   if (newData == 'a')

Better still, have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

Sorry, please ignore the if sentence or take a look for the updated code again, it’s as simple as the recommended code.

hc-11.ino (491 Bytes)

And what happens when you try the shorter program?

By the way I would replace both WHILEs with IFs and allow loop() to do the repetition.

#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() {
  if (HC12.available()) {        // If HC-12 has data
    Serial.write(HC12.read());      // Send the data to Serial monitor
  }
  if (Serial.available()) {      // If Serial monitor has data
    HC12.write(Serial.read());      // Send that data to HC-12
  }
}

…R