PeterH:
No, that's not really what I had in mind - I was thinking of something more like this:int actualSpeed = 0; // the speed the motor is currently running at
int targetSpeed = 0; // the speed we want the motor to be running at
unsigned long lastSpeedUpdateTime = 0;
const unsigned long SPEED_UPDATE_INTERVAL = 10; // ms
void setSpeed(int requiredSpeed)
{
targetSpeed = requiredSpeed;
}
void handleAcceleration()
{
unsigned long now = millis();
if(now - lastSpeedUpdateTime > SPEED_UPDATE_INTERVAL)
{
// time to check the motor speed again
if(actualSpeed < targetSpeed)
{
// accelerating
actualSpeed++;
myservoThrottle.write(actualSpeed);
}
else if(actualSpeed > targetSpeed)
{
// deccelerating
actualSpeed--;
myservoThrottle.write(actualSpeed);
}
else
{
// motor is already at the target speed - no action required
}
lastSpeedUpdateTime = now;
}
}
void loop()
{
handleAcceleration();
... etc
}
Somewhere in your code you would call setSpeed() to set the required motor speed - you only need to do that when you want the speed to change. Then loop() calls handleAcceleration() repeatedly, and that accelerates the motors smoothly towards the target speed.
Thanks for steering me in the right direction Peter,
I had a question on 'setSpeed'
This seems to be a command for a stepper motor: Stepper: setSpeed(rpms)
Should I change this to something like: speedSet() ?
t