hi.
i have an arduino pro mini and nrf24l01 as a receaver for my airplane, and if its powered via usb it works just fine, but when i try to power it via baterry and move some servos it stops reacieving data. sometimes it catches back up and works until i again move some servo, sometimes it doesnt catch back up at all and i have to restar it.
I have tried :
- powering it via 9v baterry
- powering it via 3A SBEC on ESC
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
Servo AileronL;
Servo AileronP;
Servo Elevator;
Servo Rudder;
Servo motor;
int AileronLValue, AileronPValue, ElevatorValue, RudderValue, motorValue;
RF24 radio(4, 7); // nRF24L01 (CE, CSN)
const byte address[6] = "00888";
unsigned long lastReceiveTime = 0;
unsigned long currentTime = 0;
// Max size of this struct is 32 bytes - NRF24L01 buffer limit
struct Data_Package {
byte j1PotX;
byte j1PotY;
byte j1Button;
byte j2PotX;
byte j2PotY;
byte j2Button;
byte pot1;
byte pot2;
byte tSwitch1;
byte tSwitch2;
byte button1;
byte button2;
byte button3;
byte button4;
};
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();
AileronL.attach(3);
AileronP.attach(5);
Elevator.attach(6);
Rudder.attach(8);
motor.attach(9);
}
void loop() {
// 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
}
// 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 has a throttle up and we lose connection, it can keep flying unless we reset the values
}
AileronLValue = map(data.j2PotX, 0, 255, 20, 160);
AileronPValue = map(data.j2PotY, 0, 255, 20, 160);
ElevatorValue = map(data.j1PotX, 0, 255, 20, 160);
RudderValue = map(data.j1PotY, 0, 255, 20, 160);
motorValue = map(data.pot1, 0, 255, 1000, 2000);
AileronL.write(AileronLValue);
AileronP.write(AileronPValue);
Elevator.write(ElevatorValue);
Rudder.write(RudderValue);
motor.write(motorValue);
Serial.print("; pot1:");
Serial.print(data.pot1);
Serial.print("; pot2:");
Serial.println(data.pot2);
}
void resetData() {
// Reset the values when there is no radio connection - Set initial default values
data.j1PotX = 127;
data.j1PotY = 127;
data.j2PotX = 127;
data.j2PotY = 127;
data.j1Button = 1;
data.j2Button = 1;
data.pot1 = 1;
data.pot2 = 1;
data.tSwitch1 = 1;
data.tSwitch2 = 1;
data.button1 = 1;
data.button2 = 1;
data.button3 = 1;
data.button4 = 1;
}