With this:
stepper.moveTo(100);
stepper.moveTo(-100);
my thinking was that when the pir is triggered HIGH the stepper will move 100 steps clockwise and then after that 100 steps counter clockwise.
In the second part if the pir does not detect any movement and remains low, then nothing should happen.
i cant explain the different boolean variables as this was a basic code i got from this PIRsense code page.
My thinking for the overall project, and what im trying to get the code to do is;
- when powered up, the motion sensor should have time to calibrate.
- when motion is detected the stepper should turn a certain number of steps clockwise, then a certain amount of steps counterclockwise.
- when the stepper is active there should be a delay with the motion sensor being triggered again until the the stepper has finished.
- if no motion is detected, the stepper should do nothing.
#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);
Serial.begin(9600);
pinMode(pirPin, INPUT);
}
void loop()
{
PIRSensor();
stepper.run();
}
void PIRSensor()
{
if (digitalRead(pirPin) == HIGH)
{
if (lockLow)
{
stepper.moveTo(100);
stepper.moveTo(-100);
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;
}
}
}