DM860H motor driver with Nema34(Stepper motor) coding problem

I’m making a ventilator that responds to internal and external conditions and operates on its own.
However, the reaction from all other conditions is correct, but only the stepper motor, which is the axis of rotation of the ventilator, does not operate under conditions.
For example, the ventilator itself needs to rotate +180 degrees or -180 degrees depending on internal and external conditions, but I don’t know the code that controls its behavior.
Please check the code below to see what the problem is.
Please also refer to the notes.

#include <DHT.h>

#define DHTTYPE DHT22

#define DHTPIN1 52

#define DHTPIN2 53

DHT dht1(DHTPIN1,DHTTYPE);

DHT dht2(DHTPIN2,DHTTYPE);

#include <LiquidCrystal_I2C.h>

#include <Wire.h>

LiquidCrystal_I2C lcd(0x27,20,4);

//

#include <SoftwareSerial.h>

SoftwareSerial mySerial(13,11);

//

#include <AccelStepper.h>

int direcPin = 7;
int pulsePin = 6;

AccelStepper stepper(1, pulsePin, direcPin);

//

int MQ5LPG=48,MQ7CO=49;

int fan=4,dir=3,buzzer=44;

//

float Tin=0,Tout=0;

int target=0,angle=0;

unsigned char Send_data[5] = {0x11,0x02,0x0b,0x01, 0xE1};
unsigned char Receive_Buff[20];
unsigned long PM25;
unsigned long COM_SUCCESS, COM_COUNT;
unsigned char recv_cnt = 0;

//

void Send_CMD(void)
{
 unsigned char i;
 for(i=0; i<5; i++)
 {
   mySerial.write(Send_data[i]);
   delay(1);
 }
}

//

unsigned char Checksum_cal(void)
{
 unsigned char count, SUM=0;
 for(count=0; count<19; count++)
 {
    SUM += Receive_Buff[count];
 }
 return 256-SUM;
}

//

void setup(){

pinMode(fan,OUTPUT); pinMode(dir,OUTPUT); pinMode(buzzer,OUTPUT); pinMode(11,OUTPUT);

pinMode(13,INPUT);

lcd.init(); lcd.backlight();

Serial.begin(9600); while(!Serial);

mySerial.begin(9600); while(!mySerial);

digitalWrite(dir, LOW);


}

//

void loop(){
 COM_COUNT++;  
 Send_CMD();
 while(1)
 {
   if(mySerial.available())
   { 
      Receive_Buff[recv_cnt++] = mySerial.read();
     if(recv_cnt ==20){recv_cnt = 0; break;}
   }
 } 
 if(Checksum_cal() == Receive_Buff[19])
 {
       COM_SUCCESS++;
       PM25 = (unsigned long)Receive_Buff[3]<<24 | (unsigned long)Receive_Buff[4]<<16 | (unsigned long)Receive_Buff[5]<<8| (unsigned long)Receive_Buff[6];
       
       Serial.write("    PM2.5 : ");
       
       Serial.println(PM25);
 }
  else
  {
    Serial.write("CHECKSUM Error");
  }



//

Tin=dht1.readTemperature(); Serial.write("DHT_1: "); Serial.println(Tin);

Tout=dht2.readTemperature(); Serial.write("DHT_2: "); Serial.println(Tout);

Serial.write("MQ5LPG: "); Serial.println(digitalRead(MQ5LPG));

Serial.write("MQ7CO: "); Serial.println(digitalRead(MQ7CO));

//

int dif=Tin-Tout; // Calculate the difference between indoor and outdoor temperature.

//

 if( !digitalRead(MQ5LPG) || !digitalRead(MQ7CO) || (PM25>=35.1) ){    // If LPG gas or CO is detected or fine dust concentration is high

    if( angle==0){  angle=180; stepper.moveTo (200);  stepper.run ();   }  // Operate the step motor to activate the ventilator in the external direction

   analogWrite(fan,150); tone(buzzer,500,100); // Turn high speed and release the inner air outside.

}

else if( dif>0 ){ // If the room temperature is higher

if( Tin>=25 ){  // If the room temperature is over 25 degrees

    if( angle==0){  angle=180; stepper.moveTo (200);  stepper.run ();   }  // Operate the step motor to activate the ventilator in the external direction
   
analogWrite(fan,150); noTone(buzzer); // Turn high speed and release the inner air outside.

}

}

 else if( dif<0 ){ // If the outdoor temperature is higher

 if( Tin<=18 ){  // If the room temperature is below 18 degrees

    if( angle==180){  angle=0; stepper.moveTo (-200);  stepper.run ();   } // Operate the step motor to activate the ventilator in the inner direction.

  analogWrite(fan,150); noTone(buzzer); // Turn high speed and release the outside air inside.

    }

  }

 else{                                                         // If these conditions are not met,

  analogWrite(fan,0); noTone(buzzer); // Stop

 }

 //

 lcd.setCursor(0,0); lcd.print("Tin: "); lcd.print(Tin); lcd.print("Tout: "); lcd.print(Tout);

 lcd.setCursor(0,1); lcd.print("D: ");  lcd.print(PM25);

 delay(3000);

}

What type of stepper ?
How is the stepper powered ?
How is the stepper wired ?
Do the examples with the AccelStepper library work ?

I’m making a ventilator that responds to internal and external conditions and operates on its own.
However, the reaction from all other conditions is correct, but only the stepper motor, which is the axis of rotation of the ventilator, does not operate under conditions.
For example, the ventilator itself needs to rotate +180 degrees or -180 degrees depending on internal and external conditions, but I don’t know the code that controls its behavior.

And i tried Demo code of AccelStepper, and that worked well
So i think wiring is right and power is enough
Definitely There’s a problem with the code.
Please check the code below to see what the problem is.
Especially
" if( !digitalRead(MQ5LPG) || !digitalRead(MQ7CO) || (PM25>=35.1) ){ // If LPG gas or CO is detected or fine dust concentration is high

if( angle==0){ angle=180; stepper.moveTo (200); stepper.run (); } // Operate the step motor to activate the ventilator in the external direction "
Check this part please…

#include <DHT.h>

#define DHTTYPE DHT22

#define DHTPIN1 52

#define DHTPIN2 53

DHT dht1(DHTPIN1,DHTTYPE);

DHT dht2(DHTPIN2,DHTTYPE);

#include <LiquidCrystal_I2C.h>

#include <Wire.h>

LiquidCrystal_I2C lcd(0x27,20,4);

//

#include <SoftwareSerial.h>

SoftwareSerial mySerial(13,11);

//

#include <AccelStepper.h>

int direcPin = 7;
int pulsePin = 6;

AccelStepper stepper(1, pulsePin, direcPin);

//

int MQ5LPG=48,MQ7CO=49;

int fan=4,dir=3,buzzer=44;

//

float Tin=0,Tout=0;

int target=0,angle=0;

unsigned char Send_data[5] = {0x11,0x02,0x0b,0x01, 0xE1};
unsigned char Receive_Buff[20];
unsigned long PM25;
unsigned long COM_SUCCESS, COM_COUNT;
unsigned char recv_cnt = 0;

//

void Send_CMD(void)
{
 unsigned char i;
 for(i=0; i<5; i++)
 {
   mySerial.write(Send_data[i]);
   delay(1);
 }
}

//

unsigned char Checksum_cal(void)
{
 unsigned char count, SUM=0;
 for(count=0; count<19; count++)
 {
    SUM += Receive_Buff[count];
 }
 return 256-SUM;
}

//

void setup(){

pinMode(fan,OUTPUT); pinMode(dir,OUTPUT); pinMode(buzzer,OUTPUT); pinMode(11,OUTPUT);

pinMode(13,INPUT);

lcd.init(); lcd.backlight();

Serial.begin(9600); while(!Serial);

mySerial.begin(9600); while(!mySerial);

digitalWrite(dir, LOW);


}

//

void loop(){
 COM_COUNT++;  
 Send_CMD();
 while(1)
 {
   if(mySerial.available())
   { 
      Receive_Buff[recv_cnt++] = mySerial.read();
     if(recv_cnt ==20){recv_cnt = 0; break;}
   }
 } 
 if(Checksum_cal() == Receive_Buff[19])
 {
       COM_SUCCESS++;
       PM25 = (unsigned long)Receive_Buff[3]<<24 | (unsigned long)Receive_Buff[4]<<16 | (unsigned long)Receive_Buff[5]<<8| (unsigned long)Receive_Buff[6];
       
       Serial.write("    PM2.5 : ");
       
       Serial.println(PM25);
 }
  else
  {
    Serial.write("CHECKSUM Error");
  }



//

Tin=dht1.readTemperature(); Serial.write("DHT_1: "); Serial.println(Tin);

Tout=dht2.readTemperature(); Serial.write("DHT_2: "); Serial.println(Tout);

Serial.write("MQ5LPG: "); Serial.println(digitalRead(MQ5LPG));

Serial.write("MQ7CO: "); Serial.println(digitalRead(MQ7CO));

//

int dif=Tin-Tout; // Calculate the difference between indoor and outdoor temperature.

//

 if( !digitalRead(MQ5LPG) || !digitalRead(MQ7CO) || (PM25>=35.1) ){    // If LPG gas or CO is detected or fine dust concentration is high

    if( angle==0){  angle=180; stepper.moveTo (200);  stepper.run ();   }  // Operate the step motor to activate the ventilator in the external direction

   analogWrite(fan,150); tone(buzzer,500,100); // Turn high speed and release the inner air outside.

}

else if( dif>0 ){ // If the room temperature is higher

if( Tin>=25 ){  // If the room temperature is over 25 degrees

    if( angle==0){  angle=180; stepper.moveTo (200);  stepper.run ();   }  // Operate the step motor to activate the ventilator in the external direction
   
analogWrite(fan,150); noTone(buzzer); // Turn high speed and release the inner air outside.

}

}

 else if( dif<0 ){ // If the outdoor temperature is higher

 if( Tin<=18 ){  // If the room temperature is below 18 degrees

    if( angle==180){  angle=0; stepper.moveTo (-200);  stepper.run ();   } // Operate the step motor to activate the ventilator in the inner direction.

  analogWrite(fan,150); noTone(buzzer); // Turn high speed and release the outside air inside.

    }

  }

 else{                                                         // If these conditions are not met,

  analogWrite(fan,0); noTone(buzzer); // Stop

 }

 //

 lcd.setCursor(0,0); lcd.print("Tin: "); lcd.print(Tin); lcd.print("Tout: "); lcd.print(Tout);

 lcd.setCursor(0,1); lcd.print("D: ");  lcd.print(PM25);

 delay(3000);

}

Simple Question
What is the problem with the code below?

 if( !digitalRead(MQ5LPG) || !digitalRead(MQ7CO) || (PM25>=35.1) ){    // If LPG gas or CO is detected or fine dust concentration is high

    if( angle==0){  angle=180; stepper.moveTo (200);  stepper.run ();   }  // Operate the step motor to activate the ventilator in the external direction

You tell us.

A closing bracket is missing :slight_smile:

Indent your code correctly

if( !digitalRead(MQ5LPG) || !digitalRead(MQ7CO) || (PM25>=35.1) ) // If LPG gas or CO is detected or fine dust concentration is high
{    
    if( angle == 0 ) // Operate the step motor to activate the ventilator in the external direction
    {
        angle = 180;
        stepper.moveTo(200);
        stepper.run();
    }

@gongdoll, do not cross-post. Threads merged.

It's quite odd that you mash multiple statements onto a single line as if vertical space was precious, yet you add a bunch of random pointless blank lines. Both these things make the code more difficult to read, which makes errors more likely and finding anything in the code slower.