【Arduino实战教程 002】控制舵机转动

实验环境:
Arduino1.8.3 IDE
Arduino mega 2560
奥松移动机器人舵机版本
在做机器人移动控制时,难免会在选择电机和舵机之间有些困惑。简单讲,电机和舵机是两种不同的驱动方式,用的控制器不同,电机是靠电机驱动板驱动,舵机一般是用能驱动舵机的控制器,或者是其他驱动舵机的板子来驱动,还有就是电机只能正转和反转,舵机是可以控制旋转角度。舵机其实也是一种电机,它是使用一个反馈系统来控制电机的位置。舵机通常情况下只能旋转180°。硬件的连接不详细讲解,此处直接上代码:

/**************************************************************
 作者:DaveBobo
 博客:http://blog.csdn.net/DaveBobo/article/details/78755152
**************************************************************/
#include <Servo.h> //载入 Servo.h 库文件
Servo coreservo5; //建立一个舵机对象,名称为 coreservo5
Servo coreservo9; 

int pos = 0;    // variable to store the servo position

void setup() {
  coreservo5.attach(5);//将引脚 5 上的舵机与舵机对象连接起来
  coreservo9.attach(9);
}

void loop() {
  check5();
  check9();
  /* If you wanted to read the angle of your servo at any given time, use servoname.read();
   * If you wanted to write a pulse of a certain width use servoname.writemicroseconds(value in microseconds);
   */
}

void check5()
{
 for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    coreservo5.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 
  {                                
    coreservo5.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
}

void check9()
{
 for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    coreservo9.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 
  {                                7
    coreservo9.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
}

代码编写好,编译上传即可。上传成功后我们可以观察到两个舵机已经开始转动了。
Reference:
https://jingyan.baidu.com/article/cd4c297903fb17756e6e6008.html?qq-pf-to=pcqq.c2c
http://www.arduino.cn/thread-12589-1-1.html
https://www.w3cschool.cn/arduino/arduino_servo_motor.ht

  • 10
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DaveBobo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值