NRF24L01 + Joystick Module's ButtonState Problem in Receiver section

I am using arduino mega 2560, nrf24 module and arduino joythick module (https://images-na.ssl-images-amazon.com/images/I/61NeJDN7ACL.SL1024.jpg).

This is my transmitter code:

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

RF24 radio(4, 5); // CE - CSN ( csn == SS )
const byte rxAddr[6] = "00001";
// int data[9];
uint8_t data[9];
// Arduino pin numbers
#define joyX A0
#define joyY A1 
#define SW_pin 3 // Click Button on Joystick
// Arduino pin numbers
//const int SW_pin = 2; // digital pin connected to switch output
//const int X_pin = 0; // analog pin connected to X output
//const int Y_pin = 1; // analog pin connected to Y output
boolean buttonState = 0;

void setup()
{
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);


  
  Serial.begin(115200);
  radio.begin();
  radio.setRetries(15, 15);
  radio.openWritingPipe(rxAddr);
  
  radio.stopListening();
}

void loop()
{
  //const char text[] = "Hello World";
  //radio.write(&text, sizeof(text));
  //digitalWrite(SW_pin, HIGH);

  int xValue = analogRead(joyX);
  xValue = map(xValue, 0,1023, 0, 256);
  
  int yValue = analogRead(joyY);
  yValue = map(yValue,0,1023,0,256);

  int buttonState_1 = digitalRead(SW_pin); // joystick 1

  
  
  data[0] = xValue; // analogRead(joyX)
  data[1] = yValue; // analogRead(joyY)
  data[3] = buttonState_1;
  radio.write(&data, sizeof(data));


  if (!digitalRead(SW_pin))
  {
    Serial.println("Button is pressed: "); 
  }else
  {
     Serial.println("Button is NOT pressed !!!");
  }
  
  Serial.print("Button Switch:  ");
  Serial.print(buttonState_1);
  Serial.print("\n");
  Serial.print("X-axis: ");
  Serial.print(xValue);
  Serial.print("\n");
  Serial.print("Y-axis: ");
  Serial.println(yValue);
  Serial.print("\n\n");

  
  delay(500);
}

This is my receiver code:

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

RF24 radio(4, 5); // CE - CSN ( csn == SS )

const byte rxAddr[6] = "00001";
//int data[9]; // defines size of array
uint8_t data[9];
void setup()
{
  while (!Serial);
  Serial.begin(115200);
  
  radio.begin();
  radio.openReadingPipe(0, rxAddr);
  
  radio.startListening();
}

void loop()
{
  if (radio.available())
  {
    //char text[32] = {0};
    //radio.read(&text, sizeof(text));
    radio.read(&data, sizeof(data));
    //Serial.println(text);
    if(data[2] == 1){
      Serial.print("Button Switch:  ");
      Serial.print(data[2]);
      Serial.print("  - Button is not pressed");
      Serial.print("\n");
      }else if(data[2] == 0){
        Serial.print("Button Switch:  ");
        Serial.print(data[2]);
        Serial.print("  - Button is pressed !!!");
        Serial.print("\n");
        }    
    
    //Serial.print("Button Switch:  ");
    //Serial.print(data[2]);
    //Serial.print("\n");
    Serial.print("X valuses,data[0]=");
    Serial.println(data[0]);
    Serial.print("Y valuses,data[1]=");
    Serial.println(data[1]);
   

    //Serial.print("1st Joystick Button state, data[2]=");
    //Serial.println(data[2]);
  }
}

My two arduino mega boards are communicating with each other that I can send the data of analogReads to 2nd receiver arduino successfull but I cannot get the exact current state of the button at serial monitor that button state is always in “Pressed” state at receiver side while the transmitter’s serial monitor it shows the correct state.

Do you know what to fix this problem ? thank you.

Note:

Should I write the data array , while defining as uint8_t (byte) or int ? thank you.

In your Tx program you seem to be writing the button state to data[3] and in the Rx program you seem to be trying to read it from data[2]

...R

Robin2: In your Tx program you seem to be writing the button state to data[3] and in the Rx program you seem to be trying to read it from data[2]

...R

Robin you are the life saver man really. Today, I was tried the suggestions people said me for the usb host shield connectivity and nrf24 at the same time from my previous thread a few days ago which thy use the same SPI. I was so tired all day today and failed every time to do it so I decided to do your tutorial for nrf that I put a capacitor to nrf24 and connected the cables normally instead of nrf24 module. And everything is working now^^. That data[3] escaped from my eyes. Thank you robin again.