Hi all!
I have connected Nrf24lo1+ with 3pi robot with following connections:
and I am using arduino libraries for nrf24 module.
1- I have uncommented #SPI_UART in RF24_config.h file
2- I have changed pins according to my connection in RF24_config.h file and pins_arduino.h
3- I have 2 3pi robots and i want to communicate both robots through their nrf modules.
lib link: https://github.com/tmrh20/RF24
unfortunatly, I haven’t been able to succeed. Kindly help me. Maybe I’m not using the right library
nrf24l01+ pins 3pi Robot pins Arduino library pin mapping
CE PD7 7
CSN PB0 8
SCK PD4 4
MISO PD0 0
MOSI PD1 1
IRQ PD2 2
SENDER CODE:
/*
* Getting Started example sketch for nRF24L01+ radios
* This is a very basic example of how to send data from one node to another
* Updated: Dec 2014 by TMRh20
*/
#include "RF24.h"
#include <SPI_UART.h>
//#include <SPI_UART.cpp>
#include <Pololu3pi.h>
#include <PololuQTRSensors.h>
#include <OrangutanMotors.h>
#include <OrangutanAnalog.h>
#include <OrangutanLEDs.h>
#include <OrangutanLCD.h>
#include <OrangutanPushbuttons.h>
#include <OrangutanBuzzer.h>
//#include "printf.h"
#define PD_7 7
#define PB_0 8
#define PD_4 4
#define PD_0 0
#define PD_1 1
#define PD_2 2
/****************** User Config ***************************/
/*** Set this radio as radio number 0 or 1 ***/
bool radioNumber = 1;
/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
RF24 radio(PD_7,PB_0);
/**********************************************************/
byte addresses[][6] = {"1Node","2Node"};
// Used to control whether this node is sending or receiving
bool role = 1;
void setup() {
pinMode(PD_7,OUTPUT);
pinMode(PB_0, OUTPUT);
pinMode(PD_4,OUTPUT);
pinMode(PD_0, INPUT);
pinMode(PD_1, OUTPUT);
pinMode(PD_2,OUTPUT);
radio.begin();
// Set the PA Level low to prevent power supply related issues since this is a
// getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
radio.setPALevel(RF24_PA_LOW);
// Open a writing and reading pipe on each radio, with opposite addresses
if(radioNumber){
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1,addresses[0]);
}else{
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
}
// Start the radio listening for data
radio.startListening();
}
void loop() {
/****************** Ping Out Role ***************************/
if (role == 1) {
radio.stopListening(); // First, stop listening so we can talk.
//Serial.println(F("Now sending"));
unsigned long start_time = 30; // Take the time, and send it. This will block until complete
if (!radio.write( &start_time, sizeof(unsigned long) )){
;//Serial.println(F("failed"));
}
OrangutanMotors::setSpeeds(0,start_time);
// Try again 1s later
delay(1000);
}
/****************** Pong Back Role ***************************/
if ( role == 0 )
{
radio.startListening();
unsigned long got_time = 0;
if( radio.available()){
// Variable for the received timestamp
while (radio.available()) { // While there is data ready
radio.read( &got_time, sizeof(unsigned long) ); // Get the payload
OrangutanMotors::setSpeeds(got_time,0);
}
radio.stopListening(); // First, stop listening so we can talk
radio.write( &got_time, sizeof(unsigned long) ); // Send the final one back.
radio.startListening(); // Now, resume listening so we catch the next packets.
//Serial.print(F("Sent response "));
//Serial.println(got_time);
}
}
/****************** Change Roles via Serial Commands ***************************/
} // Loop
RECEIVER CODE:
/*
* Getting Started example sketch for nRF24L01+ radios
* This is a very basic example of how to send data from one node to another
* Updated: Dec 2014 by TMRh20
*/
#include "RF24.h"
#include <SPI_UART.h>
//#include <SPI_UART.cpp>
#include <Pololu3pi.h>
#include <PololuQTRSensors.h>
#include <OrangutanMotors.h>
#include <OrangutanAnalog.h>
#include <OrangutanLEDs.h>
#include <OrangutanLCD.h>
#include <OrangutanPushbuttons.h>
#include <OrangutanBuzzer.h>
//#include "printf.h"
#define PD_7 7
#define PB_0 8
#define PD_4 4
#define PD_0 0
#define PD_1 1
#define PD_2 2
/****************** User Config ***************************/
/*** Set this radio as radio number 0 or 1 ***/
bool radioNumber = 0;
/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
RF24 radio(PD_7,PB_0);
/**********************************************************/
byte addresses[][6] = {"1Node","2Node"};
// Used to control whether this node is sending or receiving
bool role = 0;
void setup() {
pinMode(PD_7,OUTPUT);
pinMode(PB_0, OUTPUT);
pinMode(PD_4,OUTPUT);
pinMode(PD_0, INPUT);
pinMode(PD_1, OUTPUT);
pinMode(PD_2,OUTPUT);
//Serial.begin(115200);
//Serial.println(F("RF24/examples/GettingStarted"));
//Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
radio.begin();
// Set the PA Level low to prevent power supply related issues since this is a
// getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
radio.setPALevel(RF24_PA_LOW);
// Open a writing and reading pipe on each radio, with opposite addresses
if(radioNumber){
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1,addresses[0]);
}else{
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
}
// Start the radio listening for data
radio.startListening();
}
void loop() {
/****************** Ping Out Role ***************************/
if (role == 1) {
radio.stopListening(); // First, stop listening so we can talk.
//Serial.println(F("Now sending"));
unsigned long start_time = micros(); // Take the time, and send it. This will block until complete
if (!radio.write( &start_time, sizeof(unsigned long) )){
;//Serial.println(F("failed"));
}
radio.startListening(); // Now, continue listening
unsigned long started_waiting_at = micros(); // Set up a timeout period, get the current microseconds
boolean timeout = false; // Set up a variable to indicate if a response was received or not
while ( ! radio.available() ){ // While nothing is received
if (micros() - started_waiting_at > 200000 ){ // If waited longer than 200ms, indicate timeout and exit while loop
timeout = true;
break;
}
}
if ( timeout ){ // Describe the results
;//Serial.println(F("Failed, response timed out."));
}else{
unsigned long got_time; // Grab the response, compare, and send to debugging spew
radio.read( &got_time, sizeof(unsigned long) );
unsigned long end_time = micros();
}
// Try again 1s later
delay(1000);
}
/****************** Pong Back Role ***************************/
if ( role == 0 )
{
radio.startListening();
unsigned long got_time = 0;
if( radio.available()){
// Variable for the received timestamp
while (radio.available()) { // While there is data ready
radio.read( &got_time, sizeof(unsigned long) ); // Get the payload
// OrangutanMotors::setSpeeds(got_time,0);
}
//OrangutanMotors::setSpeeds(got_time,0);
//radio.stopListening(); // First, stop listening so we can talk
//radio.write( &got_time, sizeof(unsigned long) ); // Send the final one back.
//radio.startListening(); // Now, resume listening so we catch the next packets.
//Serial.print(F("Sent response "));
//Serial.println(got_time);
}
}
} // Loop