nRF24L01+ module couldn't be used to transmit the int type message

i want to use the nRF24L01+ module to transmit the signal about themessage of 2-Axis Joystick module ,i try to add a variable to contain the valuable of the massage about X_PIN and Y_PIN,but it seems like it couldn't transmit the massage apart from the type of char
and my code
about transmit

//Include Libraries
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

const int SW_pin = 6; // 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
int a,b,c;


//create an RF24 object
RF24 radio(9, 8);  // CE, CSN

//address through which two modules communicate.
const byte address[6] = "00001";

void setup()
{
  //pinMode(SW_pin, INPUT);
  //digitalWrite(SW_pin, HIGH);
  radio.begin();
  
  //set the address
  radio.openWritingPipe(address);
  
  //Set module as transmitter
  radio.stopListening();
  Serial.begin(9600);
}
void loop()
{
  //Send message to receiver
  const char text[] = "switch: ";
  a=digitalRead(SW_pin);
  b=analogRead(Y_pin);
  c=analogRead(X_pin);
  radio.write(&text, sizeof(text));
  radio.write(&a,sizeof(a));
  radio.write(&c,sizeof(c));
  radio.write(&b,sizeof(b));

 

  
  delay(1000);
}

and about the receiver

//Include Libraries
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

//create an RF24 object
RF24 radio(9, 8);  // CE, CSN

//address through which two modules communicate.
const byte address[6] = "00001";


void setup()
{
  while (!Serial);
    Serial.begin(9600);
  
  radio.begin();
  
  //set the address
  radio.openReadingPipe(0, address);
  
  //Set module as receiver
  radio.startListening();
}

void loop()
{
  //Read the data if available in buffer
  if (radio.available())
  {
    char text[32] = {0};
    int a;
    int c;
    int b;
    radio.read(&text, sizeof(text));
    radio.read(&a, sizeof(a));
    radio.read(&c, sizeof(c));
    radio.read(&b, sizeof(b));
    Serial.println(text);
    Serial.println(a );
    Serial.print(c );
    Serial.println(b );
  }
}

Your topic does not indicate a problem with IDE 1.x and therefore has been moved to a more suitable location on the forum.

oh i have made a mistake :joy:

is there any other statement could be used to replace the [radio.read(&....,sizeof(...))],i think maybe here is someting wrong

You will probably want to look at and understand Data Structures: https://cplusplus.com/doc/tutorial/structures/

I would also suggest looking at Integers (The GNU C Library) because it often helps to specify your variable sizes such as with int16_t instead of int or uint16_t (instead of unsigned int).

Basically, you want to put all your data into one 'structure' and then you can transmit it all at once.

This example shows the usage transmitting and receiving data using a data structure. It may seem complex, but most of the code is just calls to Serial.print();

Each call to the write() function represents a different piece of data being sent.

if your payload is composed of the text and 3 variables, then build a struct and send the struct

struct Payload {
  bool  swicthState;
  int x;
  int y;
 char text[10];
};

then fill in the payload

Payload payload; 
...
  payload.swicthState = (digitalRead(SW_pin) == LOW) ;
  payload. x = analogRead(X_pin);
  payload. y = analogRead(Y_pin);
  strlcpy(payload. text, "message", sizeof payload. text); 

and send it

radio.write(&payload, sizeof payload);

Oh ,you reminded me,you have a point,thanks

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