Ultrasonic Sensing with 433 MHz Data Transmission

As the title suggests, I'm trying to transmit readings from an ultrasonic sensor using the 433 MHz module. I'm encountering an error I haven't seen described for this same project elsewhere in the forums.

I first tested some code I found online for the individual portions of this project. The code for 433 MHz Tx - Rx works perfectly individually, and the code for ultrasonic distance sensing works perfect on its own. It's when I combine the code that I get an error. You'll see in the code attached that I'm not even trying to send the distance readings via radio yet. All I did was combine the code but have the two portions do their own thing.

When I combine the code (just getting Tx to send "Hello World" and the Serial Monitor to give me the distance reading), it instead gives me a distance reading of 0 and on the receive side I'm getting garbled "Hello World" packets, e.g. "age:lo W!", "essaHell", "ssagHello", "WoMeello".

Any input appreciated.


#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile

RH_ASK driver;
const int trigPin = 9;
const int echoPin = 10;
// defines variables
long duration;
int distance;

void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  Serial.begin(9600); // Starts the serial communication
  if (!driver.init())
  Serial.println("init failed");
}
void loop() {
    const char *msg = "Hello World!";
    driver.send((uint8_t *)msg, strlen(msg));
    driver.waitPacketSent();
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    // Sets the trigPin on HIGH state for 10 micro seconds
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    // Reads the echoPin, returns the sound wave travel time in microseconds
    duration = pulseIn(echoPin, HIGH);
    // Calculating the distance
    distance = duration * 0.034 / 2;
    // Prints the distance on the Serial Monitor
    Serial.print("Distance: ");
    Serial.println(distance);
}



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