1.单词
| word | meaning |
|---|---|
| polarity | 极性 |
| symmetric | 不对称的 |
| diode | 二极管 |
| anode | 正极 |
| cathode | 负极 |

对比图

占空比

线路图

image.png
#include <Servo.h>
Servo servo_5;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
servo_5.attach(5);
}
void loop() {
// put your main code here, to run repeatedly:
servo_5.write(180);
servo_5.write(0);
}

image.png
电位器范围:0~1023

image.png
#include <Servo.h>
#include<LiquidCrystal.h>
LiquidCrystal mylcd(10,9,5,4,3,2);
Servo sv;
float d;
float checkdistance() { //都是硬性要求
digitalWrite(13,LOW);
delayMicroseconds(2); //等待2微秒
digitalWrite(13,HIGH);
delayMicroseconds(10); //等待10微秒
digitalWrite(13,LOW); //让他发一个低-高-低的信号,然后信号就发出去了
float distance=pulseIn(12,HIGH) /58.00; //由方波信号返回时间返回距离
return distance;
}
void setup() {
// put your setup code here, to run once:
sv.attach(6);
Serial.begin(9600);
d=0;
pinMode(13,OUTPUT);
pinMode(12,INPUT);
mylcd.begin(16,2);
mylcd.display();
}
void loop() {
// put your main code here, to run repeatedly:
d=checkdistance();
Serial.println(d);
if(d<=10)
{
for(int i=0;i<=180;i++) sv.write(i);
for(int i=180;i>=0;i--) sv.write(i);
}
mylcd.setCursor((16-3)-((16-3)/2),1);
mylcd.print(d);
}

作业
定义三个按钮管脚为a,b,c
代码:
#include <Servo.h>
Servo sv;
void setup() {
// put your setup code here, to run once:
sv.attach(6);
Serial.begin(9600);
pinMode(a,INPUT);
pinMode(b,INPUT);
pinMode(c,INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(a))
{
sv.write(45);
delay(500);
sv.write(0);
}
if(digitalRead(b))
{
sv.write(90);
delay(500);
sv.write(0);
}
if(digitalRead(c))
{
sv.write(180);
delay(500);
sv.write(0);
}
}