HC-12 communication help

Hello guys, I have a problem with a simple setup of 2 Arduino communicating with HC-12 module. The program was basically copy-pasted from the internet because I have no idea about programming. The program is basically for momentary button presses. I managed to add 3 buttons with no issues, no latency, no interference, but when I added 8 buttons there was I massive delay on the receiver end. I tried to access AT command of the HC-12 with no success to try change the baud rate, power and channel. Could you please help me? Thanks in advance.

Reciever.ino (2.46 KB)

Transmitter.ino (4.32 KB)

StreetKing17’s RX code

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

int MainLights = 13;
int EmergLight = 12;
int Motor1Forw = 11;
int Motor1Rev = 10;
int Motor2Forw = 9;
int Motor2Rev = 8;
int WaterUp = 7;
int WaterDown = 6;

void setup() {
  mySerial.begin(9600);
  pinMode(MainLights, OUTPUT);
  pinMode(EmergLight, OUTPUT);
  pinMode(Motor1Forw, OUTPUT);
  pinMode(Motor1Rev, OUTPUT);
  pinMode(Motor2Forw, OUTPUT);
  pinMode(Motor2Rev, OUTPUT);
  pinMode(WaterUp, OUTPUT);
  pinMode(WaterDown, OUTPUT);
}

void loop() {
  if(mySerial.available() > 1){    
    int input = mySerial.parseInt();//read serial input and convert to integer (-32,768 to 32,767)    
    if(input == 00){//if on code is received
      digitalWrite(MainLights, HIGH);//turn LED on
    }
    if(input == 11){//if off code is received
      digitalWrite(MainLights, LOW);//turn LED off
    }
     if(input == 22){//if off code is received
      digitalWrite(EmergLight, HIGH);//turn LED on
    }
    if(input == 33){//if off code is received
      digitalWrite(EmergLight, LOW);//turn LED off
    }
    if(input == 44){//if on code is received
      digitalWrite(Motor1Forw, HIGH);//turn LED on
    }
    if(input == 55){//if off code is received
      digitalWrite(Motor1Forw, LOW);//turn LED off
    }
    if(input == 66){//if on code is received
      digitalWrite(Motor1Rev, HIGH);//turn LED on
    }
    if(input == 77){//if off code is received
      digitalWrite(Motor1Rev, LOW);//turn LED off
    } 
    if(input == 88){//if on code is received
      digitalWrite(Motor2Forw, HIGH);//turn LED on
    }
    if(input == 99){//if off code is received
      digitalWrite(Motor2Forw, LOW);//turn LED off
    }
    if(input == 100){//if on code is received
      digitalWrite(Motor2Rev, HIGH);//turn LED on
    }
    if(input == 200){//if off code is received
      digitalWrite(Motor2Rev, LOW);//turn LED off
    } 
    if(input == 300){//if on code is received
      digitalWrite(WaterUp, HIGH);//turn LED on
    }
    if(input == 400){//if off code is received
      digitalWrite(WaterUp, LOW);//turn LED off
    }
    if(input == 500){//if on code is received
      digitalWrite(WaterDown, HIGH);//turn LED on
    }
    if(input == 600){//if off code is received
      digitalWrite(WaterDown, LOW);//turn LED off
    }
  }
  mySerial.flush();//clear the serial buffer for unwanted inputs     
  
  delay(30);//delay little for better serial communication
}

StreetKing17’s TX code

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); //RX, TX

int MainLights = 6;
int EmergLight = 7;
int Motor1Forw = 8;
int Motor1Rev = 9;
int Motor2Forw = 10;
int Motor2Rev = 11;
int WaterUp = 12;
int WaterDown = 13;
boolean onOff1 = 0;
boolean onOff2 = 0;
boolean onOff3 = 0;
boolean onOff4 = 0;
boolean onOff5 = 0;
boolean onOff6 = 0;
boolean onOff7 = 0;
boolean onOff8 = 0;
void setup() {
  pinMode(MainLights, INPUT);
  pinMode(EmergLight, INPUT);
  pinMode(Motor1Forw, INPUT);
  pinMode(Motor1Rev, INPUT);
  pinMode(Motor2Forw, INPUT);
  pinMode(Motor2Rev, INPUT);
  pinMode(WaterUp, INPUT);
  pinMode(WaterDown, INPUT);
  mySerial.begin(9600);
}

void loop() {  
  int buttonState1 = digitalRead(MainLights);//read button state
  
  if(buttonState1 == 1){//if button is down
    mySerial.println(00);//send unique code to the receiver to turn on. In this case 1111
    onOff1 = 1;//set boolean to 1
  }
  if(buttonState1 == 0 && onOff1 == 1){//Verifier to send off signal once
    mySerial.println(11);//send unique code to the receiver to turn off. In this case 0000
  }
  delay(40);//delay little for better serial communication

  int buttonState2 = digitalRead(EmergLight);
  
 if(buttonState2 == 1){//if button is down
    mySerial.println(22);//send unique code to the receiver to turn on. In this case 1111
    onOff2 = 1;//set boolean to 1
  }
    if(buttonState2 == 0 && onOff2 == 1){//Verifier to send off signal once
    mySerial.println(33);//send unique code to the receiver to turn off. In this case 0000
  }
  delay(40);//delay little for better serial communication
  
  int buttonState3 = digitalRead(Motor1Forw);
  
 if(buttonState3 == 1){//if button is down
    mySerial.println(44);//send unique code to the receiver to turn on. In this case 1111
    onOff3 = 1;//set boolean to 1
  }
    if(buttonState3 == 0 && onOff3 == 1){//Verifier to send off signal once
    mySerial.println(55);//send unique code to the receiver to turn off. In this case 0000
  }
  delay(40);//delay little for better serial communication

int buttonState4 = digitalRead(Motor1Rev);
  
 if(buttonState4 == 1){//if button is down
    mySerial.println(66);//send unique code to the receiver to turn on. In this case 1111
    onOff4 = 1;//set boolean to 1
  }
    if(buttonState4 == 0 && onOff4 == 1){//Verifier to send off signal once
    mySerial.println(77);//send unique code to the receiver to turn off. In this case 0000
  }
  delay(40);//delay little for better serial communication
  
int buttonState5 = digitalRead(Motor2Forw);
  
 if(buttonState5 == 1){//if button is down
    mySerial.println(88);//send unique code to the receiver to turn on. In this case 1111
    onOff5 = 1;//set boolean to 1
  }
    if(buttonState5 == 0 && onOff5 == 1){//Verifier to send off signal once
    mySerial.println(99);//send unique code to the receiver to turn off. In this case 0000
  }
  delay(40);//delay little for better serial communication

int buttonState6 = digitalRead(Motor2Rev);
  
 if(buttonState6 == 1){//if button is down
    mySerial.println(100);//send unique code to the receiver to turn on. In this case 1111
    onOff6 = 1;//set boolean to 1
  }
    if(buttonState6 == 0 && onOff6 == 1){//Verifier to send off signal once
    mySerial.println(200);//send unique code to the receiver to turn off. In this case 0000
  }
  delay(40);//delay little for better serial communication

int buttonState7 = digitalRead(WaterUp);
  
 if(buttonState7 == 1){//if button is down
    mySerial.println(300);//send unique code to the receiver to turn on. In this case 1111
    onOff7 = 1;//set boolean to 1
  }
    if(buttonState7 == 0 && onOff7 == 1){//Verifier to send off signal once
    mySerial.println(400);//send unique code to the receiver to turn off. In this case 0000
  }
  delay(40);//delay little for better serial communication

  int buttonState8 = digitalRead(WaterDown);
  
 if(buttonState8 == 1){//if button is down
    mySerial.println(500);//send unique code to the receiver to turn on. In this case 1111
    onOff8 = 1;//set boolean to 1
  }
    if(buttonState8 == 0 && onOff8 == 1){//Verifier to send off signal once
    mySerial.println(600);//send unique code to the receiver to turn off. In this case 0000
  }
  delay(40);//delay little for better serial communication
}

You are running into the blocking behavior/timeout of Serial.parseInt(). You are better to change the entire approach to send and receive following the methods of Robin2’s Serial Basics Tutorial.

Do not use delay on the receiving code, and let the TX code control the timing.

You are not managing the boolean control values correctly in the TX code. You never set them back to 0, and consequently you are continually sending the turnoff message every pass through loop.

You are not using the Serial monitor, so the HC12’s can be connected to the hardware serial pins 0 and 1 after the code is loaded. It can be faster and more reliable than software serial. Remember to cross connect the module RX to the arduino TX pin and the module TX to the arduino RX pin.

Thanks for the reply. The thing is I dont know a lot of the stuff you said to better understand. I will study the tutorial attached and try that approach

If someone can please upload a sample code of at least 2 buttons I may be able to figure it out. I find approaches other than the one I used too difficult to understand