arduino 控制舵机

如题所述

#include <Servo.h>

Arduino自带的Servo函数及其语句,先来介绍一下舵机函数的几个常用语句吧。

1、attach(接口)——设定舵机的接口,只有9或10接口可利用。

2、write(角度)——用于设定舵机旋转角度的语句,可设定的角度范围是0°到180°。

3、read()——用于读取舵机角度的语句,可理解为读取最后一条write()命令中的值。

4、attached()——判断舵机参数是否已发送到舵机所在接口。

5、detach()——使舵机与其接口分离,该接口(9或10)可继续被用作PWM接口。

注:以上语句的书写格式均为“舵机变量名.具体语句()”例如:myservo.attach(9)。

下面就来具体分析一个小程序

#include <Servo.h>//定义头文件,这里有一点要注意,可以直接在

Arduino软件菜单栏单击Sketch>Importlibrary>Servo,调用Servo函数,也可以直接输入#include <Servo.h>,但是在输入时要注意在#include 与<Servo.h>之间要有空格,否则编译时会报错。

Servo myservo;//定义舵机变量名

void setup()
{
myservo.attach(9);//定义舵机接口,9或10
}
void loop()
{
myservo.write(90);//设置舵机旋转的角度
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2021-01-03

本期创客G坊教朋友们使用Arduino编写一段使用电位器控制舵机的小程序

第2个回答  推荐于2018-01-31
Arduino自带的Servo函数库只可以同时控制两个模拟舵机。而且最好接到数字9 、 10脚上。想控制多个舵机就要使用到Arduino的Pwm功能了。 你也可以使用 32路伺服电机控制器 方便驱动多个舵机。本回答被网友采纳
第3个回答  2019-04-22

两种方法,1、arduino有自带的控制舵机的库函数;2、舵机通过PWM控制,搞懂原理后,控制起来也不复杂。可参考网页链接

第4个回答  2018-01-31

网页链接

attach()

Description

Attach the Servo variable to a pin. Note that in Arduino 0016 and earlier, the Servo library supports only servos on only two pins: 9 and 10.

Syntax

servo.attach(pin) 
servo.attach(pin, min, max)

Parameters

servo: a variable of type Servo

pin: the number of the pin that the servo is attached to

min (optional): the pulse width, in microseconds, corresponding to the minimum (0-degree) angle on the servo (defaults to 544)

max (optional): the pulse width, in microseconds, corresponding to the maximum (180-degree) angle on the servo (defaults to 2400)

Example

#include <Servo.h>

Servo myservo;

void setup()
{
myservo.attach(9);
}

void loop() {}


See also

    attached()

    detach()

    Reference Home

    Corrections, suggestions, and new documentation should be posted to the Forum.

    The text of the Arduino reference is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.

相似回答