Motor cant run when combine with other components

I need help with my programming.
I cant seemed to get my motor running when I combine my stepper motor with IR sensor and Servo motor.
The algorithm should be as such, stepper motor rotate until IR sensor senses an object, will stop the stepper motor from rotating and activate the servo motor.

The stepper motor works individually.
Please help me.

The following is my code:

#include <Servo.h>
#define ir 0

Servo myservo;  // create servo object to control a servo
int pos = 0;    // variable to store the servo position

int LED = 13;

void setup() {
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(LED,OUTPUT);
digitalWrite(8,LOW);
digitalWrite(9,LOW);

myservo.attach(10);  // attaches the servo on pin 10 to the servo object
analogReference(DEFAULT);
Serial.begin(9600);
pinMode (ir,INPUT);
}

void loop() {
int raw = analogRead(ir);
int volt = map(raw, 0, 1023, 0, 5000);
int cm = (21.61/(volt-0.1696))*1000;
//Serial.println(cm);

delay(100);

if( cm < 10)
{
  digitalWrite(9,LOW);
  digitalWrite(LED,HIGH);
  for (pos = 0; pos <= 180; pos += 5) { // goes from 0 degrees to 180 degrees
  myservo.write(pos);
  delay(25); 
  }
  for (pos = 180; pos >= 0; pos -= 5) { // goes from 180 degrees to 0 degrees
  myservo.write(pos);              // tell servo to go to position in variable 'pos'
  delay(15);
}
}


else
{
digitalWrite(9,HIGH);
delayMicroseconds(1);
digitalWrite(9,LOW);
delay(1);
digitalWrite(LED,LOW);
}
}

Did you forget to include the Servo library?

#include <Servo.h>
digitalWrite(9,HIGH);
delayMicroseconds(1); // try delayMicroseconds(1000);
digitalWrite(9,LOW);

Assuming pins 8 & 9 are feeding a stepper driver, you’re sending it a 500 kHz pulse string, I doubt it will do anything besides warm up.

outsider:
Did you forget to include the Servo library?

#include <Servo.h>
digitalWrite(9,HIGH);

delayMicroseconds(1); // try delayMicroseconds(1000);
digitalWrite(9,LOW);



Assuming pins 8 & 9 are feeding a stepper driver, you're sending it a 500 kHz pulse string, I doubt it will do anything besides warm up.

I have included the servo library. Is there a solution to it?

What are pins 8 & 9 connected to? Post your latest code, also which stepper motor (post a link)?

Pin 8 is connected to 'dir' pin and pin 9 is connected to 'step' pin of motor driver (EasyDriver)

The motor I am using is a stepper motor 42BYG40-160-4A

It all seems to work perfectly fine when run individually but not when its integrated together.

#include <Servo.h>
#define ir 0

Servo myservo;  // create servo object to control a servo
int pos = 0;    // variable to store the servo position

int LED = 13;

void setup() {
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(LED,OUTPUT);
digitalWrite(8,LOW);
digitalWrite(9,LOW);

myservo.attach(10);  // attaches the servo on pin 10 to the servo object
analogReference(DEFAULT);
Serial.begin(9600);
pinMode (ir,INPUT);
}

void loop() {
int raw = analogRead(ir);
int volt = map(raw, 0, 1023, 0, 5000);
int cm = (21.61/(volt-0.1696))*1000;
//Serial.println(cm);

delay(100);

if( cm < 10)
{
  digitalWrite(9,LOW);
  digitalWrite(LED,HIGH);
  for (pos = 0; pos <= 180; pos += 5) { // goes from 0 degrees to 180 degrees
  myservo.write(pos);
  delay(25); 
  }
  for (pos = 180; pos >= 0; pos -= 5) { // goes from 180 degrees to 0 degrees
  myservo.write(pos);              // tell servo to go to position in variable 'pos'
  delay(15);
}
}


else
{
digitalWrite(9,HIGH);
delayMicroseconds(1);
digitalWrite(9,LOW);
delay(1);
digitalWrite(LED,LOW);
}
}