Program to slow the speed of a servomotor

Hi,

I want to program my servomotor. All he needs to do, is to turn 180° stay there for a while and turn back. The problem is, that the motor does this too fast. Can i slow him somehow down? Or probably someone knows how to programm a ramp. So the motor starts slow and gets faster over time, that would be perfect.

That’s my code so far:

#include <Servo.h>  
Servo myServo;

int pos = 0;     

void setup() {
  myServo.attach(9); 
  myServo.write(pos);  
  delay(2000);
}

void loop() {
 myServo.write(0);
 delay(300);
 myServo.write(179);
 delay(300);
}

Try this:

#include <Servo.h>  
Servo myServo;

int pos = 0;
unsigned long currentTime = millis();
unsigned long previousTime = currentTime;
unsigned long delayTime = 100;

void setup() 
{
  myServo.attach(9); 
  myServo.write(pos);
}

void loop() 
{
   currentTime = millis();
   if((currentTime - previousTime) >= delayTime)
   {
      previousTime = currentTime;
      if(pos <= 180)
      {
         myServo.write(pos);
         pos = pos + 1;
      }
      else
      {
         pos = 0;
         myServo.write(pos);
      }
   }
}

Or there is an alternative servo library which allows you to set the speed. If you use VarSpeedServo.h instead of Servo.h you can just change Servo.myservo to VarSpeedServo.myservo then myservo.write(pos) to myservo.write(pos, speed) where speed can vary from 1 (very very slow) to 255 (fast).

See GitHub - netlabtoolkit/VarSpeedServo: Arduino library for servos that extends the standard servo.h library with the ability to set speed, and wait for position to complete

Steve

slipstick: Or there is an alternative servo library which allows you to set the speed. If you use VarSpeedServo.h instead of Servo.h you can just change Servo.myservo to VarSpeedServo.myservo then myservo.write(pos) to myservo.write(pos, speed) where speed can vary from 1 (very very slow) to 255 (fast).

See https://github.com/netlabtoolkit/VarSpeedServo

Steve

Sounds like a band-aid solution to an easy problem. The code in my first reply is more flexible and isn't difficult to understand/implement.