Hi guys:
Can someone explain to me how to transmit several variables between two arduino boards with this module and that it recognizes them when receiving them?
I want on a board to have some sensors for telemetry, for example rpm, voltage, speed, etc... And I want to transmit those variables wirelessly to another arduino and for it to recognize them.
I have only seen transmissions with a single variable.
Thanks for the help.
Take a look at Simple nRF24L01+ 2.4GHz transceiver demo to see how to transmit a struct containing multiple variables
+1 for Robin2's tutorial. It helped me a lot. Pay particular attention to the part about providing adequate current to the 3.3V power to the radios.
Here are demo programs that I wrote to transmit joystick data from one Arduino to another. This code uses example code from Robin2's tutorial that @UKHeliBob linked. The data is contained in a struct structure. The beauty of the struct is that it can contain a mixture of data types. It is like an array, but an array can contain only 1 data type.
Transmitter:
#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.stopListening();
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) );
}
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;
}
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.