HC-12 2-Way Comms to light LEDs only works one way

I am working on a simple project. Press a button on unit A and unit B lights up a LED for the duration of the button press (momentary), and vise versa. I have only been able to get them to work in one direction. I have figured out the last unit to power up is the one able to receive the signal and light up i.e. power up unit A then unit B, only B will light up. I am new to Arduino and can't for the life of me figure this out.

Each unit is an Arduino Nano with and HC-12, LEDs /w resistors, and a switch with a pulldown resistor and running the same sketch.

Any help would be amazing.

#include <SoftwareSerial.h>

SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin

int button = 2;
int led = 6;
boolean onOff = 0;



void setup() {
  pinMode (button, INPUT);
  pinMode (led, OUTPUT);
  Serial.begin(9600);             // Serial port to computer
  HC12.begin(9600);               // Serial port to HC12
}

void loop() {

  int buttonState = digitalRead(button);//read button state
  
  if(buttonState == 1){//if button is down
    HC12.println(1111);//send unique code to the receiver to turn on.
    onOff = 1;//set boolean to 1
  }
  if(buttonState == 0 && onOff == 1){//Verifier to send off signal once
    HC12.println(2222);//send unique code to the receiver to turn off. In this case 2222
  }



    if(HC12.available() > 1){    
    int input = HC12.parseInt();//read serial input and convert to integer (-32,768 to 32,767)    
    
    if(input == 1111){//if on code is received
      digitalWrite(led, HIGH);//turn LED on
    }
    if(input == 2222){//if off code is received
      digitalWrite(led, LOW);//turn LED off
    }
    }
       
  
  delay(20);

}

You have taken steps to ensure that the "off signal" is sent only once - at the moment the button is released - this is correct. It would be even more correct to send the "on signal" only once too - only at the moment the button pressed. You can use the same onOff flag for it.

Sending codes as 4-chars strings and reading them with parseInt() function is definitely a bad idea. It would be much faster to send only one byte code and read them simply with the read() command.

b707 thank you for your reply. I am new at this so bear with me.

If I enter the onOff flag, will the just send one signal, or will it be continuous as long as the button is held?

For sending the code, do I just replace parseInt with read, then reduce the characters being sent in the code?

Thank you!

you are using the SoftwareSerial library which has the restriction It cannot transmit and receive data at the same time. - this may be part of your problem
you could try AltSoftSerial

I think you can change the algorithm and send one signal for turning the LED on and one for off.

If you replaced the 4-digit char code by one byte, you will be able to read it with a single read() command.

Thank you horace. I tried AltSoftSerial and I get nothing at all now. I am at a loss

#include <AltSoftSerial.h>

AltSoftSerial HC12(2, 3); // HC-12 RX=2, TX=3

int button = 8;
int led = 6;
boolean onOff = 0;

void setup() {
  pinMode (button, INPUT);
  pinMode (led, OUTPUT);
  Serial.begin(9600);             // Serial port to computer
  HC12.begin(9600);               // Serial port to HC12
}

void loop() {

  int buttonState = digitalRead(button);//read button state
  
  if(buttonState == 1){//if button is down
    HC12.println(1);//send unique code to the receiver to turn on.
    onOff = 1;//set boolean to 1
  }
  if(buttonState == 0 && onOff == 1){//Verifier to send off signal once
    HC12.println(2);//send unique code to the receiver to turn off. In this case 2222
  }

    if(HC12.available() > 1){    
    int input = HC12.parseInt();//read serial input and convert to integer (-32,768 to 32,767)    
    
    if(input == 1){//if on code is received
      digitalWrite(led, HIGH);//turn LED on
    }
    if(input == 2){//if off code is received
      digitalWrite(led, LOW);//turn LED off
    }
    }
       
  
  delay(20);

}

there appears to be a problem using nano pins 2 and 3 with altSoftSerial
ran following program using altSoftSerial on a nano

// AltSoftSerial transmit/Receive Test
//
// Transmit data with Serial1 and try to receive
// it with AltSoftSerial.  You must connect a wire
// from Serial1 TX to AltSoftSerial RX.

#include <AltSoftSerial.h>

AltSoftSerial altser(9,8);
const int mybaud = 9600;

// Board            Serial1 TX   AltSoftSerial RX
// -----            ----------   ----------------
// UNO                   9              8
// Teensy 3.x            1              20
// Teensy 2.0            8 (D3)         10 (C7)
// Teensy++ 2.0          3 (D3)          4 (D4)
// Arduino Leonardo      1              13
// Arduino Mega         18              48

// Serial1 on AVR @ 16 MHz minimum baud is 245
// Serial1 on Teensy 3.2 @ 96 MHz minimum baud is 733

void setup() {
  delay(200);
  Serial.begin(115200);
  while (!Serial) ;  // wait for Arduino Serial Monitor
  altser.begin(mybaud);   // to AltSoftSerial RX
  Serial.println("AltSoftSerial transmit/ Receive Test");
}

void loop() {
  // transmit a test byte on Serial 1
  if (Serial.available() > 0) {
    altser.write(Serial.read());
  }
  // attempt to receive it by AltSoftSerial
  if (altser.available() > 0) {
    Serial.write(altser.read());
  }
}

note the pins used

AltSoftSerial altser(9,8);

running the above program and connecting pins 8 and 9 ran a loopback test OK
text entered on the serial monitor appears on the display, e.g.

AltSoftSerial transmit/ Receive Test
test 1
test 2 abcdefgh
resr 3 0987654321

Note - using pins 2 and 3 with the above program did not work
suggest changing your program to using pins 8 and 9

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.