Hi i would like some advice please and help. I have googled and looked on the forum here for help, and so far I have a semi working code that i have put together and this is far as i could get after weeks. It does not seem much but i have tried.
What i want the code to do is from the Transmitter which has the ultrasonic sensor, is to report the Distance to the Receiver and with the Distance values i get i can use that to turn on LEDs. But most of the projects out there all just do a Serial.print to LCDs
Ok what my code does currently it sends the distance from the Transmitter to the Receiver, and i'm getting unreliable distance readings, Trigger points even from the Serial monitor. I know this is where my fault lays but don't know how to correct. And place a reliable (distance) variable in is place
My idea is to have full control over the Variable received from the transmitter and use it as i like on the receiver.
Thank you in advance.
These are the links i have tried to put this togeather.
https://forum.arduino.cc/t/how-to-send-data-from-one-arduino-to-another/393936/10```
This is my transmitter code.
// Transmitter
#include<RH_ASK.h>
#include<SPI.h>
RH_ASK rf_driver(2000, 11, 12);
//const int Led = 8;
const int trig = 2;
const int echo = 3;
long distance = 0;
void setup(void) {
Serial.begin(9600);
rf_driver.init();
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(Led, OUTPUT);
}
void loop(void) {
rf_driver.send((uint8_t*)distance,(uint8_t)1);
rf_driver.waitPacketSent();
long duration;
digitalWrite(trig, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trig, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance = (duration / 2) / 29.1;
}
Receiver code
// Receiver
#include<RH_ASK.h>
#include<SPI.h>
const int Led = 8;
uint8_t Received = 1;
long distance = 0;
RH_ASK rf_driver(2000, 11, 12);
void setup() {
Serial.begin(9600);
rf_driver.init();
pinMode(Led, OUTPUT);
}
void loop() {
if (rf_driver.recv((uint8_t *) (&distance), &Received))
if(distance > 10 && distance < 30){
digitalWrite(Led, 1);
delay(30);
digitalWrite(Led, 0);
delay(30);
}
Serial.println(distance);
}