Struct / array help

Hi, I am not a programmer, not even an electronic guy any help will be great here once I am new on Arduino.

I am trying to connect 3 Arduino in an RF24 so 2 Arduino with ultrasonic sensors will check the distance and send the data to a receiver.

As far as I know, I have to put it in an array or a struct but I could not make it work I have checked many topics and tried to adapt it to my code but I am probably missing something.

the following code is my code working without errors but because I could not set it in array/struct I am receiving random measurements in the receiver at the moment.

RECEIVER


// RECEIVER


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

#include <LiquidCrystal.h>
const int rs = 10, en = 7, d4 = 6, d5 = 5, d6 = 4, d7 = 3;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

RF24 radio(9, 8); // CE, CSN
const byte addresses[][10] = {"ADDRESS01","ADDRESS02"};
int distance_mm1;
int distance_mm2;

void setup() {
  Serial.begin(9600);  
  radio.begin();  
  radio.openWritingPipe(addresses[1]);    // ADDRESS02
  radio.openReadingPipe(1, addresses[0]); // ADDRESS01
  radio.setPALevel(RF24_PA_MAX);

  Serial.println("Loading"); // print some text in Serial Monitor
  Serial.println("Ultra sonic");
  lcd.begin(16, 2); // lcd starts with resolution 16x2
};

void loop() {  
  delay(10);

  radio.startListening();
  if (radio.available()) {                // Receive
    
    radio.read(&distance_mm1, sizeof(distance_mm1)); 
   

  lcd.setCursor(0, 0);
  lcd.print("SENSOR1: ");
  lcd.print(distance_mm1);
  lcd.println("mm    ");

radio.read(&distance_mm2, sizeof(distance_mm2));


  lcd.setCursor(0, 1);
  lcd.print("SENSOR2: ");
  lcd.print(distance_mm2);
  lcd.println("mm    ");
  };

delay(1000);

  
  radio.stopListening();                  // Transmit
  const char txt_sent[] = "Sender 2";
  radio.write(&txt_sent, sizeof(txt_sent));  
  Serial.print("Sent to: ");Serial.println(txt_sent);

Serial.println(distance_mm1);
Serial.println("");
Serial.println(distance_mm2);


};

TRANSMITER1


// TRANSMITTER


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

#define echoPin 5 // attach pin D4 Arduino to pin Echo of HC-SR04
#define trigPin 4 //attach pin D5 Arduino to pin Trig of HC-SR04
long duration; // variable for the duration of sound wave travel
int distance_mm1; // variable for mm measurement

RF24 radio(9, 8);          // CE, CSN
const byte addresses[][10] = {"ADDRESS01","ADDRESS02"};

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(addresses[0]);    // ADDRESS01
  radio.openReadingPipe(1, addresses[1]); // ADDRESS02
  radio.setPALevel(RF24_PA_MAX);

  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
};

void loop() {
 // Clears the trigPin condition
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
    // Calculating the distance
  distance_mm1 = (duration * 0.034 / 2)*10; // Speed of sound wave divided by 2 (go and back)


  delay(10);
  
  radio.stopListening();                  // Transmit
  radio.write(&distance_mm1, sizeof(distance_mm1));
  //const char txt_sent[] = "Sender 1";
  //radio.write(&txt_sent, sizeof(txt_sent));  
  //Serial.print("Sent to: ");Serial.println(txt_sent);
  
  delay(10);

  radio.startListening();
  if (radio.available()) {                // Receive
    char txt_received[10] = "";
    radio.read(&txt_received, sizeof(txt_received)); 
    Serial.print("Received from: ");Serial.println(txt_received);
  
  };
 
};


// TRANSMITTER 2


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

#define echoPin 5 // attach pin D4 Arduino to pin Echo of HC-SR04
#define trigPin 4 //attach pin D5 Arduino to pin Trig of HC-SR04
long duration; // variable for the duration of sound wave travel
int distance_mm2; // variable for mm measurement

RF24 radio(9, 8);          // CE, CSN
const byte addresses[][10] = {"ADDRESS01","ADDRESS02"};

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(addresses[0]);    // ADDRESS01
  radio.openReadingPipe(1, addresses[1]); // ADDRESS02
  radio.setPALevel(RF24_PA_MAX);

  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
};

void loop() {
 // Clears the trigPin condition
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
    // Calculating the distance
  distance_mm2 = (duration * 0.034 / 2)*10; // Speed of sound wave divided by 2 (go and back)


  delay(10);
  
  radio.stopListening();                  // Transmit
  radio.write(&distance_mm2, sizeof(distance_mm2));
  //const char txt_sent[] = "Sender 1";
  //radio.write(&txt_sent, sizeof(txt_sent));  
  //Serial.print("Sent to: ");Serial.println(txt_sent);
  
  delay(10);

  radio.startListening();
  if (radio.available()) {                // Receive
    char txt_received[10] = "";
    radio.read(&txt_received, sizeof(txt_received)); 
    Serial.print("Received from: ");Serial.println(txt_received);
  
  };
 
};

Why a structured array shall help?

I am not sure but according to the other topics I have set it as a struct to receive the right data in the receiver but I couldn't do the right code.

These are (from the view of a NRF24L01) identical (the first 5 bytes are used).

Do you mean that I have to use the same byte or a different one in my reading/writing?

If you only use one pipe at a time, it just wastes memory, but will work.

The sending address is used as a receive address for pipe 0
in the acknowledge process anyway.

To avoid an unnecessary copy each time you call startListen, it is better to use pipe 1.

At the moment I am using only one pipe but I intend to send back a command if it works.

I still have many problems making it works,

error: the initializer fails to determine the size of 'txt_sent'

the data I am trying to send is variable how can I set a size?

do you have any good tutorials or videos about arrays? I am still very newbie on it :sweat_smile:

int txt_sent[] = distance_mm1;
  radio.write(&txt_sent, sizeof(txt_sent));
struct Packet {
uint16_t distance;
uint8_t color;
uint8_t flavor;
} pack;


  pack.distance = whatever;
  radio.write(&pack, sizeof(Packet));

But I am still with a problem with the received data, the receiver is just receiving one of the struct instructions(the first on the list) and did not make any differentiation between distance1 and distance2.
Even if just sender 2 is on. It reads as distance1.

RECEIVER


// RECEIVER


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

#include <LiquidCrystal.h>
const int rs = 10, en = 7, d4 = 6, d5 = 5, d6 = 4, d7 = 3;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

RF24 radio(9, 8); // CE, CSN
const byte addresses[][10] = {"ADDRESS01","ADDRESS02"};


struct Packet {

uint16_t distance1;
uint16_t distance2;

//uint8_t color;
//uint8_t flavor;
} pack;

int packet;
int d1;
int d2;

void setup() {
  Serial.begin(9600);  
  radio.begin();  
  radio.openWritingPipe(addresses[1]);    // ADDRESS02
  radio.openReadingPipe(1, addresses[0]); // ADDRESS01
  radio.setPALevel(RF24_PA_MAX);

  Serial.println("Loading"); // print some text in Serial Monitor
  Serial.println("Ultra sonic");
  lcd.begin(16, 2); // lcd starts with resolution 16x2
};

void loop() {  
  delay(10);

  radio.startListening();
  if (radio.available()) {                // Receive
    
    radio.read(&pack, sizeof(packet)); 
    
  
  };

d1 = pack.distance1;
d2 = pack.distance2;

delay(1000);

lcd.setCursor(0, 0);
  lcd.print("SENSOR1: ");
  lcd.print(d1);
  lcd.println("mm    ");

  lcd.setCursor(0, 1);
  lcd.print("SENSOR2: ");
  lcd.print(d2);
  lcd.println("mm    ");

  radio.stopListening();                  // Transmit
  const char txt_sent[] = "Sender 2";
  radio.write(&txt_sent, sizeof(txt_sent));  
  Serial.print("Sent to: ");Serial.println(txt_sent);

Serial.println(pack.distance1);
Serial.println("");
Serial.println(pack.distance2);


};

TRANSMITTER1


// TRANSMITTER


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

#define echoPin 5 // attach pin D4 Arduino to pin Echo of HC-SR04
#define trigPin 4 //attach pin D5 Arduino to pin Trig of HC-SR04
long duration; // variable for the duration of sound wave travel
int distance_mm; // variable for mm measurement

RF24 radio(9, 8);          // CE, CSN
const byte addresses[][10] = {"ADDRESS01","ADDRESS02"};

struct Packet {
uint16_t distance1;
//uint8_t color;
//uint8_t flavor;
} pack;

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(addresses[0]);    // ADDRESS01
  radio.openReadingPipe(1, addresses[1]); // ADDRESS02
  radio.setPALevel(RF24_PA_MAX);

  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
};

void loop() {
 // Clears the trigPin condition
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
    // Calculating the distance
  distance_mm = (duration * 0.034 / 2)*10; // Speed of sound wave divided by 2 (go and back)


  delay(10);
  
  radio.stopListening();                  // Transmit
  pack.distance1 = distance_mm;
  radio.write(&pack, sizeof(Packet));
  //const char txt_sent[] = "Sender 1";
  //radio.write(&txt_sent, sizeof(txt_sent));  
  //Serial.print("Sent to: ");Serial.println(txt_sent);
  
  delay(10);

  radio.startListening();
  if (radio.available()) {                // Receive
    char txt_received[10] = "";
    radio.read(&txt_received, sizeof(txt_received)); 
    Serial.print("Received from: ");Serial.println(txt_received);
  
  };
 
};

TRANSMITTER2


// TRANSMITTER


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

#define echoPin 5 // attach pin D4 Arduino to pin Echo of HC-SR04
#define trigPin 4 //attach pin D5 Arduino to pin Trig of HC-SR04
long duration; // variable for the duration of sound wave travel
int distance_mm; // variable for mm measurement

RF24 radio(9, 8);          // CE, CSN
const byte addresses[][10] = {"ADDRESS01","ADDRESS02"};

struct Packet {
uint16_t distance2;
//uint8_t color;
//uint8_t flavor;
} pack;

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(addresses[0]);    // ADDRESS01
  radio.openReadingPipe(1, addresses[1]); // ADDRESS02
  radio.setPALevel(RF24_PA_MAX);

  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
};

void loop() {
 // Clears the trigPin condition
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
    // Calculating the distance
  distance_mm = (duration * 0.034 / 2)*10; // Speed of sound wave divided by 2 (go and back)


  delay(10);
  
  radio.stopListening();                  // Transmit
  pack.distance2 = distance_mm;
  radio.write(&pack, sizeof(Packet));
  //const char txt_sent[] = "Sender 1";
  //radio.write(&txt_sent, sizeof(txt_sent));  
  //Serial.print("Sent to: ");Serial.println(txt_sent);
  
  delay(10);

  radio.startListening();
  if (radio.available()) {                // Receive
    char txt_received[10] = "";
    radio.read(&txt_received, sizeof(txt_received)); 
    Serial.print("Received from: ");Serial.println(txt_received);
  
  };
 
};

If you want to distinguish the transmitters, you could use

struct Packet {
uint8_t source;
uint16_ t distance;
} pack;

// T1

  pack.source = 1;
  pack.distance = whatever;
  result = radio.send(&pack, sizeof(pack));

// T2

  pack.source = 2;
  pack.distance = whatever;
  result = radio.send(&pack, sizeof(pack));

You really think that would make a difference at the receiving side?

Why do you think, both packets would be sent by the same transmitter?

Sorry, perhaps I didn't read the question...

and what shout I write in the receiver program? It did not work but maybe I have done something wrong on the receiver.

I don't know what "it" is, or how you define "not working".

Syntactically and semantically correct code.

I did as you said on transmitters but I have been confused about what should I write on the receiver.

On receiver

pack.source = 1;
   result=d1=pack.distance;

pack.source = 2;
   result=d2=pack.distance;

The LCD is reading d1 and d2 with the same result.

I have tried in some different ways but could not make it(the code) work properly.

I really think I am not writing the code Syntactically and semantically correctly :joy:

I would use something like

  if (pack.source == 1) {
    // do whatever distance of TX1 needs
  } else if (pack.source == 2) {
    // do whatever distance of TX2 needs
  } else {
    Serial.print(F("unexpected packet source received: "));
    Serial.println(pack.source);
  }

on the receiving side, after the successful reception of a packet.

Ok, now I have a distinguishment on the receiver but I have the wrong reading after around 80mm. The LCD does not show more than 250mm in a 4m distance(4000mm).

I am sure that is something related to "uint8_t source" But If I increase the results on LCD becomes 0.
If increasing the "uint16_ t distance;" to "uint32_ t distance;" nothing changes.
I also have tried with "int source;" and the results for "pack.distance" is 0


// RECEIVER


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

#include <LiquidCrystal.h>
const int rs = 10, en = 7, d4 = 6, d5 = 5, d6 = 4, d7 = 3;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

RF24 radio(9, 8); // CE, CSN
const byte addresses[][10] = {"ADDRESS01","ADDRESS02"};


struct Packet {
uint8_t source;
uint16_t distance;
} pack;

int d1;
int d2;
int packet;

void setup() {
  Serial.begin(9600);  
  radio.begin();  
  radio.openWritingPipe(addresses[1]);    // ADDRESS02
  radio.openReadingPipe(1, addresses[0]); // ADDRESS01
  radio.setPALevel(RF24_PA_MAX);

  Serial.println("Loading"); // print some text in Serial Monitor
  Serial.println("Ultra sonic");
  lcd.begin(16, 2); // lcd starts with resolution 16x2
};

void loop() {  
  delay(10);

  radio.startListening();
  if (radio.available()) {                // Receive
    
    radio.read(&pack, sizeof(packet)); 
    

  
  };

if (pack.source == 1) {
 lcd.setCursor(0, 0);
  lcd.print("SENSOR1: ");
  lcd.print(pack.distance);
  lcd.println("mm    ");
  } else if (pack.source == 2) {
  lcd.setCursor(0, 1);
  lcd.print("SENSOR2: ");
  lcd.print(pack.distance);
  lcd.println("mm    ");
  } else {
    Serial.print(F("unexpected packet source received: "));
    Serial.println(pack.source);
  }

delay(1000);



  radio.stopListening();                  // Transmit
  const char txt_sent[] = "Sender 2";
  radio.write(&txt_sent, sizeof(txt_sent));  
  Serial.print("Sent to: ");Serial.println(txt_sent);

Serial.println(pack.source);
Serial.println("");
Serial.println(pack.distance);


};


// TRANSMITTER


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

#define echoPin 5 // attach pin D4 Arduino to pin Echo of HC-SR04
#define trigPin 4 //attach pin D5 Arduino to pin Trig of HC-SR04
long duration; // variable for the duration of sound wave travel
int distance_mm; // variable for mm measurement

RF24 radio(9, 8);          // CE, CSN
const byte addresses[][10] = {"ADDRESS01","ADDRESS02"};

struct Packet {
uint8_t source;
uint16_t distance;
} pack;

int result;
void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(addresses[0]);    // ADDRESS01
  radio.openReadingPipe(1, addresses[1]); // ADDRESS02
  radio.setPALevel(RF24_PA_MAX);

  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
};

void loop() {
 // Clears the trigPin condition
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
    // Calculating the distance
  distance_mm = (duration * 0.034 / 2)*10; // Speed of sound wave divided by 2 (go and back)


  delay(10);
  
  radio.stopListening();                  // Transmit

  pack.source = 1;
  pack.distance = distance_mm;
  result=radio.write(&pack, sizeof(Packet));
  //const char txt_sent[] = "Sender 1";
  //radio.write(&txt_sent, sizeof(txt_sent));  
  //Serial.print("Sent to: ");Serial.println(txt_sent);
  
  delay(10);

  radio.startListening();
  if (radio.available()) {                // Receive
    char txt_received[10] = "";
    radio.read(&txt_received, sizeof(txt_received)); 
    Serial.print("Received from: ");Serial.println(txt_received);
  
  };
 
};


// TRANSMITTER


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

#define echoPin 5 // attach pin D4 Arduino to pin Echo of HC-SR04
#define trigPin 4 //attach pin D5 Arduino to pin Trig of HC-SR04
long duration; // variable for the duration of sound wave travel
int distance_mm; // variable for mm measurement

RF24 radio(9, 8);          // CE, CSN
const byte addresses[][10] = {"ADDRESS01","ADDRESS02"};

struct Packet {
uint8_t source;
uint16_t distance;
} pack;

int result;
void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(addresses[0]);    // ADDRESS01
  radio.openReadingPipe(1, addresses[1]); // ADDRESS02
  radio.setPALevel(RF24_PA_MAX);

  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
};

void loop() {
 // Clears the trigPin condition
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
    // Calculating the distance
  distance_mm = (duration * 0.034 / 2)*10; // Speed of sound wave divided by 2 (go and back)


  delay(10);
  
  radio.stopListening();                  // Transmit

  pack.source = 2;
  pack.distance = distance_mm;
  result=radio.write(&pack, sizeof(Packet));
  //const char txt_sent[] = "Sender 1";
  //radio.write(&txt_sent, sizeof(txt_sent));  
  //Serial.print("Sent to: ");Serial.println(txt_sent);
  
  delay(10);

  radio.startListening();
  if (radio.available()) {                // Receive
    char txt_received[10] = "";
    radio.read(&txt_received, sizeof(txt_received)); 
    Serial.print("Received from: ");Serial.println(txt_received);
  
  };
 
};

The if available ends too early, it should also enclose the if source.

What Arduinos are you using?

I have enclosed the if the source in the radio read but is exactly the same.

I am using an Arduino nano.

I have made it work by dividing the value by 10 and multiplying by 10 at the other end of the pipe.

but I want to know if have any other way to make it work.

So all three nodes are Nanos?

You did not tell me what the value was you processed that way.