Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用arduino Mega控制伺服电机的速度_Arduino - Fatal编程技术网

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

如何使用arduino Mega控制伺服电机的速度,arduino,Arduino,我正在做一个项目,我需要改变伺服电机的速度。我使用的硬件是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 } s; s、 附(

我正在做一个项目,我需要改变伺服电机的速度。我使用的硬件是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
}
s;
s、 附(9);

对于伺服库中的(int i=0;i,WriteMicroseconds(…)函数设置伺服速度


有关更多信息,请

如果它们都是相同的学位,请尝试下面的代码。

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

#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;
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);
}
您需要更改pin码

只要把它放在任何地方就行了(不要放在任何“{}”之间)

#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;
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);
}
void转向伺服系统(内到外){
伺服卫星=S1.read;
如果(伺服卫星==toDegree){
}
如果(伺服卫星>toDegree){
而(S1.read>toDegree){
int currentToDegree=S1.read-1;
S1.写入(currentToDegree);
S2.写入(currentToDegree);
S3.写入(currentToDegree);
S4.写入(currentToDegree);
S5.写入(currentToDegree);
S6.写入(currentToDegree);
S7.写入(currentToDegree);
S8.写入(currentToDegree);
S9.写入(currentToDegree);
S10.写入(currentToDegree);
S11.写入(currentToDegree);
S12.写入(currentToDegree);
延迟(10);//调整此值使其更快或更慢。
}
}
如果(伺服卫星

如何使用此选项:
setup
中,在您对伺服系统进行任何操作之前,但在我告诉您要进行设置的部分之后,请使用
ClearServos();
准备要使用的伺服系统。(这可能不是必须的,但我不知道当你使用
S1时会发生什么。在不改变它的情况下阅读
,如果伺服在不同的位置,它会解决问题。如果不会引起问题,它是可以避免的,但我认为如果可以,你应该使用它。)所有这些都会转向90度。(可以使用
void ClearServos
中的变量
startDegree
更改90度)

要转动它们,请使用旋转伺服(90);
。90是您希望它转动的程度



我没有测试过这个,因为我没有一个Mega或12伺服。如果你发现任何错误,请发表评论,因为这是巨大的。我花了很多时间在这方面,所以我希望我能帮助你。:)

也许你可以在伺服电机之前,在伺服电机的VCC引脚上串联一些电阻,以降低其两端的电压;从而降低其速度。然而,这将导致伺服电机的速度“恒定”


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

延迟微秒(值)对于我的timelapse装备上的360伺服系统,最接近90 null是最慢的,包括平移和托架,使用机械快门点击器(迷你标准伺服系统)及时拍摄移动拍摄。

我必须同时控制12个伺服电机,loop将不允许我这样做……我使用的是标准伺服系统,'servo.writeMicroseconds()'将改变轴的位置,而不是速度!