[solved] How to add release function to my coding

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.

Post a schematic of your project.

The step motor should not vibrate.

I'm pretty new at this and I was wondering if you could help me get you the schematic of my project.

Show a photo of your assembly.

You can modify your code like this:

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

please read the how to get the best out of the forum thread. Then use the IDE autoformat tool to format your code before posting in a code block.

Post a schematic. If the driver has an enable pin, perhaps a button connected to it will do what you want.

As it is, we know little of your circuit so it is hard to give advice,

there you go i edited my code so you can test it out.

Confirm that your project looks like this image.

If so, you can disable the motor using the ENA-A and ENA-B pins.

But I still maintain that under normal conditions your engine should not be vibrating.

thats exactly right. how would i use ena-a and ena-b pins to disable my motor?

ill make sure to try this

my stepper motor still seems to vibrate when no button is pressed.

Did any suggestion from January help or hinder?

It didn't help but I found ways around it. my only problem now is how the stepper motor still vibrates when no button is pressed.

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.

@ruilviana sent this photo that really explains my schematic of my board should I not be using resistors?

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.

this is a prior sketch I had made for it a couple of months back.

One leg of your button should be connected to GND, the other to internally pulled up input pin, no external resistors.

the vin pin is getting 7.2v from a battery source and also sorry for the late reply.