Accelstepper runSpeed() questions

2,674 views
Skip to first unread message

jimmyv...@gmail.com

unread,
Jan 8, 2018, 6:29:12 PM1/8/18
to accelstepper
Hi,
I have a sketch that runs constant speed that runs fast one way and slow when I try it another way and don't understand why the speed is different.
Here is my code, if I run this it turns the motor very slow

#include <AccelStepper.h>

AccelStepper stapler(AccelStepper::DRIVER, 3, 4);

stapler.setMaxSpeed(10000.0);
  stapler.setSpeed(-5000.0);

  staplercurrentState = digitalRead(staplerSensor);   //set state for movement
  if (buttonstate == HIGH && staplercounter <= 4) {
    stapler.runSpeed();
    if (staplercounter >= 4); {                  //keep checking till it hits 4
      stapler.stop();
    }
  }
  if (staplercurrentState != staplerpreviousState) {
    if (staplercurrentState == 1) {
      staplercounter = staplercounter + 1;
      Serial.println(staplercounter);
    }
  }
  staplerpreviousState = staplercurrentState;
  if (staplercounter >= 4) {
    buttonstate = LOW;                   //reset button
    staplercounter = 0;                    //reset counter for next button push
    staplercurrentState = 0;               //reset state for next button push
  }
// If I add the runSpeed(); here it ignores all the code for my button press  and counter but moves very quickly
}


Why is it so different on speed?

Thanks for any help

gregor

unread,
Jan 9, 2018, 12:58:25 PM1/9/18
to accelstepper
Hi, 

On Tuesday, January 9, 2018 at 12:29:12 AM UTC+1, jimmyv...@gmail.com wrote:
    if (staplercounter >= 4); {                  //keep checking till it hits 4
      stapler.stop();
    }
this code has two errors. 
first, if you are using runSpeed(), stop() does not do anything. to stop the stepper use stapler.setSpeed(0) or stop calling stapler.runSpeed(). see http://www.airspayce.com/mikem/arduino/AccelStepper/classAccelStepper.html#a638817b85aed9d5cd15c76a76c00aced for more information. stop() is only useful when moving with acceleration. 
second, this code is equivalent to:
    if (staplercounter >= 4);     //does not actually do anything if the condition is true, because of the ';' at the end

    stapler.stop();                    //is executed regardless of the if statement above.

also note that runSpeed() only steps the stepper if a step is due. if a step is not due it returns immediately, which causes your next statement to be executed.

I suggest to only call runSpeed() at a single location in your code (e.g. at the end of the loop() function), and use speed changes to control the stepper rather than calling runSpeed() conditionally. 
Could you share more of your code? it's difficult to understand what happens and how the other variables affect the program.

regards,
Message has been deleted

jimmyv...@gmail.com

unread,
Jan 9, 2018, 8:53:24 PM1/9/18
to accelstepper


On Tuesday, January 9, 2018 at 3:30:08 PM UTC-8, jimmyv...@gmail.com wrote:
Hi Gregor,
I have been using the accelerate/deccelarate the past few weeks and was converting over to constant speed to try to increase my speed that's why stop() is in there, it worked so I ran with it.
I totally missed the semi-colon and have taken it out, must have been from all the copy paste I've been doing.

I will add more code for you, I was trying to trim it down because I have 4 steppers and was just trying to make 1 work and apply to other motors. I"m basically turning on the motor and when I pass by the Hall Effect sensor 4 times then everything will stop and go back to their original positions.
here is code for sensor and motor with changes recommended by you and I get movement from the motor but it ignores the setSpeed(0) to stop it.

#include <AccelStepper.h>

AccelStepper stapler(AccelStepper::DRIVER, 3, 4);

//VALUES FOR BUTTON
const int button = 2;            // pin button is on
int val = LOW;                     // current button state
int old_val = LOW;
int buttonstate = LOW;
unsigned long previousMillis = LOW;


//VALUES FOR TURNING STAPLER 4 TURNS(using stapler hall Sensor)
int staplercounter = 0;
int staplercurrentState = 0;
int staplerpreviousState = 0;
int staplerSensor = 47;

void setup() {

  pinMode(button, INPUT_PULLUP);
  pinMode(staplerSensor, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {

  readButton();
  staplefour();
}

void readButton() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= 100) {
    val = digitalRead(button);
    if ((val == LOW) && (old_val == HIGH)) {
      buttonstate = HIGH;
    }
    previousMillis = currentMillis;
    old_val = val;
    Serial.println(buttonstate);
  }
}
void staplefour() {

  stapler.setMaxSpeed(10000.0);

  staplercurrentState = digitalRead(staplerSensor);   //set state for movement
  if (buttonstate == HIGH && staplercounter <= 4) {
    stapler.setSpeed(-5000);
    if (staplercounter >= 4) {                  //keep checking till it hits 4
      stapler.setSpeed(0);
    }
  }
  if (staplercurrentState != staplerpreviousState) {
    if (staplercurrentState == 1) {
      staplercounter = staplercounter + 1;
      Serial.println(staplercounter);
    }
  }
  staplerpreviousState = staplercurrentState;
  if (staplercounter >= 4) {
    buttonstate = LOW;                   //reset button
    staplercounter = 0;                    //reset counter for next button push
    staplercurrentState = 0;               //reset state for next button push
  }
     stapler.runSpeed(); 
}

gregor

unread,
Jan 11, 2018, 3:36:49 PM1/11/18
to accelstepper
Hi, 

in this code
  if (buttonstate == HIGH && staplercounter <= 4) {
   stapler.setSpeed(-5000);
   if (staplercounter >= 4) {                  //keep checking till it hits 4
     stapler.setSpeed(0);
   }
 }
stapler.setSpeed(0); is only called if staplercounter is exactly 4. however, staplercounter will be reset to 0 once it reaches 4 by this code:
  if (staplercounter >= 4) {
   buttonstate = LOW;                   //reset button
   staplercounter = 0;                    //reset counter for next button push
   staplercurrentState = 0;               //reset state for next button push
 }
therefore stapler.setSpeed(0) will never be reached.

jimmyv...@gmail.com

unread,
Jan 11, 2018, 6:00:44 PM1/11/18
to accelstepper
Thanks,
It's always the little things that get me. 
I really appreciate the lesson in how to use the library, It's hard to understand the library when you don't know quite what it's trying to tell you. LOL

jimmyv...@gmail.com

unread,
Jan 14, 2018, 6:55:29 PM1/14/18
to accelstepper
I have one more problem with another motor movement. With the accel/decel code I moved a motor 50 steps and waited a brief time and then moved it again and continued with that action. I thought I had it figured out but it moves without any pause, I will post what I used to use and my attempt at the new way.

OLD WAY
void rosette() {

  stepper2.setMaxSpeed(100.0);
  stepper2.setAcceleration(800.0);

  unsigned long currentMillis2 = millis();
  if (buttonstate == 1) {
    if (currentMillis2 - previousMillis2 >= interval) {
      previousMillis2 = currentMillis2;
      stepper2.move(50);
    }
  }
  stepper2.run();
}

NEW WAY(not working)

void rosette() {

  roserotate.setMaxSpeed(200.0);
  unsigned long currentMillis2 = millis();
  if (buttonstate == 1 ) {
    if (currentMillis2 - previousMillis2 >= interval) {
      previousMillis2 = currentMillis2;
      roserotate.setSpeed(100);
    }
  }
  roserotate.runSpeed();
}

any guidance is appreciated.

gregor

unread,
Jan 15, 2018, 3:23:54 AM1/15/18
to accelstepper
hi,
have you tried using your 'old code' but replacing run() with runSpeedToPosition() ( http://www.airspayce.com/mikem/arduino/AccelStepper/classAccelStepper.html#a9270d20336e76ac1fd5bcd5b9c34f301 )? you still need to set the movement speed with setSpeed().
in your new code, I do not see where / if the steppper is stopped.

jimmyv...@gmail.com

unread,
Jan 15, 2018, 10:20:35 AM1/15/18
to accelstepper
Thanks Gregor,

I read that runToPosition() was blocking and just assumed runSpeedToPosition() was also blocking, I haven't added code to stop it yet because I don't have all the motors running at the same time for the counters to count and force a stop.
Message has been deleted

jimmyv...@gmail.com

unread,
Jan 25, 2018, 1:59:59 PM1/25/18
to accelstepper


On Thursday, January 11, 2018 at 12:36:49 PM UTC-8, gregor wrote:
Hi, 

in this code
  if (buttonstate == HIGH && staplercounter <= 4) {
   stapler.setSpeed(-5000);
   if (staplercounter >= 4) {                  //keep checking till it hits 4
     stapler.setSpeed(0);
   }
 }
stapler.setSpeed(0); is only called if staplercounter is exactly 4. however, staplercounter will be reset to 0 once it reaches 4 by this code:
  if (staplercounter >= 4) {
   buttonstate = LOW;                   //reset button
   staplercounter = 0;                    //reset counter for next button push
   staplercurrentState = 0;               //reset state for next button push
 }
therefore stapler.setSpeed(0) will never be reached.



Hi gregor,

I'm sill having a problem with this part of the code, it worked as written with the access/decell code going by the hall sensor 4 times and then stopping and resetting the sensor and the button and waiting for the next button press.
 I don't know why it doesn't operate the same way with the constant speed code, if I change the staplercounter to 3 then the button and hall sensor don't reset and if I change both to 3 then I end up with the same situation of the motor not stopping. The code as written say's greater than or equal to 4 not exactly 4 or am I misunderstanding what that means, it works for one code but not the other and not sure why.
WORKING CODE
 if (buttonstate == 1) {
    stapler.move(400);
    if (hallcounter >= 4) {                  //keep checking till it hits 4
      stapler.stop();
and then

  if (hallcounter >= 4) {
     buttonstate = LOW;
     hallcounter = 0;
     hallcurrentState = 0;
  }

gregor

unread,
Jan 26, 2018, 5:51:43 AM1/26/18
to accelstepper
Hi,

can you post your whole code again, the working and not working versions?
keep in mind that stop() does not do anything if you are moving the stepper with runSpeed(), in this case you need to call setSpeed(0) instead of stop().

jimmyv...@gmail.com

unread,
Jan 26, 2018, 9:18:43 AM1/26/18
to accelstepper
WORKING CODE:

#include <AccelStepper.h>

AccelStepper stapler(AccelStepper::DRIVER, 3, 4);

const int button = 2;            // pin button is on
int val = LOW;                     // current button state
int old_val = LOW;
int buttonstate = LOW;
unsigned long previousMillis = LOW;

//VALUES FOR TURNING STAPLER 4 TURNS(using position Sensor)
int hallcounter = LOW;
int hallcurrentState = LOW;
int hallpreviousState = LOW;
int positionSensor = 35;

void setup() {

  pinMode(button, INPUT_PULLUP);
  pinMode(positionSensor, INPUT_PULLUP);
  stapler.setMaxSpeed(1000);
  stapler.setAcceleration(6000);
  Serial.begin(9600);

}

void loop() {

 hallcurrentState = digitalRead(positionSensor);

 
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= 100) {
    val = digitalRead(button);
    if ((val == LOW) && (old_val == HIGH)) {
      buttonstate = HIGH;
    }
    previousMillis = currentMillis;
    old_val = val;
  }
  if (buttonstate == HIGH) {

    stapler.move(400);
    if (hallcounter >= 4) {                  //keep checking till it hits 4
      stapler.stop();
    }
  }
  if (hallcurrentState != hallpreviousState) {
    if (hallcurrentState == 1) {
      hallcounter = hallcounter + 1;
      Serial.println(hallcounter);
    }
  }
  hallpreviousState = hallcurrentState;

   if (hallcounter >= 4) {
     buttonstate = LOW;
     hallcounter = 0;
     hallcurrentState = 0;
  }
  stapler.run();
}
_______________________________________________________________

NON WORKING(trying to change to this for more speed):

#include <AccelStepper.h>

AccelStepper stapler(AccelStepper::DRIVER, 3, 4);

const int button = 2;            // pin button is on
int val = LOW;                     // current button state
int old_val = LOW;
int buttonstate = LOW;
unsigned long previousMillis = LOW;

//VALUES FOR TURNING STAPLER 4 TURNS(using position Sensor)
int hallcounter = 0;
int hallcurrentState = 0;
int hallpreviousState = 0;
int positionSensor = 35;

void setup() {

  pinMode(button, INPUT_PULLUP);
  pinMode(positionSensor, INPUT_PULLUP);
  Serial.begin(9600);

}

void loop() {

 hallcurrentState = digitalRead(positionSensor);

 
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= 100) {
    val = digitalRead(button);
    if ((val == LOW) && (old_val == HIGH)) {
      buttonstate = HIGH;
    }
    previousMillis = currentMillis;
    old_val = val;
  }
 stapler.setMaxSpeed(10000.0);

  hallcurrentState = digitalRead(positionSensor);   //set state for movement
  if (buttonstate == HIGH) {
    stapler.setSpeed(-5000);

    if (hallcounter >= 4) {                  //keep checking till it hits 4
      stapler.setSpeed(0);
    }
  }
  if (hallcurrentState != hallpreviousState) {
    if (hallcurrentState == 1) {
      hallcounter = hallcounter + 1;
      Serial.println(hallcounter);
    }
  }
  hallpreviousState = hallcurrentState;
  if (hallcounter >= 4) {
    buttonstate = LOW;                   //reset button
    hallcounter = 0;                    //reset counter for next button push
    hallcurrentState = 0;               //reset state for next button push
  }
     stapler.runSpeed(); 
}

jimmyv...@gmail.com

unread,
Jan 26, 2018, 11:36:59 AM1/26/18
to accelstepper
Hi gregor,
With the new code above when I open the serial window and watch the serial prints I have the screen starts with 0's running down the screen showing the button is low, when the button is pressed 1's start rolling down the screen and the motor starts to turn and then you see the count for the hall sensor. When the count hits 4 on the sensor the button registers low with 0's running down the screen and the hall sensor continues to count one through 4 for the motor revolutions because it didn't stop.

jimmyv...@gmail.com

unread,
Jan 30, 2018, 9:32:05 AM1/30/18
to accelstepper
Hi gregor,
I got the code working last night, I had to move the stapler.runSpeed above the hall sensor counting portion of the code and it works fine now.
Thanks for all the help.
NOW WORKING:
     stapler.runSpeed(); 
  }
  if (hallcurrentState != hallpreviousState) {
    if (hallcurrentState == 1) {
      hallcounter = hallcounter + 1;
      Serial.println(hallcounter);
    }
  }
  hallpreviousState = hallcurrentState;
  if (hallcounter >= 4) {
    buttonstate = LOW;                   //reset button
    hallcounter = 0;                    //reset counter for next button push
    hallcurrentState = 0;               //reset state for next button push
  }
     (moved stapler.runSpeed(); from here  

jimmyv...@gmail.com

unread,
Jan 31, 2018, 9:29:51 PM1/31/18
to accelstepper
I'm working with another motor's movement and I guess I just can't seem to wrap y head around what needs to go where to make them move properly. This motor moves in to a hall sensor just like the other ones but then it turns back out to where it started, the first part works perfect but the moving out part doesn't want to stop after the assigned amount of steps, not sure where I have gone wrong.
Here's the code:
#include <AccelStepper.h>

AccelStepper gantry(AccelStepper::DRIVER, 5, 6);


const int button = 2;            // pin button is on
int val = LOW;                     // current button state
int old_val = LOW;
int buttonstate = LOW;
unsigned long previousMillis = LOW;

//VALUES FOR TURNING STAPLER 4 TURNS(using stapler hall Sensor)
int staplercounter = 0;
int staplercurrentState = 0;
int staplerpreviousState = 0;
int staplerSensor = 47;

//VALUE FOR GANTRY MOVE
int gantrysensor = 46;
int gantryhome = 0;


void setup() {

  pinMode(button, INPUT_PULLUP);
  pinMode(gantrysensor, INPUT_PULLUP);
  pinMode(staplerSensor, INPUT_PULLUP);
  Serial.begin(9600);

}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= 100) {
    val = digitalRead(button);
    if ((val == LOW) && (old_val == HIGH)) {
      buttonstate = HIGH;
    }
    previousMillis = currentMillis;
    old_val = val;
    //Serial.println(buttonstate);

    staplercurrentState = digitalRead(staplerSensor);
    if (staplercurrentState != staplerpreviousState) {    //check the count and add 1
      if (staplercurrentState == 1) {
        staplercounter = staplercounter + 1;
        Serial.println(staplercounter);
      }
    }
    staplerpreviousState = staplercurrentState;
  }
  gantry.setMaxSpeed(4000.0);

  gantryhome = digitalRead(gantrysensor);
  if (buttonstate == HIGH && gantryhome == HIGH && staplercounter < 3) {
    gantry.move(50);                                 //move to sensor
    gantry.setSpeed(1000);
    gantryhome = digitalRead(gantrysensor);
  } 
  if (staplercounter >= 4) {
    gantry.setSpeed(1000);
    gantry.move(-200);
  }
  gantry.runSpeedToPosition();
}

gregor

unread,
Feb 1, 2018, 3:15:21 AM2/1/18
to accelstepper
Hi,

this code

  if (staplercounter >= 4) {
   gantry.setSpeed(1000);
   gantry.move(-200);
 }
is executed in every loop if staplercounter >= 4, so each time the program executes this code (which can happen several thousand times / second) the target position is shifted by -200 steps.
you need to make sure to pass new relative coordinates only once (or use absolute coordinates instead).

gregor

unread,
Feb 1, 2018, 3:33:53 AM2/1/18
to accelstepper
On Thursday, February 1, 2018 at 9:15:21 AM UTC+1, gregor wrote:
each time the program executes this code (which can happen several thousand times / second) the target position is shifted by -200 steps.
this in not correct. each time the program executes this code, the target position is reset to -200 steps from the current position, so the stepper can never reach its destination.
sorry, haven't had enough caffeine yet ;-)

jimmyv...@gmail.com

unread,
Feb 1, 2018, 8:15:59 AM2/1/18
to accelstepper
Now all the examples adding the position data into the setup makes sense, it only runs once. I changed move to moveTo and that makes it stop appropriately. I could have sworn that was the first thing I tried but I must have had something else in there that didn't make it work.
Appreciate the help.

jimmyv...@gmail.com

unread,
Feb 5, 2018, 12:42:50 PM2/5/18
to accelstepper
I have added everything together so I have 4 motors moving at the same time, the problem I have now is I was trying to time the stapler stepper to start a little later than the other steppers so I put a millis delay in to make it start later but the other steppers move and then just sit there because the stapler doesn't move, if I remove the millis delay all motors run correctly, any ideas where I went wrong.

#include <AccelStepper.h>

AccelStepper stapler(AccelStepper::DRIVER, 3, 4);
AccelStepper roserotate(AccelStepper::DRIVER, 12, 13);
AccelStepper gantry(AccelStepper::DRIVER, 5, 6);
AccelStepper carriage(AccelStepper::DRIVER, 7, 11);

//VALUES FOR BUTTON
const int button = 2;            // pin button is on
int val = LOW;                     // current button state
int old_val = LOW;
int buttonstate = LOW;
unsigned long currentMillis = millis();
unsigned long previousMillis = 0;


//VALUES FOR TURNING STAPLER 4 TURNS(using stapler hall Sensor) plus delay the start
int staplercounter = 0;
int staplercurrentState = 0;
int staplerpreviousState = 0;
int staplerSensor = 47;
unsigned long previousMillis3 = 0;
const long pause = 1000;



//VALUE FOR PAUSING ROSETTE TURN
unsigned long previousMillis2 = 0;
const long interval = 250;

//VALUE FOR GANTRY MOVE
int gantrysensor = 46;
int gantryhome = 0;

//VALUE FOR CARRIAGE MOVE
int carriagesensor = 45;
int carriagehome = 0;

void setup() {

  pinMode(button, INPUT_PULLUP);
  pinMode(staplerSensor, INPUT_PULLUP);
  pinMode(gantrysensor, INPUT_PULLUP);
  pinMode(carriagesensor, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  starter();
  counter();

  gantry.setMaxSpeed(10000.0);
  gantryhome = digitalRead(gantrysensor);
  if (buttonstate == HIGH && gantryhome == HIGH && staplercounter < 3) {
    gantry.move(50);                                 //move to sensor for stapling
    gantry.setSpeed(1000);
    gantryhome = digitalRead(gantrysensor);
  }
  gantry.runSpeedToPosition();
  ///////////////////////////////////////////////////////////////////////
  carriage.setMaxSpeed(10000.0);
  carriagehome = digitalRead(carriagesensor);
  if (buttonstate == HIGH && carriagehome == HIGH && staplercounter < 3) {
    carriage.move(-50);                         //move to sensor for stapling
    carriage.setSpeed(2000);
    carriagehome = digitalRead(carriagesensor);
  }
  carriage.runSpeedToPosition();
  //////////////////////////////////////////////////////////////////////
  stapler.setMaxSpeed(10000.0);
  staplercurrentState = digitalRead(staplerSensor);   //set state for movement
  if (buttonstate == HIGH) {
    if (currentMillis - previousMillis3 > pause) {           //short pause for slider & gantry move
      previousMillis3 = currentMillis;
      stapler.setSpeed(-5000);
      if (staplercounter >= 4) {
        stapler.setSpeed(0);                      //turn motor off
      }
    }
    stapler.runSpeed();
  }
  ///////////////////////////////////////////////////////////////////////////////
  roserotate.setMaxSpeed(1000);          //rotate substrate at intervals
  if (buttonstate == HIGH && staplercounter >= 1) {
    if (currentMillis - previousMillis2 >= interval) {
      previousMillis2 = currentMillis;
      roserotate.move(50);
      roserotate.setSpeed(800);
      staplercurrentState = digitalRead(staplerSensor);
      if (staplercounter >= 3) {
        roserotate.setSpeed(0);
      }
    }
  }
  roserotate.runSpeedToPosition();
  ///////////////////////////////////////////////////////////////////////////////
  gantry.setMaxSpeed(10000.0);
  if (staplercounter >= 4) {          //Slide gantry back to start position
    gantry.moveTo(-200);
    gantry.setSpeed(1000);
  }
  gantry.runSpeedToPosition();
  /////////////////////////////////////////////////////////////////////////////
  carriage.setMaxSpeed(10000.0);
  if (staplercounter >= 4) {        //slide carriage back to start position
    carriage.moveTo(400);
    carriage.setSpeed(2000);
  }
  carriage.runSpeedToPosition();
  ////////////////////////////////////////////////////////////////////
  if (staplercounter >= 4) {
    buttonstate = LOW;                   //reset button
    staplercounter = 0;                    //reset counter for next button push
    staplercurrentState = 0;               //reset state for next button push
  }
}

void starter() {                            //state machine for button & debounce
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= 100) {
    val = digitalRead(button);
    if ((val == LOW) && (old_val == HIGH)) {
      buttonstate = HIGH;
    }
    previousMillis = currentMillis;
    old_val = val;
    //Serial.println(buttonstate);
  }
}
void counter() {                                      //used to time all events

gregor

unread,
Feb 5, 2018, 5:14:30 PM2/5/18
to accelstepper
delay() stops the program, the other steppers runSpeedToPosition() is not called until the delay has expired. don't use delays when using AccelStepper.

jimmyv...@gmail.com

unread,
Feb 5, 2018, 6:46:33 PM2/5/18
to accelstepper
I'm not using delay(), I'm using millis for pausing the start time of the stapler stepper. The pause time should be expired before the motor movement is called with stapler.setSpeed() and then at the end stapler.runSpeed() is called.
All the motors move to position but then wait for the stapler stepper to move but it doesn't so the rest of the code doesn't run.

The milis delay for turning the roserotate stepper works fine but I can't seem to make the stapler stepper run with it.

gregor

unread,
Feb 6, 2018, 2:55:05 AM2/6/18
to accelstepper
sorry, I was too quick to judge you when I saw you mention "delay" ;-)

I tried rearranging and cleaning up the code but did not find anything that would cause the program to stop.
void setup() {
    pinMode
(button, INPUT_PULLUP);
    pinMode
(staplerSensor, INPUT_PULLUP);
    pinMode
(gantrysensor, INPUT_PULLUP);
    pinMode
(carriagesensor, INPUT_PULLUP);
   
   
Serial.begin(9600);


    gantry
.setMaxSpeed(10000.0);
    carriage
.setMaxSpeed(10000.0);
    stapler
.setMaxSpeed(10000.0);

    roserotate
.setMaxSpeed(1000);          //rotate substrate at intervals

    gantry
.setMaxSpeed(10000.0);
    carriage
.setMaxSpeed(10000.0);  
}

void loop() {
    starter
();
    counter
();

    gantryhome
= digitalRead(gantrysensor);
    carriagehome
= digitalRead(carriagesensor);

    staplercurrentState
= digitalRead(staplerSensor);   //set state for movement

   
if (buttonstate == HIGH) {
       
if (currentMillis - previousMillis3 > pause) {           //short pause for slider & gantry move
            previousMillis3
= currentMillis;
            stapler
.setSpeed(-5000);
       
}


       
if (gantryhome == HIGH && staplercounter < 3) {

            gantry
.move(50);                                 //move to sensor for stapling
            gantry
.setSpeed(1000);
       
}


       
if (carriagehome == HIGH && staplercounter < 3) {

            carriage
.move(-50);                         //move to sensor for stapling
            carriage
.setSpeed(2000);
       
}


       
if (staplercounter >= 1) {

           
if (currentMillis - previousMillis2 >= interval) {
                previousMillis2
= currentMillis;
                roserotate
.move(50);
                roserotate
.setSpeed(800);

               
               
if (staplercounter >= 3) {
                    roserotate
.setSpeed(0);
               
}
           
}
       
}
   
}

   
///////////////////////////////////////////////////////////////////////////////

   
if (staplercounter >= 4) {          //Slide gantry back to start position

        stapler
.setSpeed(0);                      //turn motor off
               
        gantry
.moveTo(-200);
        gantry
.setSpeed(1000);

        carriage
.moveTo(400);
        carriage
.setSpeed(2000);


        buttonstate
= LOW;                   //reset button
        staplercounter
= 0;                    //reset counter for next button push
        staplercurrentState
= 0;               //reset state for next button push
   
}


    stapler
.runSpeed();
    gantry
.runSpeedToPosition();
    carriage
.runSpeedToPosition();
    roserotate
.runSpeedToPosition();
}

jimmyv...@gmail.com

unread,
Feb 6, 2018, 12:47:54 PM2/6/18
to accelstepper
Thanks gregor,
That is a bit easier to read, one question on the setMaxSpeed in the setup(), do I need to specify the gantry and the carriage speeds twice since they are the same speed?

I put this in my IDE and added the rest of the code for the button and counter and it runs just like my previous code, it ignores the stapler stepper millis pause but does the millis for the roserotate stepper. I'm at a loss for the different behavior.

gregor

unread,
Feb 6, 2018, 2:22:09 PM2/6/18
to accelstepper
Hi,


On Tuesday, February 6, 2018 at 6:47:54 PM UTC+1, jimmyv...@gmail.com wrote:
Thanks gregor,
That is a bit easier to read, one question on the setMaxSpeed in the setup(), do I need to specify the gantry and the carriage speeds twice since they are the same speed?
no, this was an oversight on my part.

jimmyv...@gmail.com

unread,
Feb 11, 2018, 2:25:11 PM2/11/18
to accelstepper
Hi gregor,
I was having a discussion on another forum about Accelstepper and they were adamant about runSpeedToPosition() being a blocking command and I told them I was running 3 steppers at the same time using it without any problems. I read the doc and it doesn't specifically state that it is or isn't so I thought I would ask.
The person thought the motors were running because my code was somehow so messed up it really wasn't doing what I thought it was doing, I based it off the earlier code you cleaned up for me.

gregor

unread,
Feb 11, 2018, 4:57:20 PM2/11/18
to accelstepper
Hi, 

runSpeedToPosition() does not block. Here's the function definition of runSpeedToPosition() (comments by me):
boolean AccelStepper::runSpeedToPosition()
{
    if (_targetPos == _currentPos)   //if the stepper is already at the target position
return false;                //do nothing
    if (_targetPos >_currentPos)     //else, set the movement direction accordingly
_direction = DIRECTION_CW; 
    else
_direction = DIRECTION_CCW;
    return runSpeed();               //and take a step if a step is due. does nothing if a step is not due.
}
runSpeed() returns immediately if a step is not due, if a step is due it sets the pins accordingly (or calls the callback functions, if you have passed any in the constructor), then returns.

regards,
Reply all
Reply to author
Forward
0 new messages