Show toolbar

2014年4月13日 星期日

Servo Motor Control with an Arduino

標題:使用Arduino控制伺服馬達


Arduino (servo_motor):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <Servo.h>
Servo myservo;
const int umin = 500, umax = 2400, umid = (umin + umax)/2;
const int buttonPin = 2;
int buttonState = 0, buttonDownState = 0, motorState = 0;
void setup() {
pinMode(buttonPin, INPUT);
myservo.attach(9, umin, umax);
myservo.writeMicroseconds(umid);
delay(3000);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
buttonDownState = 1;
} else {
if(buttonDownState == 1) {
if(motorState == 0) {
myservo.writeMicroseconds(umin);
motorState = 1;
} else if(motorState == 1) {
myservo.writeMicroseconds(umid);
motorState = 2;
} else if(motorState == 2) {
myservo.writeMicroseconds(umax);
motorState = 3;
} else {
myservo.writeMicroseconds(umid);
motorState = 0;
}
buttonDownState = 0;
}
}
}

Arduino Diecimila
電阻 - 10kΩ
伺服機 - GWS S03T

說明:
按鈕依序輸出脈衝寬度最小、中間、最大,以控制依序移動伺服機位置。

2 則留言:

  1. 恩?! 伺服機的紅、黑線是電源跟地線嗎? 怎麼都接在地線?

    回覆刪除
  2. 已修正,謝謝你的提醒 : )

    回覆刪除