提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
Arduino 超声波避障循迹小车,智能小车
文章目录
- 前言
- 一、仿真原理图
- 二、使用步骤
- 1.引入库
- 2.程序
- 总结
所用到的元器件有:arduino uno1块,L298直流电机驱动板2块,lcd1602带iic1块,HC-05超声波模1块,led灯,直流电机2个,循迹模块4个,有源蜂鸣器1个
当智能小车离障碍物只有20cm时,小车左转,然后前进,当第2和第3个循迹模块遇到黑线时小车前进,第1个和第2个循迹模块遇到黑线时小车左转,第3个和第4个循迹模块遇到黑线时小车右转。
代码如下(示例):
#include
#include
lcd.print("如有不懂的地方加作者Q:2188263281");
代码如下(示例):
#include
#include //引用lcd1602I2C库
//设置LCD1602设备地址,这里的地址是0x3F,一般是0x20,或者0x27,具体看模块手册
LiquidCrystal_I2C lcd(0x27,20,4);
const int Pin1 = 2;
const int Pin2 = 3;
const int Pin3 = 4;
const int Pin4 = 5;
const int Pin5 = 6;
const int Pin6 = 7;
const int Pin7 = 8;
const int Pin8 = 9;
const int TrigPin = 10;
const int EchoPin = 11;
const int fmq = 12;
const int gd1= A0;
const int gd2= A1;
const int gd3= A2;
const int gd4= A3;
int cm;
int sensor[5];
void setup() {
lcd.init(); // 初始化LCD
lcd.backlight(); //设置LCD背景等亮
for(int a = 2; a < 11;a++)
{
pinMode(a, OUTPUT);
}
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
Serial.begin(9600);
pinMode(EchoPin, INPUT);
pinMode(fmq, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(TrigPin, LOW);
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
cm = pulseIn(EchoPin, HIGH) / 58.0;
//算成厘米
cm = (int(cm * 100.0)) / 100.0;
//保留两位小数
lcd.setCursor(0,0);
lcd.print(cm + 1);
lcd.setCursor(3,0);
lcd.print("cm");
sensor[0]=digitalRead(A0);
sensor[1]=digitalRead(A1);
sensor[2]=digitalRead(A2);
sensor[3]=digitalRead(A3);
if(sensor[0] == 0&& sensor[1] == 0&& sensor[2] == 1&&sensor[3] == 0) //0010
{
moto_run4(); //右转
}
else if(sensor[0] == 0&& sensor[1] == 0&& sensor[2] == 0&&sensor[3] == 1) //0001
{
moto_run4() ;//右转
}else if(sensor[0] == 0&& sensor[1] == 1&& sensor[2] == 0&&sensor[3] == 0) //0100
{
moto_run3(); //左转
}else if(sensor[0] == 1&& sensor[1] == 0&& sensor[2] == 0&&sensor[3] == 0) //1000
{
moto_run3(); //左转
}else if(sensor[0] == 0&& sensor[1] == 1&& sensor[2] == 1&&sensor[3] == 0) //0110
{
moto_run1(); //直行
}else if(cm < 20)
{
digitalWrite(fmq, 1);
moto_run3(); //左转
delay(3000);
}else if(sensor[0] == 0&& sensor[1] == 0&& sensor[2] == 0&&sensor[3] == 0)
{
moto_run1(); //向前
digitalWrite(fmq, 0);
}
}
void moto_run1()//向前
{
digitalWrite(Pin1, 1);
digitalWrite(Pin2, 0);
digitalWrite(Pin3, 1);
digitalWrite(Pin4, 0);
digitalWrite(Pin5, 1);
digitalWrite(Pin6, 0);
digitalWrite(Pin7, 1);
digitalWrite(Pin8, 0);
}
void moto_run2()//向后
{
digitalWrite(Pin1, 0);
digitalWrite(Pin2, 1);
digitalWrite(Pin3, 0);
digitalWrite(Pin4, 1);
digitalWrite(Pin5, 0);
digitalWrite(Pin6, 1);
digitalWrite(Pin7, 0);
digitalWrite(Pin8, 1);
}
void moto_run3()//向左
{
digitalWrite(Pin1, 0);
digitalWrite(Pin2, 1);
digitalWrite(Pin3, 0);
digitalWrite(Pin4, 1);
digitalWrite(Pin5, 1);
digitalWrite(Pin6, 0);
digitalWrite(Pin7, 1);
digitalWrite(Pin8, 0);
}
void moto_run4()//向右
{
digitalWrite(Pin1,1);
digitalWrite(Pin2, 0);
digitalWrite(Pin3, 1);
digitalWrite(Pin4, 0);
digitalWrite(Pin5, 0);
digitalWrite(Pin6, 1);
digitalWrite(Pin7, 0);
digitalWrite(Pin8, 1);
}
void moto_run5()//停止
{
digitalWrite(Pin1,0);
digitalWrite(Pin2, 0);
digitalWrite(Pin3, 0);
digitalWrite(Pin4, 0);
digitalWrite(Pin5, 0);
digitalWrite(Pin6, 0);
digitalWrite(Pin7, 0);
digitalWrite(Pin8, 0);
}
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了智能小车的使用。