一.前言
最近在公司学习Arduino uno ,用它实现小车超声波避障功能。实现的功能很简单,就是在小车前方挂一个超声波模块,当碰到障碍物时,会通过舵机进行摆头,判断两边的距离,进行左右转弯。但是碰到了这样一个问题,舵机库和pwm输出冲突,当舵机旋转时,pwm输出函数analogWrite()不管用了。
二.解决
先分析一下问题产生的原因,我们之前如果想控制舵机,比较常见的方法就是调用舵机库:
#include
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 i