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);
}