查看: 2938  |  回复: 0
arduino 并行控制多个舵机程序
小小宅138
13
主题
42
回复
发表于2018-10-24 14:56:42 | 只看该作者
1# 电梯直达

用扫描的思想,依次对多个舵机进行控制,当速度足够快就可以认为是同时控制的!

下面是一些程序,可供参考

#include <Servo.h>

Servo servo1; // 建立Servo实例
Servo servo2;
const int servo1Pin = 8;  // servo1 接 Pin 8
const int servo2Pin = 9;  // servo2 接 Pin 9
const int every1 = 15;
const int every2 = 15;
int servo1Target = 0;
int servo2Target = 0;
int servo1Dir = 10;
int servo2Dir = 1;
int posOfServo1 = 90;
int posOfServo2 = 90;
void setup() {
  servo1.attach(servo1Pin, 500, 2500); // pin8,
  servo2.attach(servo2Pin, 500, 2500); // pin9
  Serial.begin(9600);
  servo1.write(posOfServo1);
  servo2.write(posOfServo2);
  Serial.println("2 Servo test");
  delay(500);
}
void loop( ) {
  checkServo1();
  checkServo2();
  if (posOfServo1 > 179)
    servo1Target = 0;
  if (posOfServo1 < 1 )
    servo1Target = 180;
  if (posOfServo2 > 179)
    servo2Target = 0;
  if (posOfServo2 < 1 )
    servo2Target = 180;
}

void checkServo1( ) {
  static uint32_t Timer;
  if (Timer > millis())
    return;
  Timer = millis() + every1;
  if (servo1Target > posOfServo1) {
    posOfServo1 += servo1Dir;
  } else if (servo1Target == posOfServo1) {
    return;
  } else {
    posOfServo1 += -servo1Dir;
  }
  servo1.write(posOfServo1);
}
void checkServo2( ) {
  static uint32_t Timer;
  if (Timer > millis())
    return;
  Timer = millis() + every2;
  if (servo2Target > posOfServo2) {
    posOfServo2 += servo2Dir;
  } else if (servo2Target == posOfServo2) {
    return;
  } else {
    posOfServo2 += -servo2Dir;
  }
  servo2.write(posOfServo2);
}
void checkServo3(){
  //依Servo1,servo2,以同样方法添加更多舵机
}
单片机小白

主题

回复
  • 温馨提示: 标题不合格、重复发帖、发布广告贴,将会被删除帖子或禁止发言。 详情请参考: 社区发帖规则
  • 您当前输入了 0 个文字。还可以输入 8000 个文字。 已添加复制上传图片功能,该功能目前仅支持chrome和火狐

禁言/删除

X
请选择禁言时长:
是否清除头像:
禁言/删除备注:
昵 称:
 
温馨提示:昵称只能设置一次,设置后无法修改。
只支持中文、英文和数字。

举报

X
请选择举报类型:
请输入详细内容:

顶部