Right way to step a stepper

Status
Not open for further replies.

tomfrisch

Well-known member
I'm making a device that moves a camera along a linear slider controlled by a stepper.

The step speed is affected by the amount of stuff the code does in the main loop before returning to the beginning when it sends another step. This stuff includes checking to see if one of the end stops have been hit, and checking to see if one of the 3 buttons has been pushed. If I get around to utilizing the accelstepper library, that would also add more overhead in there.

I don't need the maximum speed, but do need consistent speed while the processor is doing other things.

I don't need to check the buttons every cycle, but do need the end stops to reverse direction immediately on contact.

I'm sure there are 10 better ways to do this- maybe interrupts? maybe some sort of multi-threaded state machine?

Ideas?

-Tom
 
I'm sure there are 10 better ways to do this- maybe interrupts? maybe some sort of multi-threaded state machine?

You don't mention what you are using for control. If it is a Teensy3, then for making something occur with a fixed period you can use IntervalTimer. I didn't see a writeup on it yet on the web docs, but it's pretty straightforward:

Code:
IntervalTimer timer1;

void setup()
{
   ...
   timer1.begin(stepFunction, period); // replace "period" with interval in microseconds
   ...
}

void stepFunction(void)
{
   // do stuff
   
   // make it one-shot
   if(condition) 
      timer1.end();
   
   // make it change period
   else if(other condition) 
      timer1.begin(stepFunction, period2);
}
 
Yep, I'm using Teensy3 for this project.

That intervaltimer is pretty cool. Does it run concurrently with other tasks?

-Tom
 
Status
Not open for further replies.
Back
Top