Problem with nRF24 data sending

I want to send multiple data between 2 arduinos and i get only 0 on output pls help. I connected everything as it should be input is from joystick. When I use TransferTimeout example it transfers with no problems.

Code for transfer

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

RF24 radio(7, 8);

const byte rxAddr[6] = "00001";

  
  
typedef struct{
    int x;
    int y;
  }
  joystick;

  joystick joy;
void setup() {

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

void loop() {
  
joy.x = analogRead(A5);
  joy.y = analogRead(A4);
  radio.write( &joy, sizeof(joy) );
      radio.startListening();
      delay(200);

}

and for reciver

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

RF24 radio(7, 8);

const byte rxAddr[6] = "00001";

typedef struct{
    int x;
    int y;
    }
    joystick;
    joystick joy;
void setup() {

  Serial.begin(9600);
  radio.begin();
  radio.setRetries(15, 100);
  radio.openWritingPipe(rxAddr);
  radio.startListening();
  
}

void loop() {
  // put your main code here, to run repeatedly:
  
  radio.read( &joy, sizeof(joy) );
  Serial.print("joy.x = ");
    Serial.println(joy.x);
    Serial.print("joy.y = ");
    Serial.println(joy.y);
    delay(200);
  
}

Have a look at this Simple nRF24L01+ Tutorial.

Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with. Start by getting the first example to work

There is also a connection test program to check that the Arduino can talk to the nRF24 it is connected to.

...R

Problem in my situation is that I need to send variables. I sucessfully transfered text by myself before but i dont know how to send variables by using structure. :confused:

First you need to declare your struct right:

struct TYPENAME { struct members }; //This is right
struct { struct members } VARIABLENAME; //This is wrong

You should also make sure that the size of the payload matches the size of the message with "radio.setPayloadSize(sizeof(TYPENAME))".

Even though it not causes a problem, you should set pinMode for the analog pins before reading from them. I would also make some Serial.prints for the "joy" variable on the transmitter end to be sure that the analog reads actually produces values other than zero.

Oh thanks I'll try it later. Thanks for explaining how structure work.

struct { struct members } VARIABLENAME; //This is wrong

Whilst it does not define a type is it really wrong ?

UKHeliBob:
Whilst it does not define a type is it really wrong ?

If OP wants to use the struct as a type, which it seems like due to the "typedef" and the "joystick joy;" declarations, then I would consider it wrong. If the struct is only to be used as a single instance variable, then it is not wrong.

If OP wants to use the struct as a type, which it seems like due to the "typedef"

I rather suspect that it was copied from old examples rather than being a conscious decision

Ok so i write code

transmiter

    #include <SPI.h>  
    #include "RF24.h"
    RF24 myRadio (7, 8);

        struct package
    {
      int id=1;
      float temperature = 18.3;
      char  text[100] = "Text to be transmitted";
    };
    typedef struct package Package;
    Package data;
const byte rxAddr[6] = "00001";
    
    void setup()
    {
      Serial.begin(9600);
      delay(1000);
      myRadio.begin();  
      myRadio.setChannel(115); 
      myRadio.setPALevel(RF24_PA_MAX);
      myRadio.setDataRate( RF24_250KBPS ) ; 
      myRadio.openWritingPipe(rxAddr);
      delay(1000);
    }

        void loop()
    {
      myRadio.write(&data, sizeof(data)); 
      Serial.print("\nPackage:");
      Serial.print(data.id);
      Serial.print("\n");
      Serial.println(data.temperature);
      Serial.println(data.text);
      delay(1000);
    }

and for reciver

    #include <SPI.h>  
    #include "RF24.h"
const byte rxAddr[6] = "00001";
    RF24 myRadio (7, 8);
    struct package
    {
      int id=0;
      float temperature = 0.0;
      char  text[100] ="empty";
    };
 
    package data;
    
    byte addresses[][6] = {"0"};
    
    void setup() 
    {
      Serial.begin(9600);
      delay(1000);
      myRadio.begin(); 
      myRadio.setChannel(115); 
      myRadio.setPALevel(RF24_PA_MAX);
      myRadio.setDataRate( RF24_250KBPS ) ; 
      myRadio.openReadingPipe(1, rxAddr);
      myRadio.startListening();
    }
        void loop()  
    {
      if ( myRadio.available()) 
      {
        while (myRadio.available())
        {
          myRadio.read( &data, sizeof(data) );
        }
        Serial.print("\nPackage:");
        Serial.print(data.id);
        Serial.print("\n");
        Serial.println(data.temperature);
        Serial.println(data.text);
      }
    }

If enyone know what the hell is bad with it please write

char  text[100] = "Text to be transmitted";

How many bytes can the NRF24 send with one write() command ?

If someone can write code that can send variables I'll be happy because rly don't know what i do wrong that it doesn't work

rly don't know what i do wrong that it doesn't work

How many bytes can the NRF24 send with one write() command ?

The answer is 32
How many bytes are you trying to send ?

i know i changed that to 32 and still nothing

i changed that to 32 and still nothing

If you are sending a struct then the total size of the struct must not exceed 32 bytes, not just each element of it

Post the Tx and Rx programs as they are now

transmiter

 #include <SPI.h>  
    #include "RF24.h"
        RF24 myRadio (7, 8);
const byte rxAddr[6] = "00001";
        struct package
{
  int id=1;
  float temperature = 18.3;
  char  text[32] = "elon";
};
typedef struct package Package;
Package data;
    void setup()
    {
      Serial.begin(9600);
      delay(1000);
      myRadio.begin();  
      myRadio.setChannel(115); 
      myRadio.setPALevel(RF24_PA_MAX);
      myRadio.setDataRate( RF24_250KBPS ) ; 
      myRadio.openReadingPipe(1, rxAddr);
      delay(1000);
    }
        void loop()
    {
      myRadio.write(&data, sizeof(data)); 
      Serial.print("\nPackage:");
      Serial.print(data.id);
      Serial.print("\n");
      Serial.println(data.temperature);
      Serial.println(data.text);
      data.id = data.id + 1;
      data.temperature = data.temperature+0.1;
      delay(1000);
    }[

reciver

    #include <SPI.h>  
    #include "RF24.h"
const byte rxAddr[6] = "00001";
    RF24 myRadio (7, 8);
    struct package
    {
      int id=0;
      float temperature = 0.0;
      char  text[32] ="";
    };
 
    package data;
    
    
    void setup() 
    {
      Serial.begin(9600);
      delay(1000);
      myRadio.begin(); 
      myRadio.setChannel(115); 
      myRadio.setPALevel(RF24_PA_MAX);
      myRadio.setDataRate( RF24_250KBPS ) ; 
      myRadio.openReadingPipe(1, rxAddr);
      myRadio.startListening();
    }
        void loop()  
    {
      if ( myRadio.available()) 
      {
        while (myRadio.available())
        {
          myRadio.read( &data, sizeof(data) );
        }
        Serial.print("\nPackage:");
        Serial.print(data.id);
        Serial.print("elo");
        Serial.println(data.temperature);
        Serial.println(data.text);
      }
    }
struct package
{
  int id = 1;
  float temperature = 18.3;
  char  text[32] = "elon";
};

I count 38 bytes. Gonna be tough to squeeze that into the 32 bytes allowed for payload.

typedef struct package Package;
Package data;

What is this ?

You have a struct whose data type is package. To declare an instance of that struct you just need to do

package data;

As has been pointed out again the size of the struct is still too big

You can only send 32 bytes of data in one message, but you can send larger amounts of data in multiple messages / chunks. You should ask yourself wheter you need to send any text at all? You could send a "text_id" identifying the text which is known to both ends..