Problem with the code to control a step motor via serial port (LabVIEW).

Good afternoon.

I created a code in the arduino IDE to control the speed, the number of steps and the direction of a stepper motor. But the code is not working. I would like some help to solve the problem or some example that I could use.

The code is as follows:

int pino_passo = 9;
int pino_direcao = 8;
String readString;
String ent;
int j, posArroba, posCifrao, velocidade, numero_de_passos, expoente, expoente2;

int direcao = 1;
int passos_motor = 1600;

void setup(){
pinMode (pino_passo, OUTPUT);
pinMode (pino_direcao, OUTPUT);
Serial.begin(9600);
}

void loop()
{
while (Serial.available())
{
delay(30);
ent = Serial.readString();
//readString += c;
}

if (ent.length() > 0)
{
//Serial.println(readString);
//#D%100@33$
if (ent.charAt(1) == "#")
{
if(ent.charAt(2) == "D")
{
direcao = 1;
j = 2;
}
else if (ent.charAt(2) == "E")
{
direcao = 0;
j = 2;
}

while (ent.charAt(j) != "@")
{
j++;
}

posArroba = j;

while (ent.charAt(j) != "$")
{
j++;
}
posCifrao = j;

expoente = 0;
numero_de_passos = 0;
for (int p= posArroba -1 ; p >= 3 ; p=p-1)
{
numero_de_passos = ent.charAt(p)*10^expoente+numero_de_passos;
expoente++;
}

expoente2 = 0;
velocidade = 0;
for (int p= posCifrao - 1 ; p >= posArroba + 1 ; p=p-1)
{
velocidade = ent.charAt(p)*10^expoente2 + velocidade;
expoente2++;
}
}
float usDelay = (1/(velocidade/100)) * 70;
// Define a direcao de rotacao

digitalWrite(pino_direcao, direcao);
for (int p=0 ; p <= numero_de_passos; p++)
{
digitalWrite(pino_passo, 1);
delayMicroseconds(usDelay);
digitalWrite(pino_passo, 0);
delayMicroseconds(usDelay);
}
}
}

The program I use to set the parameters is LabVIEW, from where I send a string to the Arduino.

Thanks,

Lucas

"But the code is not working."

Does it compile ok? For which Arduino? What does it do, or not do, different from what you are expecting?

The code does not display any errors, compiles. I'm using the Arduino Uno. The stepper motor rotates, but only for one direction, only one speed and no set number of steps.

//Serial.println(readString);

The only print to the serial monitor is commented out, so not surprised no errors show up when running.

Stepper motors use a lot of current and need a motor driver. What are you using, please post a schematic of your setup.

                    float usDelay = (1/(velocidade/100)) * 70;
                    // Define a direcao de rotacao
                    
                    digitalWrite(pino_direcao, direcao);
                    for (int p=0 ; p <= numero_de_passos; p++)
                    {
                        digitalWrite(pino_passo, 1);
                        delayMicroseconds(usDelay); 
                        digitalWrite(pino_passo, 0);
                        delayMicroseconds(usDelay); 
                    }

delayMicroseconds() expects a whole number from 0 to 2^32-1 (or 0 to 0xFFFFFFFF).
float will create a number like 32.123456, I don't know what delayMicroseconds() will do with that.

I replaced the float with the int but it still has the same problem.

I'm using the easy driver to control the stepper motor.

Do you think it's possible to make this code work or should I try to make another one? Would you have a similar example?

What is the value of usDelay and numero_de_passos is being used here? Too small of a number and the motor won't have time to react.
digitalWrite(pino_passo, 1);
delayMicroseconds(usDelay);
digitalWrite(pino_passo, 0);
delayMicroseconds(usDelay);