当前位置:网站首页>【Ardunio】2种方法控制舵机
【Ardunio】2种方法控制舵机
2022-07-23 07:06:00 【与太阳有关_】
这是我参加第十一届海上航行器大赛 的 方案三:Ardunio方案 中的控制舵机
详细的舵机控制参考-https://www.bilibili.com/video/BV15X4y157ay
第一种方法:
analogWrite(pin, value)
这种方法只能让舵机在 0~90度转 且输入的参数只能在 [70,350] 之间,否则会不动,达不到我的需求。
#define pinServo 11 /*舵机控制引脚 基于ATmega168和ATmega328的Arduino控制器中,
analogWrite()函数支持以下引脚: 3, 5, 6, 9, 10, 11*/
//舵机角度 测试出来的值
#define servoRight 70
#define servoMiddle 210
#define servoLeft 350
void setup()
{
Serial.begin(9600);// 串口初始化,波特率9600字节/秒
}
void loop()
{
prePWM = angle2Pwm(realAngle);//计算返回当前舵机角度对应的 PWM
analogWrite(pinServo,prePWM);
}
int angle2Pwm(int preAngle)
{
//把角度转换成相应的 pwm
int realAngle;
prePWM = float(255*(float)preAngle / 360);
return prePWM;
}
第二种方法:
Servo库
能控制的角度范围较大,满足我的需求。nice
#include <Servo.h>
Servo myservo; //创建一个舵机控制对象
#define pinServo 11//舵机控制引脚 基于ATmega168和ATmega328的Arduino控制器中,analogWrite()函数支持以下引脚: 3, 5, 6, 9, 10, 11。
void setup()
{
myservo.attach(pinServo,500,2500); // 告知Arduino舵机的数据线连接在哪一个引脚上。
}
void loop()
{
//让舵机在0~180来回转动
myservo.write(0);
delay(1000);
myservo.write(180);
delay(1000);
}
边栏推荐
- 回溯法解决 八皇后问题
- 关于#redis#的问题:Redis设置数据持久化之后还是会有丢失数据的风险
- 数据库系统原理与应用教程(040)—— MySQL 查询(二):设置要查询的列名或表达式
- 2022-07-22 回顾链表操作以及部分问题
- 【MUDUO】EPOLLPOLLER事件分发器
- Power bi - Comprehensive Application
- Smart city infrastructure management based on bim+3dgis
- 轻重链剖分/树链剖分
- IP地址分类及范围
- Shell operator, $((expression)) "or" $[expression], expr method, condition judgment, test condition, [condition], comparison between two integers, judgment according to file permission, judgment accor
猜你喜欢

【可視化調度軟件】上海道寧為SMB組織帶來NETRONIC下載、試用、教程

LeetCode_51_N皇后

Wu Enda machine learning series p31~p42
Smart canteen data analysis system

浅谈Anroid设备的CPU类型以及so文件的放置目录

"Computing beast" Inspur nf5468a5 GPU server open trial free application

专题讲座5 组合数学 学习心得(长期更新)

China leads the United States in another emerging technology field and stands at the commanding height of scientific and technological innovation

How to deal with the new development mode when doing testing?
![[graphics] ASTC texture compression format](/img/d1/734fe91d56716c610980696a8e0d06.png)
[graphics] ASTC texture compression format
随机推荐
[图形学]ASTC纹理压缩格式
Smart canteen data analysis system
微服务重点
[cocos creator] spin animation, monitoring and playback end
About this pointer
决策树详解
Convergence of abnormal integral
Smart city infrastructure management based on bim+3dgis
Knowledge map: basic concepts
-XX:+UseCGroupMemoryLimitForHeap 无法创建虚拟机问题
ModuleNotFoundError: No module named ‘setuptools_ rust‘
PHP获取当前时间戳三位毫秒 - 毫秒时间戳
LeetCode_52_N皇后Ⅱ
Special lecture 5 combinatorial mathematics learning experience (long-term update)
【PART 2】OAK-D+TurtleBot3机器人项目全解析
2022暑假软件创新实验室集训 项目实战1
IP address classification and range
In depth interpretation of EVM's ecological Empire
Wu Enda machine learning series p31~p42
接口测试-简单的接口自动化测试Demo