Issues with HC12 coding

So I'm trying to send the detected distance of an object using an ultrasonic distance sensor to a seperate arduino using HC12 transmitters. From there, the receiver is supposed to light an LED if the sensor detects something.
I'm having issues getting the LED to light up, so any help would be appreciated.

**Receiver Code**
#include <NewPing.h>
#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin

int ledGGo = 8;
int echoPin1 = A0;
int trigPin1 = A1;

NewPing sonar(trigPin1, echoPin1, 30); 

void setup(){
  Serial.begin(9600);
  pinMode(trigPin1, OUTPUT); 
  pinMode(echoPin1, INPUT); 
  pinMode(ledGGo, OUTPUT); 
  HC12.begin(9600);
}
void loop(){
  double detected = sonar.ping_cm();
  //Serial.write(HC12.read());
  while (HC12.available()){  // data is on the transmitter
    Serial.println(HC12.read()); // prints whats on the transmitter
    if (HC12.read() > 0){
        digitalWrite(ledGGo,HIGH);
       // delay(500);
       // digitalWrite(ledGGo,LOW);
    }
    if (HC12.read() < 0){
      digitalWrite(ledGGo,LOW);
    }
  }
}
**Transmitter Code**
#include <NewPing.h>
#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin
int echoPin1 = A0;
int trigPin1 = A1;

NewPing sonar(trigPin1, echoPin1, 30);

void setup(){
  Serial.begin(9600);
  HC12.begin(9600);               // Serial port to HC12
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
}
void loop(){
  double detected = sonar.ping_cm();
  Serial.println(detected);
  HC12.write(detected);
  delay(500);
    }

are you sure the HC-12 is receiving any data?
check you are receiving data using a simple SoftwareSerial test
what appears on the serial monitor?
show us a schematic of how it is wired?

Yes both serial monitors show the detected distance of the object, but the LED still doesnt light up.
Sorry I don't have a schematic just the completed circuits.


the code

  while (HC12.available()){  // data is on the transmitter
    Serial.println(HC12.read()); // prints whats on the transmitter
    if (HC12.read() > 0){

reads a byte from the HC12 then reads again so you miss the first byte
try

char ch;  
while (HC12.available()){  // data is on the transmitter
    Serial.println(ch=HC12.read()); // prints whats on the transmitter
    if (ch> 0){
      .......
   }
  else
     {
     .....
    }
 what form does the received data take, e.g. a byte, a text string, ????

Is that above line compliable? No!

char ch = HC12.read();
Serial.println(ch);
if( ch > 0){

why? what is wrong with it?
this compiles OK if I set the target board as a Arduino Mega

 while (HC12.available()){  // data is on the transmitter
    char ch;
    Serial.println(ch=HC12.read()); // prints whats on the transmitter
    if (ch > 0){
        digitalWrite(ledGGo,HIGH);
       // delay(500);
       // digitalWrite(ledGGo,LOW);
    }
    if (ch < 0){
      digitalWrite(ledGGo,LOW);
    }

however, your version is more readable

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