Hello, I'm having some issues with getting two nRF24Ls to connect to each other. I'm building a basic RC Car with a brushless motor and servo powered by LiPos and two Arduino Nanos. This used to work, but a short killed the Uno I was using, and after migrating to a new Nano I can't get my Remote to control the car. I've dug through several videos and posts over the topic and have found some errors, but none have allowed my remote to control the servo or motor. I've also verified all my connections are working with a multimeter to my radio modules. I'll include a picture of my diagrams and codes. I'm posting here to hopefully rule out a code error, as I'm sure my code has holes in it not allowing it to work properly. I've tried to include all info to help I can, let me know if you need more.
Note: This mainly isn't my code, its someone else's I've edited quite a bit.
Sorry for everything looking so first gradeish.
#include <SPI.h>
#include <RF24.h>
RF24 radio(9, 10); // nRF24L01 (CE, CSN)
const byte address[6] = "00001"; // Address
// Max size of this struct is 32 bytes - NRF24L01 buffer limit
struct Data_Package {
byte j1PotY;
byte j2PotX;
};
Data_Package data; //Create a variable with the above structure
void setup() {
Serial.begin(9600);
pinMode(27, OUTPUT);
// Define the radio communication
radio.begin();
radio.openWritingPipe(address);
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_LOW);
radio.stopListening();
// Set initial default values
// Values from 0 to 255. When Joystick is in resting position, the value is in the middle, or 127. We actually map the pot value from 0 to 1023 to 0 to 255 because that's one BYTE value
data.j1PotY = 127;
data.j2PotX = 127;
}
void loop() {
// Read all analog inputs and map them to one Byte value
data.j1PotY = map(analogRead(A0), 0, 1023, 0, 255);
data.j2PotX = map(analogRead(A2), 0, 1023, 0, 255);
// Send the whole data from the structure to the receiver
radio.write(&data, sizeof(Data_Package));
}
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
RF24 radio(9, 10); // nRF24L01 (CE, CSN)
const byte address[6] = "00001";
unsigned long lastReceiveTime = 0;
unsigned long currentTime = 0;
Servo esc; // create servo object to control the ESC
Servo servo1;
int escValue, servo1Value, servo2Value;
// Max size of this struct is 32 bytes - NRF24L01 buffer lit
struct Data_Package {
byte j1PotY;
byte j2PotX;
};
Data_Package data; //Create a variable with the above structure
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_LOW);
radio.startListening(); // Set the module as receiver
resetData();
esc.attach(6, 1000, 2000); // Arduino digital pin D10 - CH9 on PCB board
servo1.attach(5); // D3 - CH1
}
void loop() {
// Check whether we keep receving data, or we have a connection between the two modules
currentTime = millis();
if ( currentTime - lastReceiveTime > 250 ) { // If current time is more then 1 second since we have recived the last data, that means we have lost connection
resetData(); // If connection is lost, reset the data. It prevents unwanted behavior, for example if a drone jas a throttle up, if we lose connection it can keep flying away if we dont reset the function
}
// Check whether there is data to be received
if (radio.available()) {
radio.read(&data, sizeof(Data_Package)); // Read the whole data and store it into the 'data' structure
lastReceiveTime = millis(); // At this moment we have received the data
}
// Controlling servos
servo1Value = map(data.j2PotX, 0, 255, 0, 180); // Map the receiving value form 0 to 255 to 0 to 180(degrees), values used for controlling servos
servo1.write(servo1Value);
// Controlling brushless motor with ESC
escValue = map(data.j1PotY, 127, 255, 1000, 2000); // Map the receiving value form 127 to 255 to 1000 to 2000, values used for controlling ESCs
esc.writeMicroseconds(escValue); // Send the PWM control singal to the ESC
}
void resetData() {
data.j1PotY = 127;
data.j2PotX = 127;
}