Low Rpm when analogRead is called

I'm trying to make a code that is supposed to control the velocity of the pulses that generates and goes to the motor driver.

The problem is that in this code it spins really slow:

int PUL 3;
int Poten = 0;
int Velocity = 0;

void setup()
{
//Setup Stepper Motor
pinMode(PUL,OUTPUT);
digitalWrite(PUL, LOW);

}

void loop()
{

Velocity = analogRead(Poten);
Velocity = map(Velocity, 0, 1023, 1, 1024);

digitalWrite(PUL, HIGH);
delayMicroseconds(1024/Velocity);
digitalWrite(PUL, LOW);
delayMicroseconds(1024/Velocity);

}

But when I delete the 2 lines of analog reading and mapping and upload it to the arduino uno the motor spins much faster. I can't get to know what is the problem here, I don't think the analog takes that long to read and map. Or does it?

Thanks in advance.

Your map() command is doing nothing useful, and it is a slow function. Just add 1 like this

Velocity += 1;

Also division is very slow so don't use it - and certainly don't do the same calculation twice.

The control of a stepper motor will make more sense like this

      digitalWrite(PUL, HIGH);
      delayMicroseconds(pulseWidth);
      digitalWrite(PUL, LOW);
      delayMicroseconds(intervalBetweenPulses);

The pulse width should be constant - and something about 10 microsecs is sufficient. Then you can control the speed by changing the intervalBetweenPulses.

Have a look at the second example in this Simple Stepper Code which uses millis() and micros() for non-blocking timing. That allows the analogRead() to take place in the time between steps without impacting on the interval between steps.

...R
Stepper Motor Basics

I've been having a look at you posts and I have read them many times, but I can't get to understand how to put the analogRead to "non-block the timing". Could you help me?

Hcax2002:
I've been having a look at you posts and I have read them many times, but I can't get to understand how to put the analogRead to "non-block the timing". Could you help me?

You don't need to do anything with analogRead(). But you should use non-blocking timing for the motor - as in my demo code.

How many steps per second do you want the motor to move?

...R

That's the point, I want to have a potentiometer to regulate the velocity.

Hcax2002:
That's the point, I want to have a potentiometer to regulate the velocity.

You have to tell me the maximum number of steps per second if I am to help. Also tell me the minimum - though that is unlikely to be an impediment.

...R

Code like this using delays:

      digitalWrite(PUL, HIGH);
      delayMicroseconds(1024/Velocity);
      digitalWrite(PUL, LOW);
      delayMicroseconds(1024/Velocity);

Is never accurate as is cannot account for time taken elsewhere in the code.

Its often better to synchronize precisely to the clock using micros() or millis():

unsigned long last_pulse_time = 0L ;

void loop()
{
  long delay = 2048L / Velocity ;  // get the current amount to delay, note long constant required to force long division
  if (micros() - last_pulse_time >= delay)  // detect pulse is due
  {
    last_pulse_time += delay ;   // set last_pulse_time without drift

    digitalWrite (PUL, HIGH);  // output a high-going pulse of several us
    delayMicroseconds (5);    // this delay isn't going to affect the timing of next pulse
    digitalWrite (PUL, LOW);
  }
  .... other stuff ....
}

Here this means the (approx 110us) delays caused by executing analogRead() can only create jitter in the step pulses, not actually affect them beyond that (unless you get to 100% CPU usage, when all bets are off).

So the code would look like this?

#define pulseWidth 10


int PUL = 3;          
int Poten = 0;
int Velocity = 0;
int intervalBetweenPulses = 0;

void setup()
{
  //Setup Stepper Motor
 pinMode(PUL,OUTPUT);  
 digitalWrite(PUL, LOW); 

}
unsigned long last_pulse_time = 0L ;
void loop()
{
  Velocity = analogRead(Poten);
  Velocity += 1;
  long delay = 1024L / Velocity ;  // get the current amount to delay, note long constant required to force long division
  if (micros() - last_pulse_time >= delay)  // detect pulse is due
  {
    last_pulse_time += delay ;   // set last_pulse_time without drift

    digitalWrite (PUL, HIGH);  // output a high-going pulse of several us
    delayMicroseconds (5);    // this delay isn't going to affect the timing of next pulse
    digitalWrite (PUL, LOW);
  }
      
}

Thanks for both answers, they made me know more about these motors. Still, I want to make it spin faster.
Could it go faster if I made another void that gets triggered by a button and putting the analogRead there?
The original project is to measure(just by calculating the steps) and regule the velocity of the motor.

Hcax2002:
Still, I want to make it spin faster.

How fast ? For the 3rd time IIRC.

...R

Hcax2002:
Thanks for both answers, they made me know more about these motors. Still, I want to make it spin faster.
Could it go faster if I made another void that gets triggered by a button and putting the analogRead there?
The original project is to measure(just by calculating the steps) and regule the velocity of the motor.

Its a function with no return type, "void" is a type declation meaning no value.

If the analogRead doesn't happen everytime round the loop you ought to be able to get
faster overall rate, but the jitter issue cannot be fixed unless some sort of interrupt-based
mechanism is used, or you roll-your-own ADC reading code that is non-blocking.

You might want to loop at AccelStepper library as it drives steppers like this but hides the
details and supports velocity ramping.