Help! Making a servo and Stepper work simultaneously

Hello,

I'm working on my first project and it is a pan and tilt system but I have ran into a road block. I am using a servo for my tilt due to needing to control the angles with out having a physical limiting switch and a stepper for the pan. They are controlled by a ps2 style joystick and when I move the stepper I can not simultaneously move the servo and vise versa. I've attached my code and any help with solving this issue and making them run simultaneously would be greatly appreciated.

I have them power by a 9v battery and I am using an Elegoo UNO R3 and the components, including the motors, are from the Elegoo starter kit.

Stepper_Test.ino (1.7 KB)

#include<Stepper.h>
// define stepper motor control pins
#define IN1 2
#define IN2 3
#define IN3 5
#define IN4 6

int STEPS=2048;

// initialize stepper library
Stepper stepper(STEPS, 2, 5, 3, 6);

//Servo
#include <Servo.h>
Servo Xservo;

int Xpin=A0;
int Ypin=A1;
int XSpin=13;

int WVx;
int WVy;
int Xval;
int Yval;
int dt=600;

//servo end

void setup()
{
Serial.begin(9600);

//Servo

// put your setup code here, to run once:
pinMode(Xpin,INPUT);
pinMode(Ypin,INPUT);
pinMode(XSpin,OUTPUT);

Xservo.attach(XSpin);

//Servo End
}

void loop()
{
// read analog value from the potentiometer
int val = analogRead(Ypin);

// if the joystic is in the middle ===> stop the motor
if( (val > 495) && (val < 520) )
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}

else
{
// move the motor in the first direction
while (val >= 520)
{
// map the speed between 1 and 17 rpm
int speed_ = map(val, 520, 1023, 1, 17);
// set motor speed
stepper.setSpeed(speed_);

// move the motor (1 step)
stepper.step(1);

val = analogRead(Ypin);
}

// move the motor in the other direction
while (val <= 495)
{
// map the speed between 1 and 17 rpm
int speed_ = map(val, 495, 0, 1, 17);
// set motor speed
stepper.setSpeed(speed_);

// move the motor (1 step)
stepper.step(-1);

val = analogRead(Ypin);

}

}

//Servo

Xval=analogRead(Xpin);
Xval=map(Xval,0,1023,0,180);

Xservo.write(Xval);

Serial.print("Y Axis ");
Serial.println(val);
Serial.print("x Axis ");
Serial.println(Xval);
Serial.print("X axis volts");
Serial.println(Xval*(1023/180));
delay(600);
}

Don't use WHILE or FOR is you want an responsive program because they block the Arduino until they complete. Use IF and allow loop() to do the repetition.

Have a look at how the code is organized in Several Things at a Time

Note how each function runs very briefly and returns to loop() so the next one can be called. None of the functions tries to complete a task in one call. And there may be dozens of calls to a function before it is actually time for it to do anything.

If you are moving the stepper motor 1 step at a time the speed setting is irrelevant and you need to do the step timing yourself. With the standard Stepper library moving 1 step at a time is necessary for a responsive program. Have a look at the AccelStepper library which has non-blocking run() and runSpeed() functions.

...R