Arduino库教程-Servo-Sweep
Sweep
将一个RC伺服电机的轴扫过180度。
这个例子利用了Arduino伺服库。
硬件要求
- Arduino or Genuino Board
- 伺服电机
- 连接线
电路
- 伺服电机有三根线:电源、接地和信号。电源线通常是红色的,应该连接到Arduino或genuino板的5V引脚上。接地线通常为黑色或棕色,应连接到板上的地引脚。该信号引脚通常是黄色或橙色,应连接到主板上的引脚pin 9。
图由 Fritzing 软件绘制
原理图
样例代码
/* Sweep
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
modified 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Sweep
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
[Get Code]
更多
- attach()
- write()
- map()
- Servo library reference
- Knob: 用电位器控制伺服的位置。
- Sweep: 扫描伺服电机前进后退的轴。
获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号