Hi every one,
what type of data that is got from NRF24L01 receiver? a String? or Array? or Number?
how to identify a data type in Arduino?
Thanks
Adam
Lots of info here:
The basic answer is, what you give it it will send/receive, within reason. Look closely at the examples, you'll see it could be a string, a float, whatever. It's important that the receiver know what is being sent, of course.
C
1 Like
I usually make my payloads from structs. That way I can include any data type and a mixture of data types if necessary.
Here is an example master sending a struct with joystick data.
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
const byte CE_PIN = 9;
const byte CSN_PIN = 10;
const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
struct JoyValues
{
int joy1x;
int joy1y;
int joy2x;
int joy2y;
}joyValues;
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second
const byte joy1xPin = A0;
const byte joy1yPin = A1;
const byte joy2xPin = A2;
const byte joy2yPin = A3;
void setup()
{
Serial.begin(115200);
Serial.println("SimpleTx Starting");
radio.begin();
radio.setChannel(76); //76 library default
//RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
radio.setPALevel(RF24_PA_HIGH);
radio.setDataRate( RF24_250KBPS );
radio.setRetries(3, 5); // delay, count
radio.openWritingPipe(slaveAddress);
}
void loop()
{
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis)
{
send();
Serial.print("JOY 1 X = ");
Serial.print( joyValues.joy1x);
Serial.print(" JOY 1 Y = ");
Serial.print( joyValues.joy1y);
Serial.print(" JOY 2 X = ");
Serial.print( joyValues.joy2x);
Serial.print(" JOY 2 Y = ");
Serial.println( joyValues.joy2y);
prevMillis = millis();
}
}
//====================
void send()
{
joyValues.joy1x = analogRead(joy1xPin);
joyValues.joy1y = analogRead(joy1yPin);
joyValues.joy2x = analogRead(joy2xPin);
joyValues.joy2y = analogRead(joy2yPin);
radio.write( &joyValues, sizeof(joyValues) );
}
One can easily add character, long , float or string data to that same struct. There is still the 32 character limit that you have to obey, of course.
And the receiver:
// SimpleRx - the slave or the receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
const byte CE_PIN = 9;
const byte CSN_PIN = 10;
const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};
RF24 radio(CE_PIN, CSN_PIN);
struct JoyValues
{
int joy1x;
int joy1y;
int joy2x;
int joy2y;
}joyValues;
bool newData = false;
//===========
void setup()
{
Serial.begin(115200);
Serial.println("SimpleRx Starting");
radio.begin();
radio.setChannel(76); //76 library default
//RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
radio.setPALevel(RF24_PA_HIGH);
radio.setDataRate( RF24_250KBPS );
radio.openReadingPipe(1, thisSlaveAddress);
radio.startListening();
}
//=============
void loop()
{
getData();
showData();
}
//==============
void getData()
{
if ( radio.available() )
{
radio.read( &joyValues, sizeof(joyValues) );
newData = true;
}
}
void showData()
{
if (newData == true)
{
Serial.print("Data received >> ");
Serial.print("JOY 1 X = ");
Serial.print( joyValues.joy1x);
Serial.print(" JOY 1 Y = ");
Serial.print( joyValues.joy1y);
Serial.print(" JOY 2 X = ");
Serial.print( joyValues.joy2x);
Serial.print(" JOY 2 Y = ");
Serial.println( joyValues.joy2y);
newData = false;
}
}
1 Like
Great.
Thank you all.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.