【Arduino】开发入门【八】舵机操作

【Arduino】开发入门【八】舵机操作+源代码


1.servo类成员函数
函数

说明

attach()


设定舵机的接口,只有9或10接口可利用。

write()


用于设定舵机旋转角度的语句,可设定的角度范围是0到180。

writeMicroseconds()


用于设定舵机旋转角度的语句,直接用微秒作为参数。

read()


用于读取舵机角度的语句,可理解为读取最后一条write()命令中的值。

attached()


判断舵机参数是否已发送到舵机所在接口。

detach()


使舵机与其接口分离,该接口(9或10)可继续被用作PWM接口。

2.Arduino舵机实现代码

// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.


#include <Servo.h>

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

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>=1; 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
}
}



3.Arduino舵机连接图
  实物图中,舵机GND(棕色线)、VCC(红色线)、Signal(橙色线)




  线路图中,舵机GND(黑色线)、VCC(红色线)、Signal(黄色线)

...全文
5113 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
bbxmxzx 2019-05-05
  • 打赏
  • 举报
回复
再来一个!!!!!
line_us 2016-05-19
  • 打赏
  • 举报
回复
越来越好玩了,再来一个

2,851

社区成员

发帖
与我相关
我的任务
社区描述
本论坛以AI、WoS 、XR、IoT、Auto、生成式AI等核心板块组成,为开发者提供便捷及高效的学习和交流平台。 高通开发者专区主页:https://qualcomm.csdn.net/
人工智能物联网机器学习 技术论坛(原bbs) 北京·东城区
社区管理员
  • csdnsqst0050
  • chipseeker
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧