Hi Robin2,
i have been struggling with it, even after reading copious threads and looking at the AccelStepper examples.
I have tried to incorporate the Bounce code, but all im getting is that the stepper turns once on start up and thats it.
Its important that the pir sensor has time to calibrate on start up and that the pir should not try to activate the stepper when the stepper is already moving. If there are any suggestions on how to achieve this is a cleaner way with the coding i'd be happy to give it a go?
#include <AccelStepper.h>
AccelStepper stepper (1, 3, 2); // name of stepper motor (1 = driver, pin 3 = step, pin 2 = direction)
int pirPin = 4;
int calibrationTime = 30;
long unsigned int lowIn;
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int PIRValue = 0;
long previousMillis = 0;
long interval = 1000;
void setup()
{
stepper.setMaxSpeed(1000);
stepper.setAcceleration(200);
stepper.moveTo(100);
Serial.begin(9600);
pinMode(pirPin, INPUT);
}
void loop()
{
PIRSensor();
stepper.run();
}
void PIRSensor()
{
if (digitalRead(pirPin) == HIGH)
{
if (lockLow)
{
if (stepper.distanceToGo() == 0) {
stepper.moveTo( -stepper.currentPosition() );
}
PIRValue = 1;
lockLow = false;
Serial.println("Motion detected.");
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > interval)
previousMillis = currentMillis;
}
takeLowTime = true;
}
else (digitalRead(pirPin) == LOW);
{
if (takeLowTime) {
lowIn = millis();
takeLowTime = false;
}
if (!lockLow && millis() - lowIn > pause)
{
PIRValue = 0;
lockLow = true;
Serial.println("Motion ended.");
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > interval)
previousMillis = currentMillis;
}
}
}