Hello,
I'm working with a set of arduinos with HC-12 modules and have found a strange behavior in the Receiving module when powered up after the Transmitter.
My code simply tests the connection between the 2 arduinos by which the Tx module repeatedly sends a string of "Testing" and the Rx module blinks when it receives the string.
My problem is that when the Rx module is powered on after the Tx module is already on, it freezes and does not react to the signal. When the Tx module is powered on second, they both work just fine. It's as if the Tx is interrupting the Rx.
Thanks for your help!
Here is my code:
---Transmitter---
#include <SoftwareSerial.h>
#define setPin 4
SoftwareSerial HC12(3,2); // HC12 TX Pin, HC-12 RX Pin
byte incomingByte;
String readBuffer = "";
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
HC12.begin(9600); // Open serial port to HC12
pinMode(setPin, OUTPUT);
digitalWrite(setPin, LOW); // Put HC12 in Command Mode
delay(250);
HC12.write("AT+DEFAULT"); // Set default settings
delay(250);
digitalWrite(setPin, HIGH); // Put HC12 in Transparent Mode
delay(100);
}
void loop() {
HC12.write("Testing");
delay(100);
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
delay(50); // wait
digitalWrite(LED_BUILTIN, LOW); // turn the LED off
delay(70);
delay(500);
}
---Receiver---
#define setPin 4
byte incomingByte;
String readBuffer = "";
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600); // Open serial port to computer
pinMode(setPin, OUTPUT);
digitalWrite(setPin, LOW); // Put HC12 in Command Mode
delay(250);
Serial.write("AT+DEFAULT"); // Set default settings
delay(250);
digitalWrite(setPin, HIGH); // Put HC12 in Transparent Mode
delay(100);
}
void loop() {
while(Serial.available()){ // If HC12 has data
incomingByte = Serial.read(); // Store each incoming byte from HC12
readBuffer += char(incomingByte); // Add each byte to readBuffer string variable
}
delay(100);
if (readBuffer == "Testing"){
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
delay(50); // wait
digitalWrite(LED_BUILTIN, LOW); // turn the LED off
delay(70);
}
readBuffer = "";
delay(500);
}