Servo to joystick Correction

Im trying to figure out why my servo wirelessly moves in one direction the first time it communicates the joystick. After it finish its movement it will allow me to control the servo with the joystick. So long story. I have two different Arduino mini pro boards and two nRF24Lo1 one set hooked up to a servo the other hooked up to a joystick. When I turn on the servo board it will orientate like it is suppose to. When I turn on the joystick side and it makes first communication with the servo board it will turn the servo almost 180 degrees in the same direction that it makes communication the first time every time then it will allow me to move the servo. I would like to stop this first time movement if possible and keep it in the orientation spot if someone could help me out with my code i would really appreciate it.

Servo side

#include <SPI.h> 
#include "RF24.h"  

uint16_t data[2];

RF24 radio(9,10);

const uint64_t pipe = 0xE8E8F0F0E3LL;

#include <Servo.h>
int servo_angle =0;
int servo_pin = 5;
int servo_angle2 =0;
int servo_pin2 = 6;

Servo Tilt;
Servo Pan;

void setup() {
Serial.begin(9600);
radio.begin();             
radio.setPALevel(RF24_PA_MIN);  
radio.setDataRate(RF24_2MBPS);
radio.setChannel(124);      
radio.openReadingPipe(1, pipe);   
radio.startListening(); 
Tilt.attach(servo_pin);
Pan.attach(servo_pin2);

}

void loop() {

  unsigned long currentTime = millis();

   if (radio.available()){
      radio.read(data, sizeof(data));
        
if (data[0]<460){ 
  servo_angle--;
  Tilt.write(servo_angle);}
  if (servo_angle<=10){
      servo_angle =10;}
if (data[0]>564){
  servo_angle++;
  Tilt.write(servo_angle);}
  if (servo_angle>= 160){
      servo_angle = 160;}

if (data[1]<460){
  servo_angle2--;
  Pan.write(servo_angle2);}
  if (servo_angle2<=10){
      servo_angle2 =10;}
if (data[1]>564){
  servo_angle2++;
  Pan.write(servo_angle2);}
  if (servo_angle2>=160){
      servo_angle2= 160;}  
  }
}

Joystick Side

#include <SPI.h>   
#include "RF24.h"   

#define TiltCam A0
#define PanCam A1 

uint16_t data[2];

RF24 radio(9,10);
                      
const uint64_t pipe = 0xE8E8F0F0E3LL;

void setup(void){
  Serial.begin(9600);
  radio.begin(); 
  radio.setPALevel(RF24_PA_MIN);  
  radio.setDataRate(RF24_2MBPS);
  radio.setChannel(124);
  radio.stopListening();  
  radio.openWritingPipe(pipe); 
 
}

void loop(void){ 
  
  data[0] = analogRead(TiltCam);
  data[1] = analogRead(PanCam); 
  radio.write(data, sizeof(data));
}

again this only happens 1 time when the servo board power is turned on for the first time if the servo board stays on and the joystick board just cycles power doesn't do this but only when the power is cycled through the servo board side one time it will turn when the two boards communicate after that it will stay moving in the position it suppose to move when you ask it to on the joystick. I would like to eliminate this first time movement if possible.

Both servos or just one of them? Why not add a few Serial.prints to see what is being received and what written to the servo(s).

Note: when you do the servo.attach the servo is sent to position equivalent to write(90). If you want them to start anywhere else then do a servo.write(angle) immediately BEFORE the attach().

Steve

My guess is that one of these lines is waiting until a connection is established:

radio.begin();             
radio.setPALevel(RF24_PA_MIN);  
radio.setDataRate(RF24_2MBPS);
radio.setChannel(124);      
radio.openReadingPipe(1, pipe);   
radio.startListening(); 

After that delay, the servo pins are attached:

Tilt.attach(servo_pin);
Pan.attach(servo_pin2);

When you attach the pins, the servo moves to the default position: 90°

Try moving the .attach() lines to BEFORE the radio.begin() line.

right now I'm only using one servo and its being used by the x value which is A0 for the joystick and pin 5 for the servo I'm not worried about the other values for A1 and pin 6 trying to focus on one problem at a time so. I understand what your talking about seeing what is to be shown on the micro controller. From what I can tell my "if data[0]<460 and data[0]>564" have to do with when it starts off but I'm not sure what I can do to minimize or eliminate the travel when it first communicates i tried lowering the value and the best performance i get right now is what i have. If i try to change the values in a tighter range it will move in both directions and won't be able to move how i need it to. I believe the if(radio.available()){radio.read(data, sizeof(data)); is making the servo move in the direction of the if < > to verify before able to use. but not sure why its doing that.

Thanks I will give it a try and update you on the progress.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.