greetings,
I am setting up a kinetic sculpture that is trigger by a PIR sensor. What I want to achieve is for the stepper to cycle forward and back several times (varying position), then reset to the zero position and sleep until motion is detected again. I have it set to cycle once in this sketch with content borrowed from the easy stepper, however it is inconsistent and instead of cycling back to zero, it stops and gravity drops the sculpture. Guidance would be appreciated.
// B2 is Blue, B1 is RED, A1 is GREEN, A2 is BLACK
// DRV8825 driving 1206a stepper, 4v at 1200ma per coil
// steps forward then back
// Direction pin is 8, Stepper pin is 7
// derived from the Stepper Motor sketch for use with the EasyDriver v4.2 by Brian Schmalz
// MODE0-L, MODE1-L, MODE2-H, Microstep Resolution-1/16
// Triggered by a PIR sensor on pin 2 which sets pin 12 (sleep) high and sets (sleep) low after no
int Distance = 0; // Record the number of steps we've taken
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirpin = inputPin;
int pirState = LOW; // we start, assuming no motion detected
int val = 0;
void setup() {
pinMode(8, OUTPUT); // direction
pinMode(7, OUTPUT); // Stepper
pinMode(pirpin, INPUT); // pir pin 2
pinMode(12, OUTPUT); // output to sleep pin on DRV 8825
digitalWrite(8, LOW); // sets direction low
digitalWrite(7, LOW); // sets steeper low
}
void loop() {
val = digitalRead(inputPin); // read PIR input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(12, HIGH); // if input high writes to (sleep) high
pirState = HIGH;
}
digitalWrite(7, HIGH); //stepper high
delayMicroseconds(500);
digitalWrite(7, LOW); //stepper low
delayMicroseconds(100);
Distance = Distance + 1; // record this step
if (Distance == 16000)// Check to see if we are at the end of our move
{
// We are! Reverse direction (invert DIR signal)
if (digitalRead(8) == LOW)
{
digitalWrite(8, HIGH);
}
else
{
digitalWrite(8, LOW);
}
// Reset our distance back to zero since we're
// starting a new move
Distance = 0;
// Now pause for half a second
delay(500);
val = digitalRead(inputPin); // read input value
if (pirState == LOW)
digitalWrite(12, LOW);
pirState = LOW;
}}