I am trying to transmit the values of three potentiometers via the nRF24L01+ (w/antenna). I am using the regulator designed for the nRF24L01+, so I don't think I need to add a decoupling capacitor. I am using a data structure to send the POT outputs as bytes. On the transmitter end, the values of the data structure are correct. On the receiver, however, each value of the data structure is being received as 0. I have an if statement to verify if radio.available() and apparently the radio is available. If I change the wiring of either the receiver or the transmitter, the radio is not available, so I am assuming it is wired correctly. Plus, the regulator makes it pretty simple.
I have :
- CE = D7
- CSN = D8
- (and all the other mandatory pins 11, 12, 13 connected correctly) and D10 set to output with nothing connected. (I read on another post that D10 must be set to output to make it the SPI master, or something like that)...
I have been trying for days and can't seem to figure out what I'm doing wrong. Let me know if you need anymore information.
//Transmitter//
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
// Define the digital inputs
#define liftPOT A2 // Joystick button 1
#define thrustPOT A0 // Joystick button 2
#define rudderPOT A1 // Toggle switch 1
RF24 radio(7, 8); // nRF24L01 (CE, CSN)
const byte address[6] = "00001"; // Address
// Max size of this struct is 32 bytes - NRF24L01 buffer limit
struct Data_Package {
byte thrustPOT;
byte liftPOT;
byte rudderPOT;
};
Data_Package data; //Create a variable with the above structure
void setup() {
Serial.begin(9600);
// Define the radio communication
radio.begin();
radio.openWritingPipe(address);
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_LOW);
// Set initial default values
data.thrustPOT = 127; // 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.rudderPOT = 127;
data.liftPOT = 1;
}
void loop() {
// Read all analog inputs and map them to one Byte value
data.thrustPOT = map(analogRead(A1), 0, 1023, 0, 255); // Convert the analog read value from 0 to 1023 into a BYTE value from 0 to 255
data.rudderPOT = map(analogRead(A0), 0, 1023, 0, 255);
data.liftPOT = map(analogRead(A2), 0, 1023, 0, 255);
radio.write(&data, sizeof(Data_Package));
int liftVal, thrustVal, rudderPOS;
liftVal = map(analogRead(A2), 0, 1023, 0, 255);
thrustVal = map(analogRead(A0), 0, 1023, 0, 255);
rudderPOS = map(analogRead(A1), 0, 1023, 0, 255);
Serial.print("Lift : ");
Serial.print(data.liftPOT);
Serial.print(" Thrust : ");
Serial.print(data.thrustPOT);
Serial.print(" Rudder : ");
Serial.println(data.rudderPOT);
//Receiver//
#include <RF24Network.h>
#include <RF24Network_config.h>
#include <Sync.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
RF24 radio(7, 8); // nRF24L01 (CE, CSN)
const byte address[6] = "00001";
unsigned long lastReceiveTime = 0;
unsigned long currentTime = 0;
Servo thrustESC; // create servo object to control the ESC
Servo liftESC;
Servo rudder;
int thrustVal, liftVal, rudderVal;
// Max size of this struct is 32 bytes - NRF24L01 buffer limit
struct Data_Package {
byte thrustPOT;
byte liftPOT;
byte rudderPOT;
};
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();
thrustESC.attach(6);
liftESC.attach(5);
rudder.attach(3);
pinMode(10, OUTPUT);
}
void loop() {
// Check whether we keep receving data, or we have a connection between the two modules
currentTime = millis();
if ( currentTime - lastReceiveTime > 1000 ) { // 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
Serial.print("recieving");
}
// Controlling servos
rudderVal = map(data.rudderPOT, 0, 255, 0, 50);
rudder.write(rudderVal);
// Controlling brushless motor with ESC
// Lift propeller
liftVal = map(data.liftPOT, 0, 255, 1000, 2000); // Map the receiving value form 0 to 255 to 0 1000 to 2000, values used for controlling ESCs
liftESC.writeMicroseconds(liftVal); // Send the PWM control singal to the ESC
// Thrust propeller
thrustVal = constrain(data.thrustPOT, 130, 255); // Joysticks stays in middle. So we only need values the upper values from 130 to 255
thrustVal = map(thrustVal, 130, 255, 1000, 2000);
thrustESC.writeMicroseconds(thrustVal);
Serial.print("Lift : ");
Serial.print(data.thrustPOT);
Serial.print(" Thrust : ");
Serial.print(thrustVal);
Serial.print(" Rudder : ");
Serial.println(rudderVal);
}
void resetData() {
// Reset the values when there is no radio connection - Set initial default values
data.thrustPOT = 127;
data.liftPOT = 1;
data.rudderPOT = 127;
Serial.println("reset");
}