arduino-舵机

1. 认识舵机

#include <Servo.h>
Servo x;
void setup() {
  // put your setup code here, to run once:
  x.attach(7);
}

void loop() {
  // put your main code here, to run repeatedly:
  x.write(0);     //给舵机写入角度
  delay(500);     //等待转动
  x.write(50);
  delay(500);
  x.write(100);
  delay(500);
  x.write(180);
  delay(500);     
}

2. 舵机转动(利用for循环)

#include <Servo.h>
Servo x;
void setup() {
  // put your setup code here, to run once:
  x.attach(7);
}

void loop() {
  // put your main code here, to run repeatedly:
  for (int i = 0; i <= 180; i++) {
    x.write(i);
    delay(10);
  }
  for (int i = 180; i <= 0; i--) {
    x.write(i);
    delay(10);
  }
}

3. 电位器控制舵机

注意:

  1. 电位器:中间模拟端口,两边正负极
  2. 舵机:棕色代表负极,红色代表正极,黄色代表端口
#include <Servo.h>
Servo x;
int a;
void setup() {
 Serial.begin(9600);
 x.attach(7); //相当于pinMode,舵机连接的端口
 pinMode(A1,INPUT);
}

void loop() {
 a = analogRead(A1);
 x.write(a/5);
 Serial.println(a/5);
}

4. 例子4:电位器控制舵机(利用映射实现)

#include<Servo.h>
Servo myServo;

void setup() {
  myServo.attach(7);
}
void loop() {
  int val = analogRead(A0);
  val = map(val, 0, 1023, 0, 180);
  myServo.write(val);
  delay(100);
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

1024节

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

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

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

打赏作者

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

抵扣说明:

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

余额充值