NRF24L01 Sending random numbers

Hello,

for a project, I am working on I need to send numbers (random) from the transmitter to the receiver but it doesn't work. I don't find what it could be. I think it is something whit the char I am sending in.

transmitter ;

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
int randNumber = 0;
char Data[32];
RF24 transmit (2, 3);                   //create RF24 object called transmit

byte address [6] = "00001";             //set address to 00001

void setup() {
  transmit.begin();
  transmit.openWritingPipe(address);    //open writing pipe to address 00001
  transmit.setPALevel(RF24_PA_MIN);     //set RF power output to minimum
  transmit.setDataRate(RF24_250KBPS);   //set datarate to 250kbps
  transmit.setChannel(100);             //set frequency to channel 100
  transmit.stopListening();
}

void loop() {
  randNumber = random(100, 120);
  dtostrf(randNumber,10, 2, Data);       //send character string upto 32 bytes long
  transmit.write(&Data, sizeof(Data));  //transmit the data
  delay(1000);
}

receiver ;

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>

RF24 receive (2,3);                         //create object called receive
byte address [5] = "00001" ;
//creat an array with 5 elements, where each element is 1 byte;

void setup() {
  Serial.begin(19200);
  Serial.print("Starting Receiver \n");
  receive.begin();
  receive.openReadingPipe(0,address);      //open reading pipe 0 at address 00001
  receive.setPALevel(RF24_PA_MIN);         //Set RF output to minimum
  receive.setDataRate(RF24_250KBPS);       //set datarate to 250kbps
  receive.setChannel(100);                 //set frequency to channel 100
  receive.startListening();                

  }

void loop() {

  if (receive.available())                //check when received data available
  {
    char Data[32];
    receive.read(&Data, sizeof(Data));
    Serial.print(Data);                   //print data to serial monitor
  }
}

If someone could help me it would be very nice of you.

"it does not work" means very little.

Do standard examples work?

No experience with nrf24.

Why do you use dtostrf to convert an integer to text? sprintf is the usual approach

sprint(Data, "%d", randNumber);

And although I think the compiler accepts it and does the right thing, the '&" in write is not needed (or wrong)

transmit.write(Data, sizeof(Data));

And in the receiver, below is wrong

byte address [5] = "00001" ;

This is a c-string which takes 6 bytes; you did it right in the transmitter.

Do the examples in Simple nRF24L01+ 2.4GHz transceiver demo work for you ?

the example I used worked but it was "hello world" I tried to replace hello word whit a random number because I am trying to send data from a GY-521 to another Arduino. with what I got now it does this

serial monitor ;

Starting Receiver
Data =
Data =
Data =
Data =
as you can see it doesn't read any number.
and this is the code I changed as you said.

transmitter;

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
int randNumber = 0;
char Data[32];
RF24 transmit (9, 10);                   //create RF24 object called transmit

byte address [6] = "00001";             //set address to 00001

void setup() {
  Serial.begin(9600);
  transmit.begin();
  transmit.openWritingPipe(address);    //open writing pipe to address 00001
  transmit.setPALevel(RF24_PA_MIN);     //set RF power output to minimum
  transmit.setDataRate(RF24_250KBPS);   //set datarate to 250kbps
  transmit.setChannel(100);             //set frequency to channel 100
  transmit.stopListening();
}

void loop() {
  randNumber = random(10, 15);
  sprintf(randNumber,"%d", Data);       //send character string upto 32 bytes long
  transmit.write(Data, sizeof(Data));  //transmit the data
  Serial.println(Data);
  delay(1000);

receiver ;

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>

RF24 receive (9,10);                         //create object called receive
byte address [6] = "00001" ;
//creat an array with 5 elements, where each element is 1 byte;

void setup() {
  Serial.begin(9600);
  Serial.print("Starting Receiver \n");
  receive.begin();
  receive.openReadingPipe(0,address);      //open reading pipe 0 at address 00001
  receive.setPALevel(RF24_PA_MIN);         //Set RF output to minimum
  receive.setDataRate(RF24_250KBPS);       //set datarate to 250kbps
  receive.setChannel(100);                 //set frequency to channel 100
  receive.startListening();                

  }

void loop() {

  if (receive.available())                //check when received data available
  {
    char Data[32];
    receive.read(Data, sizeof(Data));
     Serial.print("Data = ");
    Serial.println(Data);                   //print data to serial monitor
  }
}

I thank you for the things you already did.

Instead of this in the Tx

  randNumber = random(10, 15);
  sprintf(randNumber,"%d", Data);       //send character string upto 32 bytes long
  transmit.write(Data, sizeof(Data));  //transmit the data
  Serial.println(Data);

Do this

  int randNumber = random(10, 15);
  transmit.write(randNumber, sizeof(randNumber));  //transmit the data
  Serial.println(randNumber);

and on the Rx side instead of this

    char Data[32];
    receive.read(Data, sizeof(Data));
     Serial.print("Data = ");
    Serial.println(Data);                   //print data to serial monitor

Do this

    int randNumber;;
    receive.read(randNumber, sizeof(randNumber));
     Serial.print("randNumber = ");
    Serial.println(randNumber);                   //print data to serial monitor
sprintf(randNumber,"%d", Data);       //send character string upto 32 bytes long
  transmit.write(Data, sizeof(Data));

Compare your arrangement of sprintf with sterretje's example

sprint(Data, "%d", randNumber);
transmit.write(Data, sizeof(Data));

You have the output buffer and starting value reversed.

thank u for the help but whit everything you say it still doesn't work. any other advice?
in my serial monitor from the transmitter, I got data = (random number) but in my receiver it still says nothing.

#include <SPI.h>

Shouldn't this be in your code?

the example I used worked but it was "hello world"

Please post your working example code.

maybe, I placed it in my sketch but I still have the same result.

6-echo:
thank u for the help but whit everything you say it still doesn't work. any other advice?

Have you tried the first example in the link in Reply #2 with NO CHANGES to the example code?

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.

A common problem with nRF24 modules is insufficient 3.3v current from the Arduino 3.3v pin. At least for testing try powering the nRF24 with a pair of AA alkaline cells (3v) with the battery GND connected to the Arduino GND.

...R