Problem mantaining stepper motor speed

I'm running a stepper motor with easydriver. I'm using a potentiometer and analogRead to set the delay between the steps so i can control the speed.

Below is the code i'm using right now (sorry, there's some stuff in portuguese but i'll explain it)

I'm trying to set a speed for analogRead values between 0 and 250, another speed for values between 250 and 500, and so forth.

It's all running smoothly except for when it reads below 100 or above 1000. What happens is that it runs a little bit faster when its <100 than when it's between 100 and 250, and a little bit slower when its over 1000 than when its between 750 and 1000.

What I suspect is that it has to do with the number of bits being read. I.e, the code runs a little bit faster when it reads only 2 numbers and a little bit slower when it reads 4. Is that a possibility? Any ideas on how to get around this?

     //ler potenciômetro e determinar velocidade
    valorpotenciometro = analogRead (potenciometro);

       if (valorpotenciometro<250){
         delayvel = 5000; //velocidade rapida
         digitalWrite(m1, HIGH); //microstep 1/8 de passo
         digitalWrite(m2, HIGH);
         Serial.println (valorpotenciometro);
         retorno =0;

        }else if(valorpotenciometro >250 & valorpotenciometro<500){
          delayvel = 10000; // velocidade media
          digitalWrite(m1, HIGH);
         digitalWrite(m2, HIGH);
         Serial.println (valorpotenciometro);
         retorno = 0;

        }else if(valorpotenciometro>500 & valorpotenciometro<750){
          delayvel = 20000; // velocidade lenta
          digitalWrite(m1, HIGH);
          digitalWrite(m2, HIGH);
         Serial.println (valorpotenciometro);
         retorno = 0;

        }else if (valorpotenciometro>750){
          delayvel = 2;
          digitalWrite (m1, LOW); //microstep meio passo
          digitalWrite (m2, LOW);
          Serial.println (valorpotenciometro);
          retorno = 1;

        }

That code isn't useful out of context.

rhasenack:
What I suspect is that it has to do with the number of bits being read. I.e, the code runs a little bit faster when it reads only 2 numbers and a little bit slower when it reads 4. Is that a possibility?

You need to post a complete program so we can see how the variables are defined etc.

...R

Full code below

//definição dos pinos para cada função
#define m1 8
#define m2 10
#define dir 2
#define passo 3
#define chaveDir 4


  //introdução de variaveis usadas no loop
  int mudaDirecao = 0;
  int valorpotenciometro = 0;
  unsigned long vel = 10000;
  unsigned long tempo = 0;
  int PulseStatus = 0;
 int delayvel= 10000;
int retorno = 1;
//potenciometro entrada analogica

#define potenciometro A5

void setup() {
  Serial.begin(19200);
  //declaração de modo dos pinos
  pinMode(m1, OUTPUT);
  pinMode(m2, OUTPUT);
  pinMode(dir, OUTPUT);
  pinMode(passo, OUTPUT);
  pinMode(chaveDir, INPUT);
  pinMode(potenciometro, INPUT);
  
  digitalWrite (dir, HIGH); //direção inicial
  digitalWrite(m1, LOW);
  digitalWrite(m2, LOW);


}

void loop() {
    //verificar posicionamento da chave de direção
  mudaDirecao= digitalRead(chaveDir);

   if (mudaDirecao == HIGH) {
      digitalWrite (dir, HIGH);
    }
   else{
      digitalWrite (dir, LOW);}

     //ler potenciômetro e determinar velocidade
    valorpotenciometro = analogRead (potenciometro);

       if (valorpotenciometro<250){
         delayvel = 5000; //velocidade rapida
         digitalWrite(m1, HIGH); //microstep 1/8 de passo
         digitalWrite(m2, HIGH);
         Serial.println (valorpotenciometro);
         retorno =0;
        }else if(valorpotenciometro >250 & valorpotenciometro<500){
          delayvel = 10000; // velocidade media
          digitalWrite(m1, HIGH);
         digitalWrite(m2, HIGH);
         Serial.println (valorpotenciometro);
         retorno = 0;
        }else if(valorpotenciometro>500 & valorpotenciometro<750){
          delayvel = 20000; // velocidade lenta
          digitalWrite(m1, HIGH);
          digitalWrite(m2, HIGH);
         Serial.println (valorpotenciometro);
         retorno = 0;
        }else if (valorpotenciometro>750){
          delayvel = 2;
          digitalWrite (m1, LOW); //microstep meio passo
          digitalWrite (m2, LOW);
          Serial.println (valorpotenciometro);
          retorno = 1;
        }
       
     //codigo para rodar os steps
     
 if (micros() > tempo & retorno == 0) {
    digitalWrite(passo, HIGH);  // give high pulse
    tempo += delayvel;         // reset for next pulse
    PulseStatus = 1;               // update pulse status
  }
  if ( PulseStatus & micros() > tempo - (delayvel / 2) & retorno == 0) { // after half the period make low pulse
    digitalWrite(passo, LOW);
    PulseStatus = 0;            // update pulse status
  }
  if (retorno == 1){
    digitalWrite (passo, HIGH);
    delay(1);
    digitalWrite (passo, LOW);
    delay (1);
  }
  

}

Have you tried changing:

  Serial.begin(19200);

to

  Serial.begin(115200);

?

Your code for causing a step seems very complex. Have a look at the code in the 2nd example in this Simple Stepper Code. To change the speed all that is needed is to change the value of millisBetweenSteps. If you need higher speeds you could change that to microsBetweenSteps.

Checking time with millis() and micros() should always use subtraction so as not to be affected when the value rolls-over.

...R
Stepper Motor Basics

MarkT:
Have you tried changing:

  Serial.begin(19200);

to

  Serial.begin(115200);

?

Just did, it worked. Thanks!

Robin2:
Your code for causing a step seems very complex. Have a look at the code in the 2nd example in this Simple Stepper Code. To change the speed all that is needed is to change the value of millisBetweenSteps. If you need higher speeds you could change that to microsBetweenSteps.

Checking time with millis() and micros() should always use subtraction so as not to be affected when the value rolls-over.

...R
Stepper Motor Basics

I'll try to make it simpler. I added some steps to try to fix another issues i was having. Thanks!