Accel Stepper Library Help

Hello,

I am working on a rfid enabled door unlocker using an arduino UNO Pololu DRV8825 stepper driver, and RC522 RFID module. Im struggling getting my stepper motors to turn. The code is much too long to post entirely here so im going to post chunks of the important parts to my question here:

Important note: i have the following code working on the arduino:

// ConstantSpeed.pde
// -*- mode: C++ -*-
//
// Shows how to run AccelStepper in the simplest,
// fixed speed mode with no accelerations
/// \author  Mike McCauley (mikem@airspayce.com)
// Copyright (C) 2009 Mike McCauley
// $Id: ConstantSpeed.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $

#include <AccelStepper.h>

AccelStepper stepper(1, 4, 3); 


int slp = 8;


void setup()
{  
  digitalWrite(slp, HIGH);
  //attachInterrupt(buttonInt, unlock, RISING);
  
   stepper.setMaxSpeed(100000);
  
   stepper.setAcceleration(100*4);
   stepper.moveTo(50*8);	
}

void loop()
{  fun();
   
   

}

void fun()
{stepper.run();
}

Now for the Not working code:

First include the accelstepper library

and tell the library what pins are hooked up:

AccelStepper stepper1(1, 4, 3);

then under void setup();

I set my max speed, acceleration, and distance

stepper1.setMaxSpeed(10000);
  stepper1.setAcceleration(100*4);
  stepper1.moveTo(50*8);

then on to my main loop. This basically check the EEPROM to see if a card is stored.

if ( findID(readCard) ) {        // If not, see if the card is in the EEPROM 
        Serial.println("Welcome, You shall pass");
        opendoor();                // Open the door lock 
      }
      else {				// If not, show that the ID was not valid
        Serial.println("You shall not pass");
        
       
        delay(10);

Warning: I am a beginner coder so this code is by no means close to perfect. I an trying my best to get it to work then i will optimize it.

Now for my functions i defined to open the door: The arduino runs the wakedriver() function which sets the slp (sleep pin) to high. The stepper motor is then under holding torque. Unfortunately it dosent do anthing else from h

void wakedriver()
{
  
   digitalWrite(slp, HIGH);    //power driver from sleep
    delay(10);                 // Pause while the driver wakes up
   
}

void opendoor()
{
  wakedriver();                //Run wake driver  I HAVE THE DRIVER WAKING UP AND AT HOLDING TORQUE
  stepper1.run();              //make stepper run based on what was done in setup
  if (stepper1.distanceToGo()==0)      //if the motors have moved the designated amount
  {door();}                            //run door else have the steppers continue running.  not sure if this is needed.
  else
    stepper1.run();
    
}

void door()
{
  if (digitalRead(doorswitch)==HIGH);        //check to see if the switch is pushed
      lockdoor();                             // if it is lock the door
}

void lockdoor()
{
  
   stepper1.moveTo(-50*8);                    //move the steppers in the opposite direction to lock the door.
   digitalWrite(slp, LOW);                    //put the driver in sleep mode to power down stepper.
}

Any and all help would be greatly appreciated

Tyler

First, you need to post all the code so we can see the overall picture. You can add it as an attachment. Problems are often in the parts where you don't look.

The Thread stepper motor basics may be helpful. Also this simple stepper code that does not need any library.

...R

Okay attached is the overall code.

I tried writing some code that didnt use the accelstepper library and it worked! Unfortunately i need acceleration in the application because of the large rotational intertia.

This seems like something that should be simple

RFIDDoorUnlockTyler.ino (20.6 KB)

The usual way to use the AccelStepper library is to have the line stepper1.run(); in loop() and for loop() to repeat hundreds or thousands of times per second.

But your code is organized very differently so that there are big blocking chunks - for example at the start of loop() where is waits for a valie RFID tag.

You are also very liberal with your use of the delay() function. It has no place in anything but the most basic of programs because it blocks the Arduino until it is finished. Use millis() as illustrated in several things at a time

Maybe you could add a WHILE loop into your stepper function to get it to repeat stepper1.run() until the motor has moved where it is meant to go. But that would be a very crude solution as nothing else could happen while that is going on.

...R

The usual way to use the AccelStepper library is to have the line stepper1.run(); in loop() and for loop() to repeat hundreds or thousands of times per second.

I take it this means that it wont work without the loop which is where im running into an issue.

You are also very liberal with your use of the delay() function. It has no place in anything but the most basic of programs because it blocks the Arduino until it is finished. Use millis() as illustrated in several things at a time

Ahh thank you for pointing that out.

Maybe you could add a WHILE loop into your stepper function to get it to repeat stepper1.run() until the motor has moved where it is meant to go. But that would be a very crude solution as nothing else could happen while that is going on.

so something like:

while (stepper1.distanceToGo>0) {

stepper1.run();

that way it would be in a loop. I don't think it will be an issue that the while loop is blocking because the motor only needs to rotate 90 degrees and then wait until a switch tells the motor to turn the opposite way.

Thank you for your help! I will try this code when I get home today

tfinses:
so something like:

while (stepper1.distanceToGo>0) {

stepper1.run();

That's what I had in mind. I think it should work, as long as you don't mind waiting for it to finish.

...R