Wireless ultrasonic sensor reading NRF-24

Hi, Can anyone help me. Im trying to transmit distance readings from an ultrasonic sensor from one arduino nano to the next using NRF - 24 modules. The modules work and so does the ultrasonic sensor. I merged the two and I have the transmitter and receiver code below. The readings are find on the end with the ultrasonic sensor but when they get transmitted over to the other arduino nano they dont show any readings. HOWEVER the reciever starts to pick up readings and displaying them perfectly when and only when the distance reading of the ultrasonic sensor is below 6 cm. Any thoughts?

transmitter

#include<SPI.h>
#include "RF24.h"

int msg[1];

RF24 radio(7,8);

const uint64_t pipe = 0xE8E8F0F0E1LL;

int val;
int trig = 4;
const int & echo = 5;
int travel_time;
int distance;
int cm = 0;

void setup(void) {
  radio.begin();
  radio.openWritingPipe(pipe);
  Serial.begin(9600);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
}


void loop(void) {

  digitalWrite(trig, LOW); 
  delay (1);
  digitalWrite(trig, HIGH);
  delay (5); 
 
  digitalWrite(trig, LOW);

  travel_time = pulseIn(echo, HIGH);
  distance = (travel_time*0.034)/2;
 
  msg[0] = distance;
  Serial.println(msg[0]);
  radio.write(msg, 1);
}

Reciever

#include <Servo.h>
#include <SPI.h>
#include "RF24.h"

Servo myservo;

RF24 radio(7, 8);

const uint64_t pipe = 0xE8E8F0F0E1LL;

int msg[1];

void setup() {
  radio.begin();
  radio.openReadingPipe(1, pipe);
  radio.startListening();
  Serial.begin(9600);
}
void loop() {
  if (radio.available()) {
    bool done = false;
    while (!done) {
      done = radio.read(msg, 1);
       Serial.println(msg[0]);
    }
  }
}

Your math is all wrong because you are trying to do floating point arithmetic using integer data types.

but the math works perfectly when the ultrasonic sensor is alone it just the receiving end that having trouble.

Could it be something to do with an int consisting of two bytes?

This is wrong

radio.write(msg, 1);

it should be

radio.write(msg, sizeof(msg[0]));

An int occupies 2 bytes.

Also in the receiver code this seems completely unnecessary

   bool done = false;
    while (!done) {

...R

Simple nRF24L01+ Tutorial

I made all those changes and got the same results which showed me that a few lines of code where unnecessary. I am still getting the same results now except instead of only displaying readings onece it gets down to 6 cm it displays them once it gets down to 9 cm. I don think that means much though. Also what do you mean by a int consisting of two bytes? I dont really understand that.

A byte is an eight bit unit
An int is a sixteen bit unit.
An int consists of two bytes.

You are transmitting and receiving one byte only.

so are you saying I should wright bytes instead of ints as my variables.

No, we’re saying if you send ints, send (and receive) two bytes, not one.

Im really sorry Im trying a bunch of stuff on my end but I dont exactly understand what you want me to do with the code. Can you be more descriptive.

radio.write(msg, 1);

Sending one byte

radio.write(msg, 2);

Sending two bytes

hert:
Im really sorry Im trying a bunch of stuff on my end but I dont exactly understand what you want me to do with the code. Can you be more descriptive.

You have to post the latest version of your programs (Tx and Rx) so we can see exactly what you have done. The devil is in the detail.

...R