Ultrasonic code works, combined with TX code not

hi all,

Trying to update my water level measuring project. i had a HC-SR04 sensor measuring distance for me and sending some information via rf 433 mhz transmitter back home.

this setup was outside for a year and my sensor gave up.

now i have a JSN SR04T, weather proof sensor. version 2.0. looks like it is compatible but it is not. i had a lot of wrong readings so i decided to use the ping library.

so far so good, all good readings.

but! ofcourse when combining my rf part of the code it all jams. i will get one reading and then nothing anymore.

i was hoping if any of you could help this beginner out.

distanse measuring works ok:

#include <NewPing.h>

#define TRIGGER_PIN  6  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     8  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 150// Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  Serial.begin(9600); // Open serial monitor at 9600 baud to see ping results.
}

void loop() {
  delay(1000);                     // Wait 1000ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
 // Serial.println(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)


  if ((sonar.ping_cm())  >= 50) //afstabd waarop de tank leeg is
{ 
  Serial.println(sonar.ping_cm());
  Serial.println("leeg");
  delay(50);
}

  else
{
  Serial.println(sonar.ping_cm());
  Serial.println("genoeg");
  delay(50);
}
 

 if ((sonar.ping_cm())  <= 30) //afstabd waarop de tank vol is
{ 
  Serial.println(sonar.ping_cm());
  Serial.println("vol");
  delay(50);

}
}

combined with rf, not worjking

#include <NewPing.h>
#include <RH_ASK.h> //rx tx library radiohead
#include <SPI.h> 

#define TRIGGER_PIN  6  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     8  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 150// Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#define LEDROOD 13 // Onboard LED
#define BLINK_COUNT 2 //hoe vaak de led knipperd als tank leeg is

// transmitter en reciever module 
const int txSpeed = 512;
const int rxPin = 5;
const int txPin = 4;
const int pttPin = 7;
#define LEDTX 8
 
RH_ASK driver(txSpeed, rxPin, txPin, pttPin);





//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!AFSTAND AANPASSEN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


 

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  Serial.begin(9600); // Open serial monitor at 9600 baud to see ping results.
  pinMode(LEDROOD, OUTPUT);//Change to output my pins
  pinMode(LEDTX, OUTPUT);
  digitalWrite(LEDROOD,HIGH);//Turn on red LED 
}

void loop() {
  delay(1500);                     // Wait 1000ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
 // Serial.println(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)


  if ((sonar.ping_cm())  >= 50) //afstabd waarop de tank leeg is
{ 
  Serial.println(sonar.ping_cm());
  Serial.println("leeg");
  delay(50);
  digitalWrite(LEDROOD, HIGH); 
  const char *msg = "1"; //stuur message 1 naar RX
  digitalWrite(LEDTX, HIGH); //LEDTX gaat branden omdat de tank vol is
  driver.send((uint8_t *)msg, strlen(msg));
  driver.waitPacketSent();
  delay(200);

  
}

  else
{
  Serial.println(sonar.ping_cm());
  Serial.println("genoeg");
  delay(50);
  digitalWrite(LEDROOD, LOW); 
  const char *msg = "2"; //stuur message 1 naar RX
  driver.send((uint8_t *)msg, strlen(msg));
  driver.waitPacketSent();
  delay(200);
}
 

 if ((sonar.ping_cm())  <= 30) //afstabd waarop de tank vol is
{ 
  Serial.println(sonar.ping_cm());
  Serial.println("vol");
  delay(50);
  const char *msg = "3"; //stuur message 1 naar RX
  digitalWrite(LEDTX, HIGH); //LEDTX gaat branden omdat de tank vol is
  driver.send((uint8_t *)msg, strlen(msg));
  driver.waitPacketSent();
  delay(200);

for ( int i=0; i < BLINK_COUNT; i++ )
{
 digitalWrite(LEDROOD, HIGH);
 delay(200);                  
 digitalWrite(LEDROOD, LOW);    
 delay(200);

}
}
}

thanks for having a look at it.

The ECHO_PIN is the same as the LEDTX pin (both 8) you define it as an output but i think the echo needs to be input...

Thanks for your reply. I feel so stupid having ECHO_PIN and LEDTX on the same pin.

I changed the echo pin to pin 9, adjusted the hardware, still only one read and then nothing anymore.

Also the library defines the Echopin to be input.

So hopefully we can still find the error

so it' has to be something in the RF communication holding up the program, Can you run an Example sketch for that ?

Hey you say // Wait 1000ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.but actually you are pinging testing every condition

if ((sonar.ping_cm())  >= 50)  

if ((sonar.ping_cm())  <= 30)   {//afstabd waarop de tank vol is
    Serial.println(sonar.ping_cm())

and printing to the Serial port the result of a new ping.
how about

int pingresult=sonar.ping_cm();

and testing and printing that instead.