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);
}