Issue implementing accelstepper

Hello forum!

I've been learning Arduino for some weeks in order to create an application that lets me input 2 values trough an LCD with keypad: the amount of steps, and the speed

I've managed to create 2 void functions: the first function requests to input the steps, and stores the value, and the second function does the same with the speed.

However, I'm not able to move the stepper using the accelstepper library. I have no issues doing this exact same thing but with the standard stepper library (by using digitalWrite).

The general idea in pseudocode:

void setup() {


stepper.setMaxSpeed(3000);
stepper.setAcceleration(1000);
stepper.setSpeed(1000);

}


void loop() {

  getSteps(); // Lets user input value and stores value into 'steps'
  getSpeed(); // Lets user input value and stores value into 'speed'
  stepper.move(steps);
  stepper.setSpeed(speed);
  stepper.runSpeedToPosition();


  
}

Even if I replace 'steps' and 'speed' with a number, i can't seem to get it to work.
Is there something I'm missing? I don't need to use acceleration, I just want to move the motor with x amount of steps with y amount of steps/second

Thanks in advance :slight_smile:

You need to post the complete program so we can see how your stepper instance is defined.

...R

You code basically worked for me, once I turned your part-sketch into something I could compile, and with the right parameters for my 28BYJ-48 stepper motor with ULN2003 driver board. I say “basically” because the motor never stopped.

I found if you use stepper.move() in loop like that, it will never stop moving. It’s a move relative to the current position, so every time it sees that command it knows to move steps from that point. But since the current position is moving, it’s always trying to move steps ahead from a moving point and chases its tail for ever.

The code below moves my stepper 8192 steps (2 revs for me) from wherever it was at reset. The values for speed and steps are hardcoded into my functions. These lines:

//these were in loop()
  getSteps(); // value hardcoded into function for now
  stepper.move(steps);

… are moved from loop() into setup() so that move() is only done once.

// https://forum.arduino.cc/index.php?topic=645911
// 12 nov 2019

//stepper stuff
// Include the AccelStepper library:
#include <AccelStepper.h>
// Motor pin definitions:
#define motorPin1  8      // IN1 on the ULN2003 driver
#define motorPin2  9      // IN2 on the ULN2003 driver
#define motorPin3  10     // IN3 on the ULN2003 driver
#define motorPin4  11     // IN4 on the ULN2003 driver
// Define the AccelStepper interface type; 4 wire motor in half step mode:
#define MotorInterfaceType 8
// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper library with 28BYJ-48 stepper motor:
AccelStepper stepper = AccelStepper(MotorInterfaceType, motorPin1, motorPin3, motorPin2, motorPin4);
int steps;
int speed;

void setup()
{
  Serial.begin(9600);
  Serial.println("forum 645911");
  
  // from orig
  stepper.setMaxSpeed(3000);
  stepper.setAcceleration(1000);
  stepper.setSpeed(1000);

  //these were in loop()
  getSteps(); // value hardcoded into function for now
  stepper.move(steps);

  Serial.println("setup() done");
  Serial.println(" ");

} //setup

void loop()
{ 
  getSpeed(); //  value hardcoded into function for now
  stepper.setSpeed(speed);
  stepper.runSpeedToPosition();
} //loop

void getSpeed()
{
  speed = 500;
} //getSpeed

void getSteps()
{
  steps = 8192;
} //getSteps

Your pseudocode is too simple.

"Input with keypad" is the hard part. sayHovis didn't attempt that. I'm searching Google for a relevant example but all the tutorials I can find just show the HelloKeypad.ino example which only tests that the keypad is connected.

This post by econjack in 2015 shows you how to build up a number or string input from a keypad.

Remember that the Arduino must keep actively controlling the motor and responding to other inputs while the user is thinking about what button to push. Make sure that loop() always loops. When you get a finished input (# is usually used as the "enter" key on a keypad, * is back or cancel) then you can act on that input ... While also continuing to read the keypad for more input during the motor move.

MorganS:
"Input with keypad" is the hard part. sayHovis didn't attempt that.

That's true, and I didn't. But that was because OP said this:

Even if I replace 'steps' and 'speed' with a number, i can't seem* to get it to work.

... which would by-pass the keypad requirement so I went about showing the code could work with hard-coded numbers.

  • for some definition of "can't seem to get it to work"

It's also not clear (to me) if:

an LCD with keypad

... means a plain lcd and a separate membrane keypad (as in the link to econjack) or one of those lcds with 5 or so buttons on the board, eg the DFR0009 or adafruit I2C equivalent.

If it's the DFR0009 style, then member jurs' #8 here shows how to dial in a value using the up/down, left/right and select buttons.