#include "U8glib.h"
#include <Servo.h>
Servo myservo;
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE | U8G_I2C_OPT_DEV_0); // I2C / TWI
#define word1W 16 //定义第一个汉字的宽度
#define word1H 16 //定义第一个汉字的高度
#define word2W 16 //定义第二个汉字的宽度
#define word2H 16 //定义第二个汉字的高度
const int TrigPin = 2;
const int EchoPin = 3;
int a = 5;
int b = 6;
int c = 9;
int d = 10;
float cm;
//第一个汉字的编码
static unsigned char word1[] U8G_PROGMEM = {
0x00, 0x00, 0xBE, 0x7F, 0xA2, 0x00, 0xA2, 0x00, 0xA2, 0x00, 0xBE, 0x3F, 0x88, 0x20, 0x88, 0x20,
0xBA, 0x20, 0x8A, 0x20, 0x8A, 0x3F, 0x8A, 0x00, 0xBA, 0x00, 0x87, 0x00, 0x80, 0x7F, 0x00, 0x00
};
//第二个汉字的编码
static unsigned char word2[] U8G_PROGMEM = {
0x40, 0x00, 0x80, 0x00, 0xFF, 0x7F, 0x00, 0x00, 0x28, 0x0A, 0xC8, 0x09, 0x28, 0x0A, 0xF8, 0x0F,
0x80, 0x00, 0xFE, 0x3F, 0x42, 0x20, 0x22, 0x22, 0xF2, 0x27, 0x22, 0x24, 0x02, 0x28, 0x02, 0x10
};
//绘画第一个汉字的子程序
void word1draw(void)
{
u8g.drawXBMP( 0, 0, word1W, word1H, word1); //x坐标0,y坐标0,宽度,高度,编码word1
}
//绘画第二个汉字的子程序
void word2draw(void)
{
u8g.drawXBMP( 18, 0, word2W, word2H, word2); //x坐标18,y坐标0,宽度,高度,编码word2
}
void chao(){
digitalWrite(TrigPin, LOW); //低高低电平发一个短时间脉冲去TrigPin
delayMicroseconds(2); // delayMicroseconds在更小的时间内延时准确
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW); //通过这里控制超声波的发射
cm = pulseIn(EchoPin, HIGH) / 58.0; //将回波时间换算成cm
cm = (int(cm * 100.0)) / 100.0; //保留两位小数
Serial.print("Distance:");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(50); }
void qian() {
Serial.write("qian");
digitalWrite(a, HIGH );
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
}
void qianzuo() {
Serial.write("qianzuo");
digitalWrite(a, LOW );
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
}
void qianyou() {
Serial.write("QIANyou");
digitalWrite(a, HIGH );
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
}
void ting() {
Serial.println("BT is ready!");
// 返回到手机调试程序上
Serial.write("TING");
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
}
void hou() {
Serial.write("hou");
analogWrite(a, 0);
analogWrite(b, 120);
analogWrite(c, 0);
analogWrite(d, 120);
}
void houzuo() {
Serial.write("houzuo");
analogWrite(a, 80);
analogWrite(b, 150);
analogWrite(c, 0);
analogWrite(d, 150);
}
void houyou() {
Serial.write("houyou");
analogWrite(a, 0 );
analogWrite(b, 150);
analogWrite(c, 80);
analogWrite(d, 150);
}
void xian(){
u8g.firstPage();
do
{chao();
word1draw();//调用绘画第一个汉字的子程序显示汉字
word2draw();//调用绘画第二个汉字的子程序显示汉字
u8g.setFont(u8g_font_fub30);//设置字体和自号,目前测试字号有fub14,17,20,30,49n
u8g.setPrintPos(0, 50); //显示的位置
u8g.print(cm);//显,k示变量val的值
u8g.setFont(u8g_font_fub14);//设置字体和自号,目前测试字号有fub14,17,20,30
u8g.setPrintPos(95, 50); //显示的位置
u8g.print("cm");//显示单位
}
while ( u8g.nextPage() );
delay(50);
}
void setup(void)
{ Serial.begin(38400);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
myservo.attach(11);
}
void zhuan()//原地转弯
{ analogWrite(a, 120);
analogWrite(b, 0);
analogWrite(c, 0);
analogWrite(d, 120);
}
void bizhang() {
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
delay(200);
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
return;
}
void loop(void)
{ chao();
xian();
int x;
int dis[3];
qian();
myservo.write(90);
dis[1] =cm;
if (dis[1] < 30)
{ ting();
for (x = 90; x <= 180; x += 1)
{
myservo.write(x);
delay(15);}
chao();
xian();
dis[2] =cm;
for (x = 180; x >= 0; x -= 1)
{
myservo.write(x);
delay(15);
if (x == 90)
dis[1] =cm;
}
chao();
xian();
dis[0] =cm;
for (x = 0; x <= 90; x += 1)
{ myservo.write(x);
delay(15);
}
if (dis[0] <dis[2])
{qianzuo();
delay(250);}
if (dis[0] >= dis[2])
{qianyou();
delay(250);}
}
}
|