New to Arduino need help slowing down servo movement

I have a working sketch for a trashcan using a one servo and a distance sensor so when my hand comes close the the sensor the servo activates but i would like to slowdown the movement of the servo so the lid of the trashcan doesn't go flying i have tried researching but i don't really understand it. Here is the sketch

#include <Servo.h>

const int trigPin = 13;   
const int echoPin = 11;     
const int servoPin = 12;     

Servo servo;                

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(servoPin, OUTPUT);

  servo.attach(servoPin);   
  servo.write(109);          
  Serial.begin(9600);       
}

void loop() {
 
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;  

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  
  

  if (distance < 30) {
    
    delay(200);
    servo.write(160);  
    delay(2500);     
    servo.write(90); 
    delay(2500);     
  }

  
  delay(100);
}

Welcome to the forum

Take a look at the Servo Sweep example in the IDE to see one way of doing it

Move the servo in 1 degree values.

Moving servo from 0 to 100 will require 100 individual movements.

A small delay is added after each 1 degree.

When you get to the desired angle, your servo is stopped.

Don’t use delay( ), used a non blocking TIMER based on millis( ).

There are libraries that help to control the speed of a servo or servos.

The servo class of the MobaTools library. MobaTools is available via the IDE library manager.

The VarSpeedServo library.

How to install an Arduino library.


Connecting an external power supply for a servo.

A 4AA battery pack is a good servo power supply. An Arduino is not a good power supply for an Arduino.

Servo power supply must provide for the stall current of the servo. Small servo 1A, bigger servos 2A or more. Consult the servo data sheet.

https://forum.arduino.cc/t/using-millis-for-timing-a-beginners-guide/483573

Credit where credits due Bob

1 Like

Non-blocking timing tutorials:
Blink without delay().
Blink without delay detailed explanation
Beginner's guide to millis().
Several things at a time.

I have a library for this named ServoSweep. Maybe that you find it usefull, maybe not who knows. You can find it in this git folder. You would need both the .cpp and .h file.

All you need to do is construct a servo sweep object with following parameters
ServoSweep::ServoSweep( uint8_t _servoPin, uint8_t _min, uint8_t _max, uint8_t _turnOff, uint8_t _speed )

example:
ServoSweep myMotor( pin, 30, 120, 1, 20) ;
Speed is the time in milliseconds that a motor shift one degree, so smaller speed is.. higher speed.. because logic :wink:

Min and max are the positions in degrees

Optionally you can let the motor to turn off when it reaches position. This can be usefull sometimes.

From void setup() you call myMotor.begin() ; to initialize the thing and from within loop() you have to call myMotor.sweep() ; continously.

With myMotor.setState( 0 ) ; // or 1 you can set the motor to either min or max positions.

Kind regards & good luck :coffee:

Bas

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.