【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

  • 9
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
可以通过使用Arduino板和循迹模块来实现循迹控制舵机转动的功能。具体实现步骤如下: 1.连接循迹模块和Arduino板。将循迹模块的VCC和GND分别连接到Arduino板的5V和GND,将循迹模块的OUT1-OUT4分别连接到Arduino板的D2-D5引脚。 2.连接舵机。将舵机的VCC和GND分别连接到Arduino板的5V和GND,将舵机的信号线连接到Arduino板的D9引脚。 3.编写Arduino程序。程序的主要步骤包括读取循迹模块的输出值、判断是否检测到黑色线条、控制舵机转动。代码如下: ``` #include <Servo.h> // 引入Servo库 Servo myservo; // 创建Servo对象 int left = 2; // 循迹模块左侧输出连接到Arduino板的2号引脚 int mid_left = 3; // 循迹模块左侧中央输出连接到Arduino板的3号引脚 int mid_right = 4; // 循迹模块右侧中央输出连接到Arduino板的4号引脚 int right = 5; // 循迹模块右侧输出连接到Arduino板的5号引脚 void setup() { myservo.attach(9); // 将舵机的信号线连接到Arduino板的9号引脚 } void loop() { int sensor_left = digitalRead(left); // 读取左侧输出值 int sensor_mid_left = digitalRead(mid_left); // 读取左侧中央输出值 int sensor_mid_right = digitalRead(mid_right); // 读取右侧中央输出值 int sensor_right = digitalRead(right); // 读取右侧输出值 if (sensor_left == LOW && sensor_mid_left == LOW && sensor_mid_right == LOW && sensor_right == LOW) { myservo.write(90); // 如果检测不到黑色线条,舵机转动到中心位置 } else if (sensor_left == LOW && sensor_mid_left == LOW && sensor_mid_right == HIGH && sensor_right == HIGH) { myservo.write(120); // 如果检测到左侧黑色线条,舵机转动到左侧 } else if (sensor_left == HIGH && sensor_mid_left == LOW && sensor_mid_right == HIGH && sensor_right == HIGH) { myservo.write(150); // 如果检测到左侧黑色线条和中央黑色线条,舵机转动到更左侧 } else if (sensor_left == HIGH && sensor_mid_left == HIGH && sensor_mid_right == LOW && sensor_right == HIGH) { myservo.write(60); // 如果检测到右侧黑色线条和中央黑色线条,舵机转动到更右侧 } else if (sensor_left == HIGH && sensor_mid_left == HIGH && sensor_mid_right == HIGH && sensor_right == LOW) { myservo.write(30); // 如果检测到右侧黑色线条,舵机转动到右侧 } delay(100); // 延时100毫秒 } ``` 4.上传程序到Arduino板。将编写好的程序上传到Arduino板。 5.测试循迹控制舵机转动的效果。将循迹模块放在黑色线条上,舵机会根据检测到的黑色线条的位置进行相应的转动

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

DaveBobo

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

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

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

打赏作者

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

抵扣说明:

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

余额充值