【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(橙色线)

  

【淘宝】【HC-06蓝牙模块】

【淘宝】【辉盛MG995 55G金属铜齿轮舵机】

【淘宝】【Arduino入门套件  Arduino UNO R3】

【淘宝】【Arduino uno r3官方版】

【淘宝】【意大利原装Arduino云 单片机开发板】

 

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

  

 

需要获取源代码的朋友,可以通过下面三种方式获取

(0)下载地址 http://download.csdn.net/detail/fnext/5252736

(1)请在评论中填写邮件地址,会通过邮箱发送源码。

(2)直接拷贝上面的代码

相关文章

 

【淘宝】【HC-06蓝牙模块】

【淘宝】【辉盛MG995 55G金属铜齿轮舵机】

【淘宝】【Arduino入门套件  Arduino UNO R3】

【淘宝】【Arduino uno r3官方版】

【淘宝】【意大利原装Arduino云 单片机开发板】

【Arduino】开发入门教程【一】什么是Arduino
【Ardunio】开发入门教程【二】数据类型
【Arduino】开发入门教程【三】Arduino开发工具
【Arduino】开发入门教程【四】Arduino驱动安装
【Arduino】开发入门教程【五】Hello Arduino
【Arduino】开发入门教程【六】数据类型转换函数
【Arduino】开发入门教程【七】7段显示器实现+源代码
【Arduino】开发入门教程【八】舵机操作+源代码

//////////////////////////////////////////////

原创声明 转载请注明

本文出自 Ray-Ray的博客

文章地址 http://www.cnblogs.com/rayray/archive/2013/04/15/3015172.html

感谢大家的推荐和收藏

你的支持! 我们的动力!

posted @   Rex..  阅读(61419)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
点击右上角即可分享
微信分享提示