Beginning of this year I bought my first nRF24L01 wireless modules. I did not have a clue back then how they worked and if I was going to get them to work with Arduino. I had found a couple of libraries for the modules so I thought of giving it a try. I spent hours trying to make them work, it took me hours to sent a simple integer from one Arduino to the other. Some time ago I have made my own remote control using a nRF24L01 module. I’ve written sketches for it so it can remote control my hexapod or my Rover 5. My Rover 5 has become my development platform. I’ve used it try out lots of different sensors. I’ve played with the encoders, tried out several libraries for the ultrasonic sensor.
When you have an Arduino on your desk you can use the serial monitor to see what is going on. But with a robot that is driving around that becomes a bit more difficult. My remote control has a 4×20 characters LCD, it is very handy to display the readings of the various sensors on the robot and display them on the LCD in my remote control.
In the beginning I just used an array of integers to sent over the wireless link. I recently wanted to sent an array of integers and an array of floats. Since you can not put different types in one array (?) I needed a different solution. I know very little about C++ so I started searching for a solution, the answer to my problem was a typedef struct. So I made the following sketches to try it out. I’ve also borrowed a couple of lines of code from the example sketches that come with the RF24 library. One Arduino has a bool “timout”, when it is true it will light a led. So you know when there is something wrong with the communication. It’s pretty handy with a robot, I shut down the motors on my robots if the link is lost.
Here are the sketches:
Arduino 1
// http://www.bajdi.com
// Sending a struct with the nRF24L01 module
// Data to be sent is the reading of 2 analog pins
// Data received is the analog reading of 2 pins on the other Arduino
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
RF24 radio(1,2); // make sure this corresponds to the pins you are using
const uint64_t pipes[2] = {
0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
const int led = 0;
typedef struct{
int A;
int B;
float C;
float D;
}
A_t;
typedef struct{
int W;
int X;
float Y;
float Z;
}
B_t;
A_t duino1;
B_t duino2;
void setup()
{
Serial.begin(57600);
pinMode(led, OUTPUT);
radio.begin();
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1,pipes[1]);
}
void loop(void)
{
// we need data to sent...
duino2.W = analogRead(A0);
duino2.X = analogRead(A1);
duino2.Y = analogRead(A0)/102.3;
duino2.Z = analogRead(A1)/102.3;
// end of analog reading
// radio stuff
radio.stopListening();
bool ok = radio.write( &duino2, sizeof(duino2) );
radio.startListening();
unsigned long started_waiting_at = millis();
bool timeout = false;
while ( ! radio.available() && ! timeout )
if (millis() - started_waiting_at > 250 )
timeout = true;
if ( timeout )
{
Serial.println("Failed, response timed out.");
digitalWrite(led, HIGH);
}
else
{
radio.read( &duino1, sizeof(duino1) );
digitalWrite(led, LOW);
}
// end of radio stuff
// serial print received data
Serial.print("duino1.A = ");
Serial.println(duino1.A);
Serial.print("duino1.B = ");
Serial.println(duino1.B);
Serial.print("duino1.C = ");
Serial.println(duino1.C);
Serial.print("duino1.D = ");
Serial.println(duino1.D);
// end of serial printing
}
Arduino 2
// http://www.bajdi.com
// Sending a struct with the nRF24L01 module
// Data to be sent is the reading of 2 analog pins
// Data received is the analog reading of 2 pins on the other Arduino
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
typedef struct{
int A;
int B;
float C;
float D;
}
A_t;
typedef struct{
int W;
int X;
float Y;
float Z;
}
B_t;
A_t duino1;
B_t duino2;
RF24 radio(8,9); // make sure this corresponds to the pins you are using
const uint64_t pipes[2] = {
0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
void setup()
{
Serial.begin(57600);
radio.begin();
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1,pipes[0]);
radio.startListening();
}
void loop(void)
{
// we need data to sent...
duino1.A = analogRead(A0);
duino1.B = analogRead(A1);
duino1.C = analogRead(A0)/102.3;
duino1.D = analogRead(A1)/102.3;
// end of analog reading
// radio stuff
if ( radio.available() )
{
bool done = false;
while (!done)
{
done = radio.read( &duino2, sizeof(duino2) );
}
radio.stopListening();
radio.write( &duino1, sizeof(duino1) );
radio.startListening();
}
// end of radio stuff
// serial print received data
Serial.print("duino2.W = ");
Serial.println(duino2.W);
Serial.print("duino2.X = ");
Serial.println(duino2.X);
Serial.print("duino2.Y = ");
Serial.println(duino2.Y);
Serial.print("duino2.Z = ");
Serial.println(duino2.Z);
// end of serial printing
}






