Is there a better way to use various speeds for a stepper motor

Hi there,

first of all, i'm using a NEMA-23 bipolar stepper motor in combination with a TB6600 motor driver and an Arduino UNO.
I couldn't get them hooked up until now, because the 12 V power supply needed for the driver and motor isn't ready yet. But well, it's not needed for now.

As far as i understand, the motor driver just needs a 5V HIGH signal and a LOW signal from the Arduino to issue the next step (of course some more pins are needed for direction and enable).

So if i want to control the speed of the stepper, i have to vary the delay time between each HIGH and LOW signal sending from the Arduino right? So that means:

void loop() {
  for (int i=0; i<6400; i++)    
  {
    digitalWrite(DIR,LOW);
    digitalWrite(ENA,HIGH);
    digitalWrite(PUL,HIGH);
    delayMicroseconds(50);
    digitalWrite(PUL,LOW);
    delayMicroseconds(50);
  }
}

Should be slower than this:

void loop() {
  for (int i=0; i<6400; i++)    
  {
    digitalWrite(DIR,LOW);
    digitalWrite(ENA,HIGH);
    digitalWrite(PUL,HIGH);
    delayMicroseconds(20);
    digitalWrite(PUL,LOW);
    delayMicroseconds(20);
  }
}

right?

I assume everything i said until now is right. So let me first explain for what reason i need the stepper.

I'm currently on my bachelor thesis project and part of it is to create a python script, which students can use to control a stepper motor during lab time. The stepper is hooked up on a motion converter (sorry, english is not my native language) which converts the rotation to a translation. So rotating clockwise moves an object forward, rotating counter-clockwise moves it backward.

My goal is that the stepper moves the object in a "-cos(x) + 1" way.

What i mean is: it starts slow, gets faster and faster, until the pi/2 points, and gets slower and slower until it reaches the local maximum (which means stop moving) and then repeating the same thing backwards.

Because the stepper needs 200 steps for 1 revolution, and 3 revolutions are needed for going all the way forward (can't change that, it has to be 3 revolutions), assuming i use half step mode, i would need freaking 1200 different delay times for my Arduino script. Even as bytes, this would need a major portion of the RAM. And half step mode isn't really the best for a smooth motion right? (as i said before, couldn't test it for now, just assuming). So microstep is out of question if i do it this way.

My question is, is there a better way to achieve the same, or nearly the same result?

The way the speed is controlled will be easier to understand if you have a variable for the width of the pulse (10 microsecs is usually sufficient) and another variable for the interval between pulses. It is the latter that is used to vary the speed. See this Simple Stepper Code

You say "My goal is that the stepper moves the object in a "-cos(x) + 1" way. " but I wonder if all that is really needed is to accelerate the motor from standstill and decelerate it to a stop? The AccelStepper library was written to facilitate that. You just give it the maximum speed, the acceleration rate and the number of steps to move.

If you want to consider writing your own acceleration code this simple acceleration code may give you some ideas

...R

This is really an exercise in data compression, encode a series of step times in a compact
manner to download over serial?

I'd suggest using quadratic model of the motion, breaking it down into segments as needed
(a parabola could be all one instruction, sinusoid would have to be broken up into several
elements for a given error tolerance).

The arduino just has to run a simple decoding engine and store a buffer of quadratic 'instructions',
the encoding would be heavy lifting in numpy/scipy. There's probably already a function for it.