Accelstepper-28BYJ-48 PIR -sleep

557 views
Skip to first unread message

John

unread,
Mar 23, 2013, 11:03:04 PM3/23/13
to accels...@googlegroups.com
Hello,
  I am making a sculpture in which the 28BYJ-48 stepper is triggered by a PIR.  The stepper will cycle CW, CCW, and CW, then stop until sensing motion again.  Unfortunately, the LEDs for pins 1 and 4 stay on when it stops.  I would like for them to go out until motion is again sensed. Ultimately I will tie this to two steppers.  Any help would be appreciated.

// accellsteppertest.ino
// Runs one stepper forwards and backwards, accelerating and decelerating
// at the limits. Derived from example code by Mike McCauley
// Set for 28BYJ-48 stepper
// Motion sensed from PIR triggers PIR pin 2 high and initiates stepper cycle
//Stops after one cycle of CW-CCW-CW
//sleep until next motion sensed

#include <AccelStepper.h>

#define FULLSTEP 4
#define HALFSTEP 8

//declare variables for the motor pins
int motorPin1 = 8;    // Blue   - 28BYJ48 pin 1
int motorPin2 = 9;    // Pink   - 28BYJ48 pin 2
int motorPin3 = 10;    // Yellow - 28BYJ48 pin 3
int motorPin4 = 11;    // Orange - 28BYJ48 pin 4
                        // Red    - 28BYJ48 pin 5 (VCC)
int ledpin = 13; // choose the pin for the LED
int pirpin = 2; // choose the input pin (for PIR sensor)
int pirstate = 0; // variable to store current pir state
int lastpirstate = 0; // variable to store last pir state
// The sequence 1-3-2-4 required for proper sequencing of 28BYJ48
AccelStepper stepper(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

void setup()
{
stepper.setMaxSpeed(1000);
stepper.setAcceleration(100);
stepper.setSpeed(200);
pinMode(ledpin, OUTPUT); // declare LED as output
pinMode(pirpin, INPUT); // declare sensor as input

}
void loop(){
pirstate = digitalRead(pirpin);
 if(pirstate != lastpirstate) {
    if(pirstate == HIGH)
    digitalWrite(ledpin, HIGH);
 
stepper.moveTo(4500);
while (stepper.currentPosition() != 4000) // Full speed up to 4000
stepper.run();
stepper.stop(); // Stop as fast as possible: sets new target
stepper.runToPosition();
// Now stopped after quickstop
// Now go backwards
stepper.moveTo(-4500);
while (stepper.currentPosition() != 0) // Full speed basck to 0
stepper.run();
stepper.stop(); // Stop as fast as possible: sets new target
stepper.runToPosition();
// Now stopped after quickstop
digitalWrite(ledpin, LOW); //turns off LED

}}

Mike McCauley

unread,
Mar 23, 2013, 11:33:44 PM3/23/13
to accels...@googlegroups.com
Hello,

See disableOutputs() in the documentation.
--
Mike McCauley mi...@open.com.au
Open System Consultants Pty. Ltd
9 Bulbul Place Currumbin Waters QLD 4223 Australia http://www.open.com.au
Phone +61 7 5598-7474 Fax +61 7 5598-7070

Radiator: the most portable, flexible and configurable RADIUS server
anywhere. SQL, proxy, DBM, files, LDAP, NIS+, password, NT, Emerald,
Platypus, Freeside, TACACS+, PAM, external, Active Directory, EAP, TLS,
TTLS, PEAP, TNC, WiMAX, RSA, Vasco, Yubikey, MOTP, HOTP, TOTP,
DIAMETER etc. Full source on Unix, Windows, MacOSX, Solaris, VMS, NetWare etc.

John

unread,
Mar 23, 2013, 11:46:42 PM3/23/13
to accels...@googlegroups.com

Thanks Mike, I figured it out:
stepper.runToPosition();
// Now stopped after quickstop
// Now go backwards
stepper.moveTo(-4500);
while (stepper.currentPosition() != 0) // Full speed basck to 0
stepper.run();

stepper.runToNewPosition(500);
stepper.runToNewPosition(0);

// Now stopped after quickstop
digitalWrite(ledpin, LOW); //turns off LED
stepper.disableOutputs( ); // disables power to motor pins
Reply all
Reply to author
Forward
0 new messages