代码
int readRange(int Trig,int Echo)
{
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
int distance = pulseIn(Echo, HIGH);
return distance/58;
}
volatile int i;
volatile int x;
volatile int y;
int speed_6;
int speed_11;
void setup() {
i = 0;
x = 30;
y = 15;
pinMode(A4, INPUT);
pinMode(A5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
digitalWrite(6, 0);
digitalWrite(5, 0);
pinMode(11, OUTPUT);
pinMode(3, OUTPUT);
digitalWrite(11, 0);
digitalWrite(3, 0);
}
void loop() {
while (true) {
i = readRange(A5, A4);
if (i > x - y && i < x + y) {
if (i < x) {
speed_6=255;
digitalWrite(5, 0);
analogWrite(6, speed_6);
speed_11=255;
digitalWrite(3, 0);
analogWrite(11, speed_11);
} else if (i > x) {
speed_6=255;
digitalWrite(6, 0);
analogWrite(5, speed_6);
speed_11=255;
digitalWrite(11, 0);
analogWrite(3, speed_11);
}
} else {
digitalWrite(6, 0);
digitalWrite(5, 0);
digitalWrite(11, 0);
digitalWrite(3, 0);
}
}
}
本作品反馈型智能跟随书包的传感器模块采用超声波传感器,智能跟随的原理是:首先设定一个反应距离范围,超声波传感器测量的距离只有在此范围内才能起作用。其次,再设定智能跟随保持距离,当超声波测量的距离超过该距离时,小车前进;当超声波测得距离小于该距离时,小车后退。
材料清单
- 跟随书包1个 核心编程主控1个 超声波传感器1个 超声波支架及底板1个 电机驱动模块2个 数码显示管模块1个 4P(2+2)支架若干 X1
步骤1 智能跟随书包流程图 获取当前距离dist 20≤dis≤40 否 是 30
评论