Stepper motor Closed Loop Problems

I posted a simple question about stepper motors before. Everyone who commented helped out a lot. So I am trying to use the same code that I had help with before.

So the code listed incorporates 2 buttons. One button drives the motor clockwise and the other drives the motor counter clockwise.

I was wondering if I could use the concept of this code and use one button instead. Like the concept of a garage door button. I am also using a feed back Pot that I will be getting soon to make the system closed loop.

I was also wondering if I would be able to use a LCD or Serial COMM to display: Direction, Speed, Position.

With the code I am using now for the 2 button setup, I am seeing a small problem when it is running. When I push the button and let go, it will keep turning and stop at a random location, instead of stopping as soon as I release the button.

Sometimes the motor will just vibrate instead of turn and will also rotate a short distance in the opposite direction than the input direction before correcting itself and turning properly.

Stepper Motor: PFCU20-40S4GM2

#include <Stepper.h> //including stepper motor library

//defining pins section

int stepIN1Pin = 4;         
int stepIN2Pin = 5;
int stepIN3Pin = 6;
int stepIN4Pin = 7;
int stepsPerRevolution = 400; // amount of steps per revolution

const int button1Pin = 2;  // pushbutton 1 pin for clockwise rotation
const int button2Pin = 3;  // pushbutton 2 pin for counter clockwise rotation

Stepper myStepper(stepsPerRevolution, stepIN1Pin, stepIN3Pin, stepIN2Pin, stepIN4Pin);

void setup() {
  // Set up the pushbutton pins to be an input:
  pinMode(button1Pin, INPUT_PULLUP);
  pinMode(button2Pin, INPUT_PULLUP);
  myStepper.setSpeed(60); //Motor RPM
}

void loop() {
 
int button1State, button2State;
  button1State = digitalRead(button1Pin);
  button2State = digitalRead(button2Pin);
 if (((button1State == LOW) && !(button2State == LOW)))  // if we're pushing button 1 OR button 2
   myStepper.step(stepsPerRevolution);
 if (((button2State == LOW) && !(button1State == LOW)))  // if we're pushing button 1 OR button 2
   myStepper.step(-stepsPerRevolution);
}

Just use the button push to toggle.

if buttonState is LOW
stepsPerRevolution = -stepPerRevoltion
myStepper.step(stepsPerRevolution)

alouisallen:
I posted a simple question about stepper motors before.

It helps if you include a link to the earlier Thread so we don't repeat ourselves.

When I push the button and let go, it will keep turning and stop at a random location, instead of stopping as soon as I release the button.

I suspect the problem is that the standard Stepper library blocks the Arduino until it completes the number of steps. Either get it to move one step at a time (which, of course, means that you have to control the speed yourself) or use the AccelStepper library which has a non-blocking mode.

...R
Stepper Motor Basics
Simple Stepper Code

https://forum.arduino.cc/index.php?topic=537689.msg3664453#msg3664453