I have two Nano's each with an HC12, and the sketches below. This is just a simple test setup to get the hang of using them. On the transmitter side, the onboard LED blinks as it should, and the TX LED also blinks, though more of a flicker, but that's to be expected.
On the receiving end, the RX LED flashes when the transmitter TX LED flashes, but not the on board LED. I added a line to print the variable to the serial monitor, and it's just blasting out zeros. So I'm inclined to think that it is receiving OK, but something is going wrong with the data. Maybe I need sleep and missed something stupid, but I can't figure out what it is.
On a side note, I had tried HC12.write instead of Serial.write and no luck. Same with Serial.print. But, when I used HC12.write (1); it was OK with that. But change the one to a zero and I would get and error "call of overloaded 'write(int)' is ambiguous"
Transmit sketch first, then the receiver sketch second.
#include <SoftwareSerial.h>
SoftwareSerial HC12(1,0); //TX pin and RX pin
void setup() {
Serial.begin(9600);
HC12.begin (9600);
pinMode (13, OUTPUT);
}
void loop() {
Serial.write (1);
digitalWrite(13, HIGH);
delay (500);
Serial.write (2);
digitalWrite(13, LOW);
delay (500);
}
#include <SoftwareSerial.h>
SoftwareSerial HC12(1,0); //TX pin and RX pin
byte val;
void setup() {
Serial.begin(9600);
pinMode (13, OUTPUT);
HC12.begin (9600);
}
void loop() {
while (Serial.available()) {
val = Serial.read();
}
if (val == 1) {
digitalWrite(13, HIGH);
}
else if (val == 2) {
digitalWrite(13, LOW);
}
}