This is my code so far it works perfectly but I want to add a release function so that the stepper motor doesn't vibrate and instead turns off when no button is pressed.
#include <Stepper.h>
// Define the number of steps per revolution for your stepper motor
const int stepsPerRevolution = 200;
// Define the pins connected to the motor driver
const int motorPin1 = 8;
const int motorPin2 = 9;
const int motorPin3 = 10;
const int motorPin4 = 11;
// Define the pins connected to the buttons
const int buttonPinClockwise = 2; // Button for clockwise rotation
const int buttonPinCounterclockwise = 3; // Button for counterclockwise rotation
// Initialize the stepper motor object
Stepper myStepper(stepsPerRevolution, motorPin1, motorPin2, motorPin3, motorPin4);
void setup() {
// Set the buttons as inputs
pinMode(buttonPinClockwise, INPUT_PULLUP);
pinMode(buttonPinCounterclockwise, INPUT_PULLUP);
// Set the motor speed (you can adjust this value as needed)
myStepper.setSpeed(60); // RPM
}
void loop() {
// Check if the clockwise button is pressed
if (digitalRead(buttonPinClockwise) == LOW) {
// Rotate the motor clockwise continuously
myStepper.step(1); // Positive steps for clockwise rotation
}
// Check if the counterclockwise button is pressed
if (digitalRead(buttonPinCounterclockwise) == LOW) {
// Rotate the motor counterclockwise continuously
myStepper.step(-1); // Negative steps for counterclockwise rotation
}
}
how can I add a release function to help with the stepper motor so it doesn't vibrate.
#include <Stepper.h>
// Define the number of steps per revolution for your stepper motor
const int stepsPerRevolution = 200;
// Define the pins connected to the motor driver
const int motorPin1 = 8;
const int motorPin2 = 9;
const int motorPin3 = 10;
const int motorPin4 = 11;
// Define the pins connected to the buttons
const int buttonPinClockwise = 2; // Button for clockwise rotation
const int buttonPinCounterclockwise = 3; // Button for counterclockwise rotation
// Initialize the stepper motor object
Stepper myStepper(stepsPerRevolution, motorPin1, motorPin2, motorPin3, motorPin4);
// Variable to track if any button is pressed
bool anyButtonPressed = false;
void setup() {
// Set the buttons as inputs
pinMode(buttonPinClockwise, INPUT_PULLUP);
pinMode(buttonPinCounterclockwise, INPUT_PULLUP);
// Set the motor speed (you can adjust this value as needed)
myStepper.setSpeed(60); // RPM
}
void loop() {
// Check if the clockwise button is pressed
if (digitalRead(buttonPinClockwise) == LOW) {
// Rotate the motor clockwise continuously
myStepper.step(1); // Positive steps for clockwise rotation
anyButtonPressed = true; // Set the flag to true when any button is pressed
}
// Check if the counterclockwise button is pressed
if (digitalRead(buttonPinCounterclockwise) == LOW) {
// Rotate the motor counterclockwise continuously
myStepper.step(-1); // Negative steps for counterclockwise rotation
anyButtonPressed = true; // Set the flag to true when any button is pressed
}
// If no button is pressed, release the stepper motor
if (!anyButtonPressed) {
myStepper.step(0); // Stop the motor
}
// Reset the flag for the next loop iteration
anyButtonPressed = false;
}
Stepper motor-driver boards use holding power to keep the stepper "still" so the motor is always being "moved"
Verify the wire pairs from the motor-driver board are the same pairs at the motor.
At the motor-driver board, try swapping OUT1 with OUT2, or OUT3 with OUT4, or both.
And my worst suggestion is to put a controlled relay inline with the power wire and signal the relay to open the stepper motor-driver board power wire.
would it be better if I sent a more clear picture of my circuit board? I want to know why the stepper motor vibrates when no button is pressed. Only since it is not supposed to at all. The code is so that when one or the other button is pressed, it goes counterclockwise or clockwise, but it probably might be hardware but it doesn't seem to stop vibrating when neither is being pressed and I want to figure out a way to make it so it doesn't vibrate and maybe turns off and doesn't use power if you know what I mean.