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...
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; }
}
}
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.