繁体   English   中英

如何使用Arduino Mega控制伺服电机的速度

[英]How to control speed of a Servo motor using arduino Mega

我正在做一个需要更改伺服电机速度的项目。 我使用的硬件是Arduino Mega 2560开发板,并且我正在使用Servo.h库来控制伺服器。 伺服从o旋转到180度。 我在项目中使用12台伺服电机,必须同时控制它们。 有办法吗?

您可以在一段时间或for循环中使用delay()函数

例:

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

如果它们都是同一学位,请尝试以下代码。

在最顶部 (不在任何“ {}”之间)

#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;

把它放在 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);

您需要更改引脚号。

只需将其放在任何地方 (不在任何“ {}”之间)

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);
}

使用方法:setup ,对伺服器进行任何操作之前,但在我告诉您安装程序的部分之后 ,请使用ClearServos(); 准备要使用的伺服器。 (这可能不一定,但是我不知道使用S1.read而不更改它会发生什么,并且如果伺服器位于不同的位置,它将解决问题。如果不会引起问题,则可以避免。 ,但我认为如果可以的话,应该使用它。)所有这些都将变成90度。 (可以使用void ClearServos的变量startDegree更改90度。)

要转动它们,请使用TurnServos(90); 90是您想要的度数。


由于我没有Mega或12个伺服器,因此尚未测试。 如果发现任何错误,请发表评论,因为这是巨大的。 我花了很多时间在此上,希望能有所帮助。 :)

也许您可以在伺服电机的VCC引脚上串联一些电阻,以降低跨接的电压。 因此减慢了速度。 但是,这将使您的伺服器保持“恒定”速度。

一种替代方法是在您的伺服VCC连接之间放置一个晶体管,并在基本引脚上设置PWM来调节电流(以调节速度),但是如果您之间不使用多路复用器,则每个伺服器会花费一个额外的引脚。 并使您的设计更加复杂。

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

伺服库中的WriteMicroseconds(...)函数可设置伺服速度。

有关更多信息,请单击

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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