Want to send the Ultrasonic Sensor data over NRF24 (one way)

Hi Guys,

I am trying to send the Ultrasonic Distance Sensor data over NRF24 module (one way communication).

I have read almost every previous post related to this issue and learned a lot, but still somehow my code is not working.

Can someone have a quick look and tell me the issue?

Below are the codes.

TX

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

const int trigPin = 9;
const int echoPin = 10;

int distance;
long duration;

struct Pack {
  int data1;
} packet;


RF24 radio(7, 8);
const byte address[6] = "00001";

void setup() {

  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.setDataRate(RF24_250KBPS);
  radio.stopListening();
  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
}


void loop() {

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;
  packet.data1 = distance;
  if (radio.write(&packet, sizeof(packet))) {
    Serial.println(packet.data1);
  } else {
    Serial.println("Send failed.");
  }
  delay(2000);
}

RX

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

RF24 radio(7,8);
const byte address[6] = "00001";

struct Pack {
  int data1;
} packet;


void setup() {
  radio.begin();
  radio.openReadingPipe(0,address);
  radio.setPALevel(RF24_PA_MIN);
  radio.setDataRate(RF24_250KBPS);
  radio.startListening();
  Serial.begin(9600);
}

void loop() {
  if ( radio.available())   {
    radio.read( &packet, sizeof(packet) );
    Serial.print("Data received = ");
    Serial.println(packet.data1);

  }
}

You haven't explained what is wrong in any detail.
Anyway, split the problem in two. Get a basic NRF24L01 communication sketch pair working. See Simple nRF24L01+ 2.4GHz transceiver demo - Exhibition / Gallery - Arduino Forum then get a basic ultrasonic sketch working.
After that, join the 2 parts together.

I figured out the problem.

Actually i want to send the continously changing values of ultrasensor distance sensor.

But in my TX code, it (radio.write) is keep on sending the first calculated value of distance (which is zero). Thats the problem.

Can you please advise, how i can proceed ? any idea?

Actually i want to send the continously changing values of ultrasensor distance sensor.

Every two seconds, anyway.

But in my TX code, it (radio.write) is keep on sending the first calculated value of distance (which is zero). Thats the problem.

Proof would be a good thing. Prove that the distance reading changes, but that the value sent doesn't.

What do you get if you Serial.println(distance) right after you calculate it?

@evanmars : it will print only first value of distance. As after this statement, compliers goes to radio.write statement, where it got stuck and keep on sending first value.

gavanpreet28:
@evanmars : it will print only first value of distance. As after this statement, compliers goes to radio.write statement, where it got stuck and keep on sending first value.

Proof still seems to be in very short supply.

Screenshots

What is the return value of radio.write?

gavanpreet28:
Screenshots

Pictures of text. How useless is that?

It would appear that radio.write() is not returning. That is NOT the same as sending the same (wrong) value over and over (which is what you said was happening).

Have you tried the examples that come with the library? Can you send ANYTHING over and over?

evanmars:
What is the return value of radio.write?

I am sorry. I didnt get you

@Paul: i am agree with you. Can you have a look at void setup section, which i suspect have some error (maybe wrong again) .

maybe wrong again

You are. Your first anonymous print happens only after radio.write() returned true.

  1. Why would anyone use anonymous printing? I want to know what the value that is printed represents, so I always print something before the value.

2). Why won't you add more Serial.print() statements, to learn just where the code is stopping?

Issue solved by changing trigPin and echoPin from 9,10 to 2,3.

CAN YOU BE ABLE TO RECEIVE THE DISTANCE VALUES IN THE RECEIVER AFTER CHANGING TRIG,ECHO PIN TO 2,3 OR YOU MADE OTHER CHANGES IN THE CODE DISPLAYED OVER TOP FOR TRANS/RECEIVER IF SO TELL THOSE CHANGES

gavanpreet28:
Issue solved by changing trigPin and echoPin from 9,10 to 2,3.

CAN YOU BE ABLE TO RECEIVE THE DISTANCE VALUES IN THE RECEIVER AFTER CHANGING TRIG,ECHO PIN TO 2,3 OR YOU MADE OTHER CHANGES IN THE CODE DISPLAYED OVER TOP FOR TRANS/RECEIVER IF SO TELL THOSE CHANGES

gavanpreet28:
Issue solved by changing trigPin and echoPin from 9,10 to 2,3.

CAN YOU BE ABLE TO RECEIVE THE DISTANCE VALUES IN THE RECEIVER AFTER CHANGING TRIG,ECHO PIN TO 2,3 OR YOU MADE OTHER CHANGES IN THE CODE DISPLAYED OVER TOP FOR TRANS/RECEIVER IF SO TELL THOSE CHANGES

@rohitchatla, please check your keyboard. The Caps Lock key appears to be stuck.

You also appear to be having finger spasms as evidenced by repeated clicking on the "Post" button.