简体   繁体   中英

How to control speed of a Servo motor using arduino Mega

I am working on a project in which I need to change the speed of servo motors. The hardware I am using is an Arduino Mega 2560 board and I am using Servo.h library to control servos. Servo rotates from o to 180 degree. I am using 12 servo motors in the project and have to control them simultaneously. Is there a way?

You can use delay() function in a while or for loop

Example:

Servo s;
s.attach(9);
for(int i=0 ; i<180 ; i++)
{
    s.write(i);
    delay(10); //10 milisecond
}

If they all are the same degree, try this code below.

At the very top (Not between any "{}"s) :

#include <Servo.h> 
Servo S1;
Servo S2;
Servo S3;
Servo S4;
Servo S5;
Servo S6;
Servo S7;
Servo S8;
Servo S9;
Servo S10;
Servo S11;
Servo S12;

Put this in Setup :

S1.attach(1);
S2.attach(2);
S3.attach(3);
S4.attach(4);
S5.attach(5);
S6.attach(6);
S7.attach(7);
S8.attach(8);
S9.attach(9);
S10.attach(10);
S11.attach(11);
S12.attach(12);

You need to change the pin numbers.

Just put this anywhere (Not between any "{}"s) :

void TurnServos(int toDegree){
  servosAt = S1.read;
  if(servosAt == toDegree){

  }
  if(servosAt > toDegree){
    while(S1.read > toDegree){
      int currentToDegree = S1.read - 1;
      S1.write(currentToDegree);
      S2.write(currentToDegree);
      S3.write(currentToDegree);
      S4.write(currentToDegree);
      S5.write(currentToDegree);
      S6.write(currentToDegree);
      S7.write(currentToDegree);
      S8.write(currentToDegree);
      S9.write(currentToDegree);
      S10.write(currentToDegree);
      S11.write(currentToDegree);
      S12.write(currentToDegree);
      delay(10);         //Adjust this to make it faster or slower.
    }
  }
  if(servosAt < toDegree){
    while(S1.read < toDegree){
      int currentToDegree = S1.read + 1;
      S1.write(currentToDegree);
      S2.write(currentToDegree);
      S3.write(currentToDegree);
      S4.write(currentToDegree);
      S5.write(currentToDegree);
      S6.write(currentToDegree);
      S7.write(currentToDegree);
      S8.write(currentToDegree);
      S9.write(currentToDegree);
      S10.write(currentToDegree);
      S11.write(currentToDegree);
      S12.write(currentToDegree);
      delay(10);         //Adjust this to make it faster or slower.
    }
  }
}
void ClearServos(){
  int startDegree = 90;   //Change this number to anything you want.
  S1.write(startDegree);
  S2.write(startDegree);
  S3.write(startDegree);
  S4.write(startDegree);
  S5.write(startDegree);
  S6.write(startDegree);
  S7.write(startDegree);
  S8.write(startDegree);
  S9.write(startDegree);
  S10.write(startDegree);
  S11.write(startDegree);
  S12.write(startDegree);
}

How to use this: In setup before you do anything with servos but after the part I told you to put in setup , use ClearServos(); to prepare the servos to be used. (This probably isn't necessarily, but I don't know what happens when you use S1.read without changing it and if the servos are at different positions, it will fix problems. It can be avoided if it won't cause problems, but I think you should use it if you can.) All of them will turn to 90 degrees. (90 degrees can be changed with the variable startDegree in void ClearServos .)

To turn them, use TurnServos(90); . 90 is the degree you want it to turn to.


Haven't tested this because I don't have a Mega or 12 servos. Please comment if you notice any errors since this is huge. I spent a lot of time on this so I hope I helped. :)

Maybe you can put some resistors in series to your servo's VCC pin, before your servo motor to decrease the voltage across; thus slowing it. However, this will cause your servo's to be "constant" speed.

An alternative could be to put a transistor in between your servo VCC connection and set PWM on base pin to regulate current (to regulate speed), but that would cost you an extra pin per servo if you're not using a multiplexer in between; and could make your design a little more complicated.

对于我的缩时摄影机上的平移和滑移,移动射击,360度伺服,最接近90-null的delayMicroseconds(value)最慢,要及时使用机械快门答题器(微型标准伺服)。

in Servo library WriteMicroseconds(...) function sets servo speed.

for more information please click

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM