一、实现要求
- 使用红外避障模块实现低位置处的障碍物避障
- 使用超声波模块实现高位置处的避障
- 使用舵机模块实现超声波模块的多角度探测
二、使用材料
- 可驱动的小车模型
- 红外避障模块
- 超声波模块
- 舵机模块
- Arduino UNO开发板
三、舵机的使用
#define servoPin 2 //servo signal
int pulseWidth;
pinMode(servoPin, OUTPUT);
void turnServo(int angle){
for(int i = 0; i <= 20; i++)
servoPulse(angle);
}
void servoPulse(int angle){
pulseWidth = (angle * 11) + 500;
digitalWrite(servoPin, HIGH);
delayMicroseconds(pulseWidth);
digitalWrite(servoPin, LOW);
delay(20-pulseWidth/1000);
}
四、红外避障模块的使用
#define infraredRight 8 //left infrared signal
#define infraredLeft 11 //right infrared signal
int infraredLeftState;
int infraredRightState;
pinMode(infraredLeft, INPUT);
pinMode(infraredRight, INPUT);
void infraredJudge(){
infraredLeftState = digitalRead(infraredLeft);
infraredRightState = digitalRead(infraredRight);
if(infraredLeftState == LOW && infraredRightState == LOW) {
}
else if(infraredLeftState == HIGH && infraredRightState == LOW){
}
else if(infraredLeftState == LOW && infraredRightState == HIGH){
}
}
五、超声波模块的使用
#define ultrasonicEcho A1 //Echo signal
#define ultrasonicTrig A0 //Trig signal
int distanceFront;
int distanceLeft;
int distanceRight;
pinMode(ultrasonicEcho, INPUT);
pinMode(ultrasonicTrig, OUTPUT);
float distanceTest(){
digitalWrite(ultrasonicTrig, LOW);
delayMicroseconds(2);
digitalWrite(ultrasonicTrig, HIGH);
delayMicroseconds(10);
digitalWrite(ultrasonicTrig, LOW);
float dis = pulseIn(ultrasonicEcho, HIGH);
dis = dis / 58;
return dis;
}