Servo pulses per second

Hello, I am stuck on a seemingly simple problem.

I am trying to create a subroutine to control the rate of pulses per second for a servo.

If Rate = Distance / Time, so I'm trying to determine the rate.

let's say: Rate = 1000 pulses / 20 seconds
Rate = 50 pulses / second

But, now I can't figure out how to translate that into something workable. I'm figuring that I need to use millis() but I'm quite confused.

If I use 2 servos now, traveling different distances, but starting and ending at the same time, would that change the method? I considered using Delay(x) and Delay(y) for each servo, but that pauses the entire program, causing both servos to slow down.

Can anyone help me?
Thank you.
Brian

Servo speed isn't controlled by pulses per second, it's controlled by internal circuitry that tries to move it to the specified position (i.e. the position encoded by the pulse width) as quickly as possible. If you want to control servo speed, you need to build your own ramping function that sends changing position commands over time.

If you want to do two timing-based operations at once, you don't want to use delay(). Instead, you either want to use timer-driven interrupts (take a look at the MsTimer2 library) or you want to poll a timer and take action only when a certain period of time has elapsed. For example:

void loop()
{
if ((millis() % 7) == 0)
{
// take this action every 7 ms
}

if ((millis() % 13) == 0)
{
// take this action every 13 ms
}
}

A more standard construction would be to use a variable to store a start time, and then do something like

if (millis() - startTime >= DURATION_IN_MS_TO_BE_TIMED)
{
// take your action
startTime = millis();
}

Because these timing functions don't block CPU execution like delay(), you can do as many as you want in parallel.

I can probably give you a pseudocode solution to your servo problem if this post doesn't clear things up for you.

  • Ben

Thanks for your help on this. Sorry for the long delay in my thanks. I had to shelve that project for awhile. Just got back into it and it all makes sense now!
Brian