How to send data of 2 ultrasonic sensors from arduino to arduino using nRF24L01

Component of transmitter: arduino, nRF24L01, 2 ultrasonic sensors
Component of receiver: arduino, nRF24L01

Aim: to send ultrasonic distance from transmitter to receiver

The problem I'm facing: receiver can only receive one of the sensor data. I have checked all the component is not broken. Below is transmitter and receiver code.

Transmitter side is working fine

Transmitter code :

// transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7,8); // CE, CSN
const byte addresses [6] = {"00001"};  


int S1_trigpin = 2; //output
int S1_echopin = 3; //input
int S2_trigpin = 4; //output
int S2_echopin = 5; //input


long S1_duration;
long S2_duration;


int S1_distance;
int S2_distance;


void setup() 
{

  pinMode(S1_trigpin, OUTPUT);
  pinMode(S1_echopin, INPUT);
  pinMode(S2_trigpin, OUTPUT);
  pinMode(S2_echopin, INPUT);


  Serial.begin(9600);
  radio.begin();                           
  radio.openWritingPipe(addresses);     
  radio.stopListening();

}
void loop() 
{  
//-----------Sensor1---------------------
  digitalWrite(S1_trigpin, LOW);
  delayMicroseconds(2);
  digitalWrite(S1_trigpin, HIGH);
  delayMicroseconds(10);
  digitalWrite(S1_trigpin, LOW);
  S1_duration = pulseIn(S1_echopin, HIGH);
  S1_distance = S1_duration * 0.0343 / 2;

  radio.write(&S1_distance, sizeof(S1_distance));  //Sending the data
  delay(25); 

//-----------Sensor2---------------------
  digitalWrite(S2_trigpin, LOW);
  delayMicroseconds(2);
  digitalWrite(S2_trigpin, HIGH);
  delayMicroseconds(10);
  digitalWrite(S2_trigpin, LOW);
  S2_duration = pulseIn(S2_echopin, HIGH);
  S2_distance = S2_duration * 0.0343 / 2;

  radio.write(&S2_distance, sizeof(S2_distance));  //Sending the data
  delay(25); 

//-----------printing---------------------
  Serial.print("S1_distance: ");
  Serial.print(S1_distance);
  Serial.print("cm");

  Serial.print("  ");

  Serial.print("S2_distance: ");
  Serial.print(S2_distance);
  Serial.println("cm");

}


Receiver code:
// receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7,8); // CE, CSN
const byte addresses [6] = {"00001"};    


//---------------Declare variable-----------------
int S1_distance = 0;
int S2_distance = 0;


void setup() 
{
  Serial.begin(9600);
  radio.begin();                            
  radio.openReadingPipe(1, addresses);   
  radio.startListening();                 

}
 
void loop() 
{
   //distance=0;                   
  if (radio.available())                     
  {
    while(radio.available())
    {
    radio.read(&S1_distance, sizeof(S2_distance));
    Serial.print("S1: ");
    Serial.print(S1_distance);
    Serial.print("cm"); 
    Serial.print(" ");

    radio.read(&S2_distance, sizeof(S2_distance));
    Serial.print("S2: ");
    Serial.print(S2_distance);
    Serial.print("cm"); 
    Serial.print(" ");
    }
  }
}

this is result of receiver, sensor(S_1) is able to receive, but sensor(S_2) is not able to receive

I keep seeing this construct, and I don't understand why - "if" or "while", but not both.

What happens if your receiver starts between the transmission of S1 and S2?
There's nothing to identify the readings, not even a comma or newline (hint)

I see this construct too (a lot) and really wonder where it came from.

Sorry i don't understand what you mean by "What happens if your receiver starts between the transmission of S1 and S2?"

This is my final year project, any contributing to solve I'll be very happy :blush:

Imagine I'm reading out pairs of numbers, and you enter the room when I've just read out the first of a pair.
How do you remain in sync?

the purpose of receiver is to receive distance of S1 and S2, but now S2 is not showing. Pls help me to solve this, thank you

Please reply to my question, thank you.

I'll just repeat the same number that you read out.

...and you'll assume it's the first of a pair.

(Final year of what?)

yes, if you read 3, then I'll assume the other pair number is also 3. This is my university final year project, I'm running out of time and I got stuck with this issue for few days, really hope someone can solve it.

I'd be inclined to send both readings in a single message.


left is transmitter, right is receiver

See reply 5, and reply 11

are you giving me hint that i just need to copy andpaste the code ? I did copy paste of S1 to S2, but not working

I'm am giving you a hint, but it is nothing to do with cut and paste

I agree with this "There's nothing to identify the readings, not even a comma or newline (hint)", may i know the correct code please?

You'll know the correct code when you write and adequately test it.

Best of luck with your project.

thanks for the wishing, I have been solving this issue for few days, if you know the correct code can you let me know please :cry:

At the risk of derailing your progress so far, have a look at post #19 in this discussion:

Look for the dataToSend array in the Tx code and see how it is populated and transmitted. The look at the Rx code at the dataReceived array and how that is received and parameters extracted.

I've not tested that actual code but it should give you one possible option for transmitting several parameters in the same message. This will avoid the sync issue when sending several parameters individually.

I cannot know the correct code, because
a) I don't have your hardware
b) I don't have any hardware right now,
c) I'm writing this on my phone because my laptops are 800km away.

So, I can't say any code I might write for hardware I don't have would be correct, but I've given you enough clues to enable you to finish the job yourself.

(What's the course?)