Stepper motor with app inventor

Hello everyone, I am making an application to vary the speed of a stepper motor (nema 17) using the A4988 driver on a cnc shield, the idea is that through bluetooth and using a slider can vary the rpm of the motor that is set using the Accelstepper library, for this type of project many things can present problems, for the moment I can not get the motor to move, (the bluetooth is connected correctly to the phone), could you give me some idea of what could be failing

#include <SoftwareSerial.h>
#include <AccelStepper.h>

// Definimos los pines utilizados
const int pinStep = 4;
const int pinDir = 7;
const int pinEnable = 8;
// Creamos un objeto de tipo AccelStepper
AccelStepper stepper = AccelStepper(1, pinStep, pinDir);
SoftwareSerial BTSerial(10, 11); // Creamos un objeto SoftwareSerial para la comunicación Bluetooth
void setup() {
  // Configuramos los pines
  pinMode(pinEnable, OUTPUT);
  digitalWrite(pinEnable, LOW); // Habilitamos el driver
  
  // Configuramos el objeto AccelStepper
  stepper.setMaxSpeed(2000); // Velocidad máxima en RPM
 BTSerial.begin(38400); // Iniciamos la comunicación Bluetooth
}

void loop() {
  while (BTSerial.available() > 0) {
    int rpm = BTSerial.parseInt(); // Leemos el valor de RPM enviado desde la app
    const int dir = -1; // 1 para CW, -1 para CCW
  // Definimos la velocidad en RPM y la dirección
  if (BTSerial.read() == '\n') { // Si se recibió información por Bluetooth
    
    rpm = 600-constrain(rpm, 0, 600);
    // Configuramos la velocidad y la dirección del motor
    stepper.setSpeed(dir * rpm / 60 * 200); // Convertimos RPM a pasos por segundo
    stepper.runSpeed(); // Hacemos que el motor gire a la velocidad configurada
  }
  }
}



A DC motor is more suitable for that. Why a stepper motor suitable for positioning?

That calls for schematics.

This line of code must be called continuously, every time thru the loop() function as fast as possible. Make sure you don't add any delay() calls because that would interfere with the speed of the motor.

Isn't being call continuosly for being in the loop?

No. Not if it is in an if code block that doesn't evaluate to true.

I tried putting outside of the if and the outside of the while but ( the line stepper.runSpeed(); ) but the motor still does not run

In my opinion, something seems to be wrong here:

you´re sending an integer number followed by a comma, but your code expects a "\n" as the "end of transmission" mark:

My guess is that you have to put both codes to get along, being them with "," or with "\n".

1 Like

Hi, it does make the motor run and vary the speed thank you, but for some reason the motor stops when rpm is lower than 60, what do you think it can be?

Hi and also thanks to you, the combinations of both solutions gave a great advance

Glad to hear that!

I would check if your slider min and max values are set to 0-600 and would Serial.println() the values:

  1. of rpm after this operation:
  1. of the result of this operation:

to check if the values that you´re obtaining are what you expected.