Problems sending data from 2 ultrasonic sensors to the second arduino

Hello everyone, I am really stuck with this problem for quite some time now. Tried many different solutions went through many different steps. I somehow managed to send basic data like some text through the NRF24L01 (one as transmitter the other as receiver). Then I managed to get separate data from 2 HC-SR04 sensors. Managed to also send the data from one sensor to the other. However, I cannot send data from 2 separate sensors to the second arduino through the wireless communication. Here's the transmitter code

#include <NewPing.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define TRIGGER_PIN1 2
#define TRIGGER_PIN2 4
#define ECHO_PIN1 3
#define ECHO_PIN2 5
#define MAX_DISTANCE 400
int distance1;
int distance2;
#define CE_PIN 9
#define CSN_PIN 10
RF24 radio(CE_PIN, CSN_PIN);
byte address[][6]={"1Node", "2Node"};
NewPing sonar1(TRIGGER_PIN1,ECHO_PIN1,MAX_DISTANCE);
NewPing sonar2(TRIGGER_PIN2,ECHO_PIN2,MAX_DISTANCE);
void setup()
{
  delay (1000);
  Serial.begin(115200);
  radio.begin();
  radio.openWritingPipe(addresses [1]);
  radio.openWritingPipe(1, addresses [0]);
  radio.setPALevel(RF24_PA_MIN);
  radio.setDataRate(RF24_2MBPS);
  radio.setChannel(124);
  radio.stopListening();
}

void loop()
{
  delay(5);
  radio.startListening();
  const char text[] = "Out of range";
  distance1=sonar1.ping_cm();
  distance2=sonar2.ping_cm();
  while (!radio.available());
  if (distance1>=400 || distance1<=0 && distance2>=400 || distance2<=0)
  {
    radio.write(&text, sizeof(text));
  }
  else
  {
    radio.write(&distance1, sizeof(distance1));
    radio.write(&distance2, sizeof(distance2));
    delay(10);
  }
}

and here's the receiver

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
RF24 radio(CE_PIN, CSN_PIN);
byte address[][6]={"1Node", "2Node"};
int distance1;
int distance2;
void setup()
{
  delay(1000);
  Serial.begin(115200);
  radio.begin();
  radio.openReadingPipe(addresses[0]);
  radio.openReadingPipe(1, addresses[1]);
  radio.setPALevel(RF24_PA_MIN);
  radio.setDataRate(RF24_2MBPS);
  radio.setChannel(124);
  radio.startListening();
}
void loop()
{
  if (radio.available())
  {
    char text[32] = "";
    while(radio.available())
    {
      radio.read(&text, sizeof(text));
      radio.read(&distance1, sizeof(distance1));
      radio.read(&distance2, sizeof(distance2));
      Serial.println(text);
      Serial.println(distance1);
      Serial.println(distance2);
    }
    delay(5);
    radio.stopListening();
    radio.write(&text, sizeof(text))
  }
  delay(1000);
}

Now, I am either getting no data at all or with minor changes, I get completely random numbers without context. I would really be glad to know where the problem lies here as I watched countless videos and tried countless combinations. Any help is much appreciated.

How does the receiver know what data it is receiving in order to put it into the correct variable ?

For instance, if the readings are not out or range then the data array will not be transmitted but the receiver always tries to read it

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