accelStepper speed issue.

Hello I am having issues with accelStepper giving me enough speed. I have used the standard stepper motor library and got much better performance. I am using a 16Mhz Nano. with a step and direction style driver. This nano should be fast enough to run this single stepper at 100 steps per second. with the code below, why am I only getting about 25 pulse per second. Later on I will need the accelerate function of this library.

#include <AccelStepper.h>

AccelStepper target(1, 6, 8); // pin 6 = pulse, pin 8 = direction
const int buttonRT = 11;
const int buttonBK = 10;
const int buttonLT = 9;
int Desired_position = 0;

void setup() {
  pinMode(buttonRT, INPUT_PULLUP);
  pinMode(buttonBK, INPUT_PULLUP); 
  pinMode(buttonLT, INPUT_PULLUP);

void loop(){ 
    // position control
   {while (digitalRead(buttonRT) == LOW) {Desired_position = 50;}
   //wired N.O.
    while (digitalRead(buttonBK) == HIGH) {Desired_position = 0;}
    //wired N.C.
    while (digitalRead(buttonLT) == LOW) {Desired_position = -50;}}
    //wired N.O.
      //Stepper control
     //{ int Desired_position = 0;
     // target.moveTo(Desired_position);}
    // Read new position
   // read the input pin:
  int buttonState1 = digitalRead(buttonRT);
  int buttonState2 = digitalRead(buttonBK);
  int buttonState3 = digitalRead(buttonLT);
  // print out the state of the button:
  delayMicroseconds(10); // delay in between reads for stability


Try losing all the print statements and the delay(). I suspect the WHILEs don't help either.

If you don't need acceleration look at the simple code in this simple stepper demo - especially the second example.



That did it?? It is depressing that a print statement is so expensive. How might I include a print statement that does not eat up as much resources. Maybe print only when it has reached position? Sorry I am talking out loud now. Thanks again.

weldsmith: Maybe print only when it has reached position?

That's probably the only option if the Arduino is too busy generating lots of pulses. If you just want to monitor progress you could maybe flash an LED.

Print less and use a high baud rate. Don't print it all between steps. Put in extra lines with target.runSpeedToPosition();

You could control your print statements with an IF statement which also sets a much lower step rate so that you could debug the code at slow speed and then switch to the high speed when everything works.