help me... stop stepper when running

tank you...
i try make same project (automatic sliding door) using tb6600 n nema stepper 23n sensor PIR...

door will be open if there is same one n will be closed if there is not same one...

i have a problem with a code when door running to be close n there is same one (PIR, HIGH again) door can't stop, door stil run until door closed perfecly... after it door run to open...

please help me...

error is in line 42.

As you have not posted your program the guess in Reply #1 is as good as any.

If you use the AccelStepper library it has a stop() function.

Otherwise you need to check the PIR between every step

...R

@fai88

Could you also take a few moments to Learn How To Use The Forum.
It will help you get the best out of the forum in the future.
Other general help and troubleshooting advice can be found here.

@ballscrewbob
i'm sorry.... this is my first post

tank's for link Learning...

@Robin2

I have tried to check the PIR between every step
but it dosn't work.... i tink my code is wrong

// Define connection pins: 
#define dirPin 2 // pin yang terhubung ke DIR+ motor driver
#define stepPin 3
#define pirPin 4
#include <AccelStepper.h>

AccelStepper stepper = AccelStepper(1, stepPin, dirPin);

//#define ledPin 13
// Create variables:
int val = 0;
bool motionState = false; 
// We start with no motion detected. 


void setup() {
// Configure the pins as input or output: 
//pinMode(ledPin, OUTPUT);
pinMode(pirPin, INPUT);
stepper.setMaxSpeed(1000);
stepper.setAcceleration(500);
// Begin serial communication at a baud rate of 9600:
Serial.begin(9600);
}

void loop()
{
  // Read out the pirPin and store as val:
  val = digitalRead(pirPin); 
  // If motion is detected (pirPin = HIGH), do the following:
  if (val == HIGH)
  {
   
    //digitalWrite(ledPin, HIGH); 
    // Turn on the on-board LED. // Change the motion state to true (motion detected):
    if (motionState == false) 
    { Serial.println("Motion detected!");
    stepper.moveTo(8000);
    stepper.runToPosition();
    motionState = true;
    }
    }
    // If no motion is detected (pirPin = LOW), do the following: 
    else {
      //digitalWrite(ledPin, LOW); 
    // Turn off the on-board LED. 
    // Change the motion state to false (no motion):
    if (motionState == true) 
    {
      Serial.println("Motion ended!");
     stepper.moveTo(7000);
    stepper.runToPosition();
    delay(500);
if (motionState == false) 
    { Serial.println("Motion detected!");
    stepper.moveTo(8000);
    stepper.runToPosition();
    motionState = true;
    }
     stepper.moveTo(6000);
    stepper.runToPosition();

     stepper.moveTo(5000);
    stepper.runToPosition();
      
      motionState = false; } 
      }
      }

in serial monitor
input pending until motor stepper running end off code

You are using stepper.runToPosition(); which blocks until it completes - as the documentation explains.

You need to use the non-blocking run() or runSpeed() functions. They need to be called very frequently - perhaps twice or three times as often as the actual step rate.

When you post the next version of your program please give as much detail as possible about what happens when you run it. The words "it doesn't work" don't provide any useful information from which to help you.

...R

@Robin2

Thank you very much
my problem is solved... stepper run without bloking....
sensor can read when motor stepper running

#define dirPin 2 
#define stepPin 3
#define pirPin 4
#include <AccelStepper.h>

AccelStepper stepper = AccelStepper(1, stepPin, dirPin);


int val = 0;
bool motionState = false; 



void setup() {
 
  pinMode(pirPin, INPUT);
  stepper.setMaxSpeed(1000);
stepper.setAcceleration(200);
stepper.setSpeed(200);


  Serial.begin(9600);
  }
  
  void loop()
  {

val = digitalRead(pirPin);
if (val == HIGH)
{
 if (motionState == false)
{ 
stepper.moveTo(6000);
stepper.runToPosition();
Serial.println(stepper.currentPosition());
motionState = true;
}
  delay(1000);  
  }      
     
       else {
          if (motionState == true)
         { 
          stepper.moveTo(0);
        Serial.println(-stepper.currentPosition());
         motionState = false;
        }
       }
stepper.run();

  }

Stick to just using run(), don't call any blocking functions...
Then you just have to call stop() whenever you like.

If I had written AccelStepper I'd have lost all the blocking functions, they often confuse new users, you'd
almost never want to call one.