当前位置:网站首页>【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);
}
边栏推荐
- 在虚拟环境下使用pip时默认使用系统环境的pip该怎么办
- Machine learning, Wu Enda, logical regression
- 了解canvas
- [图形学]ASTC纹理压缩格式
- 【JS高级】正则入门基础—关于你想知道的正则表达式_01
- 第二章关系数据库课后习题
- Shell operator, $((expression)) "or" $[expression], expr method, condition judgment, test condition, [condition], comparison between two integers, judgment according to file permission, judgment accor
- Dynamic planning daily practice (1)
- 微信小程序--动态设置导航栏颜色
- QNX modify system time
猜你喜欢

Introduction to radar part vii 1 radar and resolution
![Shell运算符、$((运算式))” 或 “$[运算式]、expr方法、条件判断、test condition、[ condition ]、两个整数之间比较、按照文件权限进行判断、按照文件类型进行判断](/img/65/a735ca2c2902e3fc773dda79438972.png)
Shell运算符、$((运算式))” 或 “$[运算式]、expr方法、条件判断、test condition、[ condition ]、两个整数之间比较、按照文件权限进行判断、按照文件类型进行判断

Special topic of MIMO Radar (0) - General Chapter

Kotlin - Job 任务/取消

Introduction to radar part vii 2 imaging method

LeetCode_52_N皇后Ⅱ
![[play with FPGA in simple terms to learn 10 ----- simple testbench design]](/img/3a/6d2f4f4e35a08803383def9c665df9.png)
[play with FPGA in simple terms to learn 10 ----- simple testbench design]

做测试如何应对新的开发模式?

keepalived双机热备

Problem solving: script file 'scripts\pip script py‘ is not present.
随机推荐
LeetCode_2341_数组能形成多少数对
Research on hardware architecture of Ti single chip millimeter wave radar xwr1642
Special topic of MIMO Radar (0) - General Chapter
Kotlin - Job 任务/取消
Ros2 self study notes: URDF robot modeling
LeetCode_52_N皇后Ⅱ
Point target simulation of SAR imaging (III) -- Analysis of simulation results
Vs2019:constexpr function "qcountleadingzerobits" cannot generate constant expressions
轻重链剖分/树链剖分
Database view detailed exploration
php连接sql server
在虚拟环境下使用pip时默认使用系统环境的pip该怎么办
Remote editing and debugging with vscode
Shell operator, $((expression)) "or" $[expression], expr method, condition judgment, test condition, [condition], comparison between two integers, judgment according to file permission, judgment accor
LeetCode_46_全排列
反常积分的审敛
Error running ‘XXX‘: Command line is too long. Shorten command line for AudioTest or also ...
-20: +usecgroupmemorylimitforheap failed to create virtual machine problem
The principle of Google interview questions is to analyze 12 table tennis balls, one of which is defective. Weigh it with a balance for 3 times to find it
SparkSQL设计及入门,220722,