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));
}