Stepper Motor with AccelStepper

Hello, I’m controlling a stepmotor and the code goes like this: If I send 1, it rolls clockwise for few centimeters. If I send 2, counter-clockwise for the same length. If 3, stop the motor wherever it is.
But, for some reason, if I dont send 3, after it has complete the length in 1 or 2, even if it has complety stoped, I can hear a noise, like it’s still on course, and then when I send, for exemple, 2 after completed the lenght of 1, it makes a wierd noise like it’s struggling with itself.
I tryed to put a “send 3” line in the last line, but the code works in loop until it has runned the length. Also tryed to put a daley(7000) line, but again it’s in loop and makes a single step every 7sec.

Any idea how can it be fixed?

By the way, I’m using a Nema17 and AccelStepper library.

Here’s the code:

#include <AccelStepper.h>

int velocidade_motor = 2400;
int aceleracao_motor = 1000;
int sentido_horario = 0;
int sentido_antihorario = 0;
int numero = 0;

// Definicao pino ENABLE
int pino_enable = 10;

// Definicao pinos STEP e DIR
AccelStepper motor1(1,7,4 );

void setup()
{
Serial.begin(9600);
pinMode(pino_enable, OUTPUT);
// Configuracoes iniciais motor de passo
motor1.setMaxSpeed(velocidade_motor);
motor1.setSpeed(velocidade_motor);
motor1.setAcceleration(aceleracao_motor);

Serial.println(“Digite 1, 2 ou 3 e clique em ENVIAR…”);
}

void loop()
{
// Aguarda os caracteres no serial monitor
if (Serial.available() > 0)
{
numero = Serial.read();
{
if (numero == ‘1’)
{
Serial.println(“Numero 1 recebido - Girando motor sentido horario.”);
digitalWrite(pino_enable, LOW);
sentido_horario = 1;
sentido_antihorario = 0;
}

if (numero == ‘2’)
{
Serial.println(“Numero 2 recebido - Girando motor sentido anti-horario.”);
digitalWrite(pino_enable, LOW);
sentido_horario = 0;
sentido_antihorario = 1;
}

if (numero == ‘3’)
{
Serial.println(“Numero 3 recebido - Parando motor…”);
sentido_horario = 0;
sentido_antihorario = 0;
motor1.moveTo(0);
digitalWrite(pino_enable, HIGH);
}
}
}

// Move o motor no sentido horario
if (sentido_horario == 1)
{
motor1.moveTo(9000);
}
// Move o motor no sentido anti-horario
if (sentido_antihorario == 1)
{
motor1.moveTo(-9000);
}

// Comando para acionar o motor no sentido especificado
motor1.run();
}

Your code seems reasonable. Do you have a spec of the driver you're using and the power supply? Maybe a wiring diagram would help.

Post a link to the datasheet for your stepper motor and tell us what stepper motor driver you are using and what stepper motor power supply you have (volts and amps).

If the motor is moving correctly and is not overheating (being so hot that it is uncomfortable to touch is normal) I would not be inclined to worry about the noise.

If it is too hot then that probably means that you have not correctly adjusted the current limit on the stepper driver

...R Stepper Motor Basics Simple Stepper Code