Problem with communication between two nrf24l01

I am working on a project with two nrf24l01 to control 4 motors using a motor driver. I know that all my hardware are working since I have ran various test code to test motor and simple sending information (nrf24l01 was used to send only one-byte value.) However, when I tried to send the information of two joysticks with ‘struct’ function, the receiver won’t respond to the received information.

This is my code for the transmitter

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

#define VRx1 A0
#define VRy1 A1
#define SW1 2
#define VRx2 A2
#define VRy2 A3
#define SW2 3
#define CE_PIN   9
#define CSN_PIN 10


int xPosition1 = 0;
int yPosition1 = 0;
int SW_state1 = 0;
int mapX1 = 0;
int mapY1 = 0;
int xPosition2 = 0;
int yPosition2 = 0;
int SW_state2 = 0;
int mapX2 = 0;
int mapY2 = 0;

typedef struct {
  byte mapX1;
  byte mapX2;
  byte mapY1;
  byte mapY2;
  byte SW_state1;
  byte SW_state2;
} Data_Package;

Data_Package data;

const byte rxAddr[6] = "00001";


RF24 radio(CE_PIN, CSN_PIN); 

void setup() {
  pinMode(VRx1, INPUT);
  pinMode(VRy1, INPUT);
  pinMode(SW1, INPUT_PULLUP);
  pinMode(VRx2, INPUT);
  pinMode(VRy2, INPUT);
  pinMode(SW2, INPUT_PULLUP);

data.mapX1 = 127;
data.mapX2 = 127;
data.mapY1 = 127;
data.mapY2 = 127;
data.SW_state1 = 1;
data.SW_state2 = 1;

  while (!Serial);
  Serial.begin(9600);
  


  radio.begin();
  radio.setPALevel(RF24_PA_MIN); 
  radio.setDataRate( RF24_250KBPS ); 
  radio.setRetries(15, 15);
  radio.setPayloadSize(6);
  radio.openWritingPipe(rxAddr);
  radio.stopListening();
}

void loop() {
  xPosition1 = analogRead(VRx1);
  yPosition1 = analogRead(VRy1);
  SW_state1 = digitalRead(SW1);
  xPosition2 = analogRead(VRx2);
  yPosition2 = analogRead(VRy2);
  SW_state2 = digitalRead(SW2);

  mapX1 = map(xPosition1, 0, 1023, 0, 255);
  mapY1 = map(yPosition1, 0, 1023, 0, 255);
  mapX2 = map(xPosition2, 0, 1023, 0, 255);
  mapY2 = map(yPosition2, 0, 1023, 0, 255);

  Serial.print("joystickX1: ");
  Serial.print(mapX1);
  Serial.print("; joystickY1: ");
  Serial.print(mapY1);
  Serial.print("; joystickX2: ");
  Serial.print(mapX2);
  Serial.print("; joystickY2");
  Serial.println(mapY2);
  if (SW_state1 == 0) {
    Serial.println("button 1 pressed");
  }
  if (SW_state2 == 0) {
    Serial.println("button 2 pressed");
  }



  radio.write(&data, sizeof(data));

  // Wait a short while before sending the other one
  delay(100);
}

And this is the code for the receiver

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

#define CE_PIN   9
#define CSN_PIN 10
#define in1 4
#define in2 5
#define in3 6
#define in4 7

typedef struct {
  byte mapX1;
  byte mapX2;
  byte mapY1;
  byte mapY2;
  byte SW_state1;
  byte SW_state2;  
} Data_Package;
Data_Package data;

RF24 radio(CE_PIN, CSN_PIN); 

const byte rxAddr[6] = "00001";

void setup()
{

  Serial.begin(9600);

  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

data.mapX1 = 127;
data.mapX2 = 127;
data.mapY1 = 127;
data.mapY2 = 127;
data.SW_state1 = 1;
data.SW_state2 = 1;

  while(!Serial);
  Serial.println("NRF24L01P Receiver Starting...");
  
 
  radio.begin();
  radio.setPALevel(RF24_PA_MIN);  
  radio.setDataRate( RF24_250KBPS ); 
  radio.setPayloadSize(6);
  radio.openReadingPipe(0, rxAddr);
  radio.startListening();
  
}

void loop()
{
  if (radio.available())
  {
    radio.read(&data, sizeof(data));
    
    Serial.println("Received Message: ");

    if (data.mapX1 > 200){Serial.print("Up");
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    // Set Motor B forward
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    }

    if (data.mapX1 > 110 and data.mapX1 < 140){Serial.print("stop");
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);}

    if (data.mapX1 < 74){Serial.print("down");
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    }

    Serial.println("");
  } 
}

You are initializing the values in the struct in "setup" in the sender, you are never assigning the actual values read from the inputs to it, therefore the receiver is always receiving the same data.

Danois90:
You are initializing the values in the struct in "setup" in the sender, you are never assigning the actual values read from the inputs to it, therefore the receiver is always receiving the same data.

I used that to reset the value in the beginning. Does that make problem?

  data.mapX1 = map(xPosition1, 0, 1023, 0, 255);
  data.mapY1 = map(yPosition1, 0, 1023, 0, 255);
  data.mapX2 = map(xPosition2, 0, 1023, 0, 255);
  data.mapY2 = map(yPosition2, 0, 1023, 0, 255);

Etc.

Edit: It’s not a problem you set them in setup, although it’s not needed. The problem is you never set them again in main.

Doesn’t help you have a load of globals, with the same names as the struct fields, which aren’t strictly needed. Just set the field in the struct directly with data.xxx = ..... No need for the intermediate variable.

pcbbc:

  data.mapX1 = map(xPosition1, 0, 1023, 0, 255);

data.mapY1 = map(yPosition1, 0, 1023, 0, 255);
  data.mapX2 = map(xPosition2, 0, 1023, 0, 255);
  data.mapY2 = map(yPosition2, 0, 1023, 0, 255);



Etc.

THANK YOU SO MUCH. YOU MADE IT WORK