Hi,
First a bit of context :
I am building a RC boat model using the following components :
- an Arduino Pro Mini 5V 16 MHz;
- a nRF24L01 for the transceiver;
- a Feetech FT90B servo to control the rudder;
- a DRV8838 to control the propeller;
- a dfplayer mini in order to add sound effects.
Before using the nRF24L01 I used a wireless PS2 gamepad (that also uses SPI to communicate with the Arduino) from Joy-It to control the boat and it worked flawlessly. I decided to replace the wireless gamepad because of its limited range.
Issue :
When I use the "attach" command from either the PWMServo library or the default Servo library, the nRF24L01 stop receiving reliably.
What I have already done :
The same sketch with only the "attach" command commented works.
At first I thought it might be a conflict with the attribution of pin D10 (my servo is, usually, attached to this pin in order to work with PWMServo) but it is not declared as the CSN for the NRF24 (I use A0 for this purpose) so it shouldn't be an issue.
Code :
#include <SPI.h>
#include <RF24.h>
#include <PWMServo.h>
//Pin definitions
#define ENABL_PIN 6
#define PHASE_PIN 5
#define SERVO_PIN 10
#define MAX_DEFLECT 60
#define NEUTRAL 90
#define DEADZONE 10
struct payload
{
int joy_x;
int joy_y;
byte buttons;
};
payload data_to_receive;
PWMServo rudder;
uint8_t address[][6] = { "1Node", "2Node" };
RF24 radio(A1,A0);
int motor_input = 0;
int rudder_input = 90;
void setup()
{
Serial.begin(9600);
Serial.println(F("Let the show begin !"));
if (!radio.begin())
{
Serial.println(F("radio hardware is not responding!!"));
while (1) {} // hold in infinite loop
}
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_LOW);
radio.openWritingPipe(address[1]);
radio.openReadingPipe(1, address[0]);
radio.startListening();
//From datasheet : 500 µs = 0°, 2500 µs = 180°
rudder.attach(SERVO_PIN, 500, 2500);
//Set Motor
pinMode(PHASE_PIN, OUTPUT);
pinMode(ENABL_PIN, OUTPUT);
//init data
data_to_receive.joy_x = 512;
data_to_receive.joy_y = 512;
data_to_receive.buttons = 0;
}
void loop()
{
uint8_t pipe;
if(radio.available(&pipe))
{
radio.read(&data_to_receive, sizeof(payload));
Serial.print(F("Message reads : "));
Serial.println(data_to_receive.joy_y);
}
if(data_to_receive.joy_y<(511 - DEADZONE))
{
digitalWrite(PHASE_PIN, LOW);
motor_input = (512 - data_to_receive.joy_y)/2;
}
else if(data_to_receive.joy_y>(511 + DEADZONE))
{
digitalWrite(PHASE_PIN, HIGH);
motor_input = (data_to_receive.joy_y - 511)/2;
}
else
{
motor_input = 0;
}
motor_input = constrain(motor_input, 0, 255);
analogWrite(ENABL_PIN, motor_input);
rudder_input = map(data_to_receive.joy_x, 0, 1023, NEUTRAL-MAX_DEFLECT, NEUTRAL+MAX_DEFLECT);
rudder.write(rudder_input);
}
Then I moved the servo to pin D7 using the Servo library instead of PWMServo without solving the issue (I got rid of SoftwareSerial and of the MP3 player to try this). The code below is the one from this last test :
EDIT : removed it because turns out Servo don't go well with RF24 after all even with the servo pin not attached.
I am a bit short on ideas on how to solve this issue and could really use an outside perspective. I thank you in advance for doing so.