How to program stepper motor and DC motor run at the same time?

i have some problem to ask. How to make the program of stepper motor and DC motor run at the same time while controlling the speed of DC motor using a potentiometer. I have tried to write the program, but it seem cannot run at the same time. Can I know where is wrong or what should added in my coding?Thanks for reply and appreciate always...This is my coding.

#include <LiquidCrystal.h>
#include <Stepper.h>
#define motorSteps 500 // change this depending on the number of steps
// per revolution of your motor
#define motorPin1 2 //stepper motor pwm1
#define motorPin2 3 // stepper motor pwm2
#define motorPin3 4 // stepper motor pwm3
#define motorPin4 5 // stepper motor pwm4

LiquidCrystal lcd(8, 9, 10, 11, 12, 13);

int ledPin3= 36;
int forwardledPin=40;
int reverseledPin=39;
int stopledPin = 38;
int pot = A2;
int motorPin = 9;
int speed = 0;

boolean shade_open = false;
boolean shade_close = true;

Stepper myStepper(motorSteps, motorPin1,motorPin2,motorPin3,motorPin4);

void setup ()
{
myStepper.setSpeed(60);
Serial.begin(9600);
pinMode(ledPin3,OUTPUT);
pinMode(forwardledPin,OUTPUT);
pinMode(reverseledPin,OUTPUT);
pinMode(stopledPin,OUTPUT);
pinMode(motorPin, OUTPUT);
pinMode(pot, INPUT);
digitalWrite(motorPin, HIGH);
}

void loop ()
{
digitalWrite(ledPin3,HIGH);
speed= analogRead(pot);
speed = map(speed, 0, 1023, 0, 255);
analogWrite (motorPin, speed);
Serial.print(speed);
delay(500);

if((motorPin==HIGH)&&(speed >50)){ //most strong wind 4.5ms-1 on dec
digitalWrite(forwardledPin,LOW);
digitalWrite(reverseledPin,LOW);
digitalWrite(stopledPin,LOW);
lcd.clear();
lcd.print(" STRONG WIND... ");
delay(5000);
lcd.clear();
lcd.print(" CLOSE SHADE ");
delay(3000);
digitalWrite(reverseledPin,HIGH);

if(shade_open){ //if shade close, then open it
myStepper.step(-2000);
shade_open = false;
shade_close = true;

}
digitalWrite(reverseledPin,LOW);
digitalWrite(stopledPin,HIGH);
}

if((motorPin==HIGH)&&(speed <50)){ // open shade if no strong wind
digitalWrite(stopledPin,LOW);
digitalWrite(forwardledPin,LOW);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" NORMAL WIND ");
lcd.setCursor(0,1);
lcd.print(" OPEN SHADE ");
delay(5000);
digitalWrite(forwardledPin,HIGH);

if(shade_close){ //if shade open, then close it
myStepper.step(2000);
shade_open = true;
shade_close = false;
}
digitalWrite(forwardledPin,LOW);
digitalWrite(stopledPin,HIGH);
}
}

3 things,

  1. only post code inside code tags (the # above)

  2. Always lay your code out using the auto format tool in the IDE. Never do it by hand. This helps you! (and us).

  3. Never use delay. You can't do what you want if you are using delay!. Look at the blink without delay example and look up Finite State Machines in the playground.

Mark

  1. map() is a very expensive way to divide by 4

  2. Use the AccelStepper library instead of the blocking Stepper library.

Thanks for comment .I already solve the problem by separate the DC motor to another source ,just give it direct supply to run .And the rotary encoder only just can read the velocity from shaft DC motor.But the LCD appear junk characters if the DC running. What should need to do or adjust?

But the LCD appear junk characters if the DC running. What should need to do or adjust?

Move them far apart. Isolate the power supplies. Your electric motor is causing interference with the LCD.

YC1234:
Thanks for comment.I already solve the problem by separate the DC motor to another source ,just give it direct supply to run .And the rotary encoder only just can read the velocity from shaft DC motor.But the LCD appear junk characters if the DC running. What should need to do or adjust?