本帖最后由 请叫我小萌兽 于 2015-2-10 08:13 编辑
笔者文采不好,不废话,直接上图!
ARDUINO代码:
const int TrigPin = 2; //将 Arduino 的 Pin2 连接至 HC-SR04 的 Echo
const int EchoPin = 3; //将 Arduino 的 Pin3 连接至 HC-SR04 的 Trig
unsigned long Time_Echo_us = 0; //用于记录传感器返回的脉冲宽度
float cm = 0; //用于存储距离值
int motorPin1 = 4; //将 Arduino 的 Pin4 连接至 L298N 的 IN3
int motorPin2 = 5; //将 Arduino 的 Pin5 连接至 L298N 的 IN4
int motorPin3 = 6; //将 Arduino 的 Pin6 连接至 L298N 的 IN2
int motorPin4 = 7; //将 Arduino 的 Pin7 连接至 L298N 的 IN1
int s1; //用于存储距离值
int s2; //用于存储距离值
#include <Servo.h>
#include <LCD5110_CN.h>
Servo myservo; //声明一个 Servo 对象
LCD5110 myGLCD(8,9,10,11,12); //Clk → Pin 8, Din → Pin9, DC → Pin 10, CE → Pin 12, RST → 11
extern uint8_t SmallFont[];
extern uint8_t MediumNumbers[];
extern uint8_t BigNumbers[];
void setup()
{
myGLCD.InitLCD(); //液晶初始化
myservo.attach(1, 500, 2500); //引脚 1 上的舵机连接到 myservo 对象
myservo.writeMicroseconds(1500); //设置舵机到初始位置
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
}
void forward() //向后
{
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
}
void stop() //刹停
{
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, HIGH);
}
void backward() //向后
{
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
}
void turn_right() //右转
{
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
}
void turn_left() //左转
{
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
}
void control()
{
digitalWrite(TrigPin, LOW); //低高低电平发一个短时间脉冲去TrigPin
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH); //开始通过 TrigPin 发送脉冲
delayMicroseconds(10); //设置脉冲宽度为 10us
digitalWrite(TrigPin, LOW); //结束脉冲
Time_Echo_us = pulseIn(EchoPin, HIGH); //获取 HC-SR04 返回的脉冲宽度
if((Time_Echo_us < 23200) && (Time_Echo_us > 1)) //脉冲有限范围(1, 23200)
{
cm = Time_Echo_us / 58.0; //将回波时间换算成cm
myGLCD.setFont(BigNumbers); //大号字体
myGLCD.printNumF(cm,2, RIGHT, 24); //输出结果至 myGLCD
delay(10);
myGLCD.clrScr(); //清屏
}
}
void comm()
{
for( int pos = 1500; pos <= 2400; pos++) //舵机转至 左侧
{
myservo.writeMicroseconds(pos);
delay(1);
}
control(); //超声波测距
s1 = cm; //s1 存储小车左侧所距障碍物距离
for(int pos = 2400; pos >= 600; pos--) //舵机转至 右侧
{
myservo.writeMicroseconds(pos);
delay(1);
}
control(); //超声波测距
s2 = cm; //s2 存储小车右侧所距障碍物距离
if(s1 > s2)
{
turn_left();
delay(150);
}
else
if(s1 < s2)
{
turn_right();
delay(150);
}
else
if((s1 <= 20) || (s2 <= 20))
{
backward();
delay(100);
}
stop();
delay(50);
for(int pos = 600; pos <= 1500; pos++) //设置舵机到初始位置
{
myservo.writeMicroseconds(pos);
delay(1);
}
}
void loop()
{
control(); //超声波测距
if(cm <= 20)
{
stop(); //刹停
comm(); //壁障处理
}
else
{
forward(); //前进
}
}
超声波测距,5110显示距离 视频:
壁障小车 视频:
http://www.tudou.com/programs/view/JfsmhDzMrpY/
5110汉字取模软件和库文件使用方法,详见:
http://www.geek-workshop.com/for ... &highlight=5110 |