How to run stepper and DC motor

Hi everybody. I want to start 2 motors at the same time by a pushbutton, one of them is stepper motor with A4988 driver and the other is a DC motor with TB6612fng driver. I want to move the stepper motor 1000 steps every 5 seconds at CW and 1000 steps CCW in the next 5 seconds. at this time the DC motor going on normally. the system will stop after the DC motor turns 15000 turns, a hall module will count the number of turns. BUT IT DOES NOT WORK >:( >:( >:(

help me, please.

this is the code

#define DC_Speed_pin 5
#define DC_IN1_pin 6
#define DC_IN2_pin 7

#define DIR_PIN 10
#define STEP_PIN 11
#define EN_PIN 12
#define pushbutton 9

#define hallsensor 3

int turn=0;
int Allowable_number_of_rotation=1500;
int lasttime=0;
int event=HIGH;
int RPM=200;
String str;
int flag=0;

void setup() {
 pinMode(DIR_PIN, OUTPUT);
 pinMode(STEP_PIN, OUTPUT);
 pinMode(EN_PIN, OUTPUT);
 digitalWrite(DIR_PIN, LOW);
 digitalWrite(STEP_PIN, LOW);
 digitalWrite(EN_PIN, LOW);
 
 pinMode(pushbutton, INPUT_PULLUP);
 pinMode(hallsensor, INPUT_PULLUP);
 attachInterrupt(digitalPinToInterrupt(hallsensor), revolotion, RISING);

 pinMode(DC_Speed_pin, OUTPUT);
 pinMode(DC_IN1_pin, OUTPUT);
 pinMode(DC_IN2_pin, OUTPUT);
 analogWrite(DC_Speed_pin, RPM);
 digitalWrite(DC_IN1_pin, LOW);
 digitalWrite(DC_IN2_pin, LOW);

 Serial.begin(9600);
 while (! Serial);
 Serial.println("open serial monitor");
}

void loop() {
 while (event == HIGH){
   if (! digitalRead(pushbutton)){
     event=LOW;
     Serial.println("Push button happened");
     digitalWrite(DC_IN1_pin, LOW);
     digitalWrite(DC_IN2_pin, HIGH);
     digitalWrite(EN_PIN, LOW);
     Serial.println("DC motor start rotation");
     
     if (Serial.available() > 0) {
       str = Serial.readString();
       delay(10);
       int new_Allowable_number_of_rotation=str.toInt();
       Serial.print("I received: ");
       Serial.println(new_Allowable_number_of_rotation,DEC);
       Allowable_number_of_rotation= new_Allowable_number_of_rotation;
       }
       else{
         Allowable_number_of_rotation=1500;
         Serial.println(Allowable_number_of_rotation,DEC);
         }
       turn=0;
     break;
     }
     Serial.println("press Push button to continue");
     delay(1000);
     Serial.println("input Allowable_number_of_rotation");
     delay(1000);
     }
   lasttime=millis();
   Serial.println("Step motor rotate CW");
   digitalWrite(STEP_PIN, LOW);
   while ((millis()-lasttime <= 5000)){
     digitalWrite(STEP_PIN, HIGH);
     delayMicroseconds(1000);
     digitalWrite(STEP_PIN, LOW);
     delayMicroseconds(1000);
     }
   
   Serial.println("Step motor stop");
   delay(5000);
   
   lasttime=millis();
   Serial.println("Step motor rotate CCW");
   digitalWrite(STEP_PIN, HIGH);
   while ((millis()-lasttime <= 5000)){
     digitalWrite(STEP_PIN, HIGH);
     delayMicroseconds(1000);
     digitalWrite(STEP_PIN, LOW);
     delayMicroseconds(1000);
     }
   }

void revolotion(){
 turn++;
 if (turn >= Allowable_number_of_rotation){
   digitalWrite(DC_IN1_pin, LOW);
   digitalWrite(DC_IN2_pin, LOW);
   digitalWrite(EN_PIN, HIGH);
   turn=0;
   event = HIGH;
  }
}

sharifan:
BUT IT DOES NOT WORK

What does your program actually do?

"does not work" gives us no useful information from which to help you.

...R

PS ... full marks for using code tags and for displaying your image in your Post
[Those were moderator added]

Which speed did you really want to use?

//ACCELEROMETER
Serial.begin(9600);

//CURRENT SENSOR
Serial.begin(19200);

//VOLTAGE SENSOR
Serial.begin(57600);

//PROXIMITY SENSOR
Serial.begin(74880);

//FLOWRATE METER
Serial.begin(115200);

Serial.begin(230400);// seial monitor initialized <<< is that a choice in the Serial Monitor?
{Didn’t used to be a chioce}

//PROXIMITY SENSOR
Serial.begin(74880);
pinMode(MotorPinA, OUTPUT);
pinMode(MotorSpeedPinA, OUTPUT); <<< which are these actually being used for?
pinMode(MotorBrakePinA, OUTPUT);<<< which are these actually being used for?

//ELECTRIC MOTOR
pinMode(MotorPinA, OUTPUT);
pinMode(MotorSpeedPinA, OUTPUT);
pinMode(MotorBrakePinA, OUTPUT);

Make some decisions on speed, clean up the duplicates.

it's frozen. nothing to do just the step motor work a little and stop/
DC motor no reflect even.
thank you

If you want a responsive program you should get rid of the delay()s and also not use WHILE or FOR as they all block the Arduino from doing other things until they complete.

Replace WHILE and FOR with IF and allow loop() to do the iteration. Only use WHILE and FOR when you know that they always complete in a few millisecs.

The functions delay() and delayMicroseconds() block the Arduino until they complete. Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

The second example in this Simple Stepper Code shows the use of millis() and micros() for non-blocking motor movements. You might also like to consider the run() and runSpeed() functions in the AccelStepper library

...R

PS ... why is your Title called "simulation"? It seems to me that something like "How to run stepper and DC motor" would better reflect your question. If you edit your Original Post you can change the Title

thank you :slight_smile: