Hey everyone,
I was hoping to get some help with the following code for the transmitter and receiver for my BB-9E project. I got the code to be kinda stable but there is something just a little off. I think its the delays but I tried a lot of different things and it still isn't right. The servos on the droid will move jerky or on their own with no input.
The setup is like this:
Transmitter:
Arduino nano to nRF24L01+ with a single joystick for control
Receiver:
nRF24L01+ to Arduino Uno
3 MG995 servos powered directly from separate 5v source (servo 4 isn't being used anymore)
So my main questions:
-
The receiver and transmitter aren't "in-sync" and dont communicate smoothly. The servos will move on their own or cut out and it takes a second or two to reconnect. Is there something obvious I'm missing? I added delays to the receiver because otherwise I could not get both channels working, is there a better way maybe?
-
The line:
"radio.setPALevel(RF24_PA_MIN);"
works when both the R and T are set to MIN, but does not when I change to MAX or HIGH. Why is that? I'd like to have on max since the receiver is inside the droid.
Transmitter Code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CSN, CE
const byte address[6] = "00001";
int x_key = A4;
int y_key = A3;
int x_pos;
int y_pos;
void setup() {
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
pinMode (x_key, INPUT) ;
pinMode (y_key, INPUT) ;
}
void loop() {
x_pos = analogRead (x_key) ;
y_pos = analogRead (y_key) ;
radio.write(&x_pos, sizeof(x_pos));
radio.write(&y_pos, sizeof(y_pos));
delay(200);
}
Receiver Code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
RF24 radio(7, 8); // CSN, CE
const byte address[6] = "00001";
int servo1_pin = 6;
int servo2_pin = 5;
int servo3_pin = 3;
int servo4_pin = 9;
void setup() {
Serial.begin(9600);
radio.begin();
servo1.attach (servo1_pin ) ;
servo1.write (84);
servo2.attach (servo2_pin ) ;
servo2.write (80);
servo3.attach (servo3_pin ) ;
servo4.attach (servo4_pin ) ;
servo4.write (103);
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
}
void loop() {
if (radio.available()) {
int x_pos ;
radio.read(&x_pos, sizeof(x_pos));
Serial.print(x_pos);
if (x_pos>400 && x_pos<700)
{ servo1.write (84); servo2.write (80);
}
else{
x_pos = map(x_pos, 0, 1023, 0, 180);
servo1.write (x_pos) ; servo2.write (180-x_pos);
}
delay(100);
int y_pos ;
radio.read(&y_pos, sizeof(y_pos));
Serial.print(y_pos);
if (y_pos>400 && y_pos<700)
{ servo3.write (90);
}
else{
y_pos = map(y_pos, 0, 1023, 70, 110);
servo3.write (y_pos) ;
}
delay(100);
}
}