thomas3120:
Or something along these lines...void moveForward()
{
interval =50;
targetSpeed =1700;
if(targetSpeed - actualSpeed > interval)
{
actualSpeed = targetSpeed;
myservoThrottle.write(targetSpeed);
}
}
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.