Controlling AccelStepper with AnalogRead

Hi there -
I'm hoping to change the speed of two stepper motors and ended up using the AccelStepper library.

At the moment, my code/the potentiometers at A1 and A2 does not seem to be changing the speed, though. I am curious if I did something obviously wrong here. I was hoping someone on the forum might have some insight. Thanks!

#include <AccelStepper.h>

const int inputA = A1;
const int inputB = A2;
const int In1 = 5;
const int In2 = 6;
const int In3 = 7;
const int In4 = 8;
const int In1b = 9;
const int In2b = 10;
const int In3b = 11;
const int In4b = 12;
int val1 = 0;         // variable to store the read value
int val2 = 0;         // variable to store the read value


AccelStepper myStepper1(AccelStepper:: FULL4WIRE, In1, In3, In2, In4);
AccelStepper myStepper2(AccelStepper:: FULL4WIRE, In1b, In3b, In2b, In4b);

void setup() {
  myStepper1.setMaxSpeed(1000);
  myStepper1.setSpeed(700);

  myStepper2.setMaxSpeed(1000);
  myStepper2.setSpeed(600);


}

void loop() {
val1 = analogRead(inputA);
val2 = analogRead(inputB);

myStepper1.setSpeed(val1);
myStepper2.setSpeed(val2);

myStepper1.runSpeed();
myStepper2.runSpeed();
delay(250);

}

With that delay(250), it looks like the upper limit on speed is 4 steps/sec, which is lower than 99% of the possible analogRead values:

Stepper.runSpeed()

1 Like

Ah - I see. Thank you for this insight.
How is this generally dealt with? I know that in other examples, the delay is used to prevent the Arduino from polling too frequently. Should I just remove it completely?

In many cases, you want things to poll as fast as possible. Two of the functions that actually move the steppers in accelstepper (stepper.run() and stepper.runSpeed()) are designed to be called as fast as possible, so that they get a chance to make steps at the right times without impacting other things you might want to do.

If you want to avoid polling the pots thousands of times per second, you might wrap them in some code to rate-limit their effect (completely untested code):

#include <AccelStepper.h>

const int inputA = A1;
const int inputB = A2;
const int In1 = 5;
const int In2 = 6;
const int In3 = 7;
const int In4 = 8;
const int In1b = 9;
const int In2b = 10;
const int In3b = 11;
const int In4b = 12;
int val1 = 0;         // variable to store the read value
int val2 = 0;         // variable to store the read value
uint32_t lastUI = 0; // memory for rate-limiting the UI


AccelStepper myStepper1(AccelStepper:: FULL4WIRE, In1, In3, In2, In4);
AccelStepper myStepper2(AccelStepper:: FULL4WIRE, In1b, In3b, In2b, In4b);

void setup() {
  myStepper1.setMaxSpeed(1000);
  myStepper1.setSpeed(700);

  myStepper2.setMaxSpeed(1000);
  myStepper2.setSpeed(600);
}

void loop() {
  if(millis() - lastUI >= 250){
     lastUI = millis(); // remember last UI polling time
     val1 = analogRead(inputA);
     val2 = analogRead(inputB);
     myStepper1.setSpeed(val1);
     myStepper2.setSpeed(val2);
  }

  myStepper1.runSpeed();
  myStepper2.runSpeed();
  //delay(250);
}

(PS: You might edit your title to be something like "Controlling AccelStepper with AnalogRead")

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.