HC-12 wireless 2 second Latency

I am working on an application where it would be great to be able to have two Arduino Nanos talk to each other with as little delay as possible, as well as be able to read follow up transmissions quickly.
I am using an HC-12 wireless kit. I have been trying all sorts of different code and so far none has shown less than about 2 seconds latency between transmit and receive.

I have tried very basic code from tutorials and each is the same
Here is an example of one try,

Transmit:

int  test = 1;

void setup() {
  Serial.begin(1200);
}

void loop() {

  char response[3] = "ML";
Serial.write(response);

  delay(5000);
}

Receive

char test[3];

void setup() {
    Serial.begin(1200);
    pinMode(LED_BUILTIN, OUTPUT);

    Serial.setTimeout(50);
}

void loop() {
    if(Serial.available() > 0)
    {
        int test = Serial.read();
        if(test == "ML") ;
        {
            digitalWrite(LED_BUILTIN, HIGH);
            delay(5);
            digitalWrite(LED_BUILTIN, LOW);
            Serial.println("There");
        }
    }

}

I only need to transmit 3 numbers or letters, nothing crazy, help?

Hello scbeanie

Welcome to the world's best Arduino forum ever.

Take a view to gain the knowledge:

Yeah, I spotted several crazy things.

You didn't say what kind of Nano you are using. There are many models of Nano these days with different chips, capabilities and features. I will assume you are using "classic" Nano 3 (atmega328).

Your code is using Serial.write(), Serial.read(), Serial.println(). These functions read and write to the PC/laptop though the USB port, not to your HC-12 modules, and are used for communicating with Serial Monitor or other apps on the PC and for uploading your code. Pins 0, 1 on Nano are involved in this process, and so are not available for connecting other components to, including your HC-12 modules. If you connect your HC-12 modules to pins 0, 1, the signals will interfere with the Nano's USB-serial chip.

You should connect your HC-12 modules to 2 other pins on the Nano and use the Software Serial library to create a serial port with those pins.

Serial.read() reads only one character. Your int variable will be assigned the ASCII code for the character that is read.

Several mistakes in this line.

You seem to be attempting to compare an int to a string. In C language, no implicit conversion will be done to give this a chance of succeeding, so they will never match.

You have a global string variable called test, and a local int variable called test. It is the local variable that will be used here.

Even if the global string variable test were to be used, you cannot use "==" to compare strings in C, you must use strcmp() function.

Finally, the ";" at the end of this line marks the end of the if-statement. The lines of code that follow, inside the { and } will be executed unconditionally.

Given you have made several common beginner C coding and Arduino wiring mistakes, I think any conclusions you have drawn about the performance of HC-12 should be put aside until your code and circuit is corrected and you can re-test your hypothesis.

Thanks for your input, I am indeed using atmega328, although it is a cheap Chinese knock off version.
I should have specified that the sketch works just fine and does an intended and is only there for testing, one of many I have tired. I have tried several others versions of sketches trying to do the same thing, transmit any data via the HC-12.

Even just a straight, Serial.write() to a Serial.read(), which works, it is just a 2 second latency. I have tried Serial.print, Serial.println, I have tried adding a carriage return and of course reducing the serial time out to 50ms.

How I have the PCB board made I can use Serial TX, RX for the HC12 without an issue of interfering with uploading new sketches. I was using string to transmit everything and using Software serial after a while would cause issues, I am hoping to go away from using strings and then hopefully can get back to using software serial.

I have tired using a wire to connect to the TX / RX pins and there is no delay, so for some reason (am sure its simple) using the HC-12 adds about 2 seconds.

I should add, the is even seen on the TX / RX lights on the unit.

Hello scbeanie

Make a cross check without using the radio modules.

Please post the latest code which has fixed the errors pointed out in post #3.

I tried that, there is no latency when the TX and RX are connected directly to each other via a wire.

Here is another sketch I tried.
Transmit:

int stuff = 1 ;
int stuff1 = 2 ;
int stuff2 = 3 ;
int stuff3 = 4 ;
int stuff4 = 5 ;
int act2 = A1;

void setup() {

//mySerial.begin(1200);
Serial.setTimeout(50);

//  pinMode(test, OUTPUT);
  pinMode(act2, INPUT);
  Serial.begin(1200);
}

void loop() {

  //while (digitalRead(test) == HIGH) {

  Serial.write(stuff);  //
   Serial.write(stuff1); 
   //Serial.write('\r'); //
   delay(2500);
    Serial.print(stuff2);  //
     Serial.write(stuff3);  //
       Serial.write(stuff4);  //
  //mySerial.write(000);  
  //Serial.write('\r');//
  delay(2500);

}

Receiver


#include <SoftwareSerial.h>
#include <Servo.h>

int incomingByte;
int incomingByteString;
int incomingByte2;
int incomingByteOld;


int ledWhite = 3 ;
int ledW = 4 ;

Servo S1;
void setup() {

Serial.setTimeout(50);


  pinMode(ledWhite, OUTPUT);
  pinMode(ledW, OUTPUT);
  

      Serial.begin(1200);
    //  mySerial.begin(1200);
 
}

void loop() {


//Serial.println("Sup"); 
if (Serial.available() > 0)     { Serial.println("Serial There"); 
//timer =  millis() ;
digitalWrite(ledWhite, LOW);

      incomingByteString =  Serial.read();//mySerial.parseInt(); //
      Serial.println(incomingByteString); 
digitalWrite(ledWhite, HIGH);

}
}

The issue is, I can't seem to get the latency less than 2 second, maybe this is a settings / coding issue or maybe this is just a HC-12 thing? Or maybe its a cheap Chinese knock off nano thing?

Also, the latency shows on the TX / RX lights on the device.

What are the settings for the HC12. FU2 has some delays. What do you see in the factory default mode?

https://www.elecrow.com/download/HC-12.pdf

They are in FU4 mode

No such mode AFIK. Default if FU3.