const int TrigPin = 2; //超声波引脚定义
const int EchoPin = 3; //超声波引脚定义
float cm; //测距参数
//l298n驱动上对应的接口
int pinI1=8;//定义I1接口
int pinI2=9;//定义I2接口
int speedpin=11;//定义EA(PWM调速)接口,短路帽要扒掉
int pinI3=6;//定义I3接口
int pinI4=7;//定义I4接口
int speedpin1=10;//定义EB(PWM调速)接口,短路帽要扒掉
#include
#include
#include
#include
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
static const unsigned char PROGMEM logo16_glcd_bmp[] =
{ B00000000, B11000000,
B00000001, B11000000,
B00000001, B11000000,
B00000011, B11100000,
B11110011, B11100000,
B11111110, B11111000,
B01111110, B11111111,
B00110011, B10011111,
B00011111, B11111100,
B00001101, B01110000,
B00011011, B10100000,
B00111111, B11100000,
B00111111, B11110000,
B01111100, B11110000,
B01110000, B01110000,
B00000000, B00110000 };
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
//对引脚的初始化定义
void setup()
{
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3c);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
pinMode(pinI1,OUTPUT);
pinMode(pinI2,OUTPUT);
pinMode(speedpin,OUTPUT);
pinMode(pinI3,OUTPUT);
pinMode(pinI4,OUTPUT);
pinMode(speedpin1,OUTPUT);
pinMode(13,INPUT);
pinMode(12,INPUT);
display.display();
delay(2000);
display.clearDisplay();
}
float testdrawchar(float x)
{
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.setTextSize(2);
display.print("s=");
display.print(x);
display.println("cm");
display.println();
display.display();
delay(1);
}
int chaoshengbo()
{
display.clearDisplay();
//发一个10ms的高脉冲去触发TrigPin
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; //保留两位小数
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(2);
testdrawchar(cm);
}
void zhizou()
{
//直走
analogWrite(speedpin,100);//输入模拟值进行设定速度
analogWrite(speedpin1,100);
digitalWrite(pinI4,LOW);//使直流电机(右)逆时针转
digitalWrite(pinI3,HIGH);
digitalWrite(pinI1,LOW);//使直流电机(左)顺时针转
digitalWrite(pinI2,HIGH);
}
void zhizou1()
{
//直走
analogWrite(speedpin,50);//输入模拟值进行设定速度
analogWrite(speedpin1,48);
digitalWrite(pinI4,LOW);//使直流电机(右)逆时针转
digitalWrite(pinI3,HIGH);
digitalWrite(pinI1,LOW);//使直流电机(左)顺时针转
digitalWrite(pinI2,HIGH);
}
void back()
{
//后退
analogWrite(speedpin,100);//输入模拟值进行设定速度
analogWrite(speedpin1,100);
digitalWrite(pinI4,HIGH);//使直流电机(右)顺时针转
digitalWrite(pinI3,LOW);
digitalWrite(pinI1,HIGH);//使直流电机(左)逆时针转
digitalWrite(pinI2,LOW);
}
void back1()
{
//后退
analogWrite(speedpin,50);//输入模拟值进行设定速度
analogWrite(speedpin1,50);
digitalWrite(pinI4,HIGH);//使直流电机(右)顺时针转
digitalWrite(pinI3,LOW);
digitalWrite(pinI1,HIGH);//使直流电机(左)逆时针转
digitalWrite(pinI2,LOW);
}
void left()
{
//左转
analogWrite(speedpin,60);//输入模拟值进行设定速度
analogWrite(speedpin1,60);
digitalWrite(pinI4,LOW);//使直流电机(右)逆时针转
digitalWrite(pinI3,HIGH);
digitalWrite(pinI1,HIGH);//使直流电机(左)逆时针转
digitalWrite(pinI2,LOW);
}
void right()
{ //右转
analogWrite(speedpin,60);//输入模拟值进行设定速度
analogWrite(speedpin1,60);
digitalWrite(pinI4,HIGH);//使直流电机(右)顺时针转
digitalWrite(pinI3,LOW);
digitalWrite(pinI1,LOW);//使直流电机(左)顺时针转
digitalWrite(pinI2,HIGH);
}
void left1()
{
//左转
analogWrite(speedpin,50);//输入模拟值进行设定速度
analogWrite(speedpin1,48);
digitalWrite(pinI4,LOW);//使直流电机(右)逆时针转
digitalWrite(pinI3,HIGH);
digitalWrite(pinI1,HIGH);//使直流电机(左)逆时针转
digitalWrite(pinI2,LOW);
}
void right1()
{ //右转
analogWrite(speedpin,50);//输入模拟值进行设定速度
analogWrite(speedpin1,48);
digitalWrite(pinI4,HIGH);//使直流电机(右)顺时针转
digitalWrite(pinI3,LOW);
digitalWrite(pinI1,LOW);//使直流电机(左)顺时针转
digitalWrite(pinI2,HIGH);
}
void stop1()
{//停止
digitalWrite(pinI4,HIGH);//使直流电机(右)刹车
digitalWrite(pinI3,HIGH);
digitalWrite(pinI1,HIGH);//使直流电机(左)刹车
digitalWrite(pinI2,HIGH);
}
void csbizhang()
{
while(1)
{
chaoshengbo();
if(cm<20)
{
stop1();
delay(100);
right();
delay(500);
}
else
{
zhizou1();
delay(50);
}
if(cm<10)
{
stop1();
delay(100);
back1();
delay(500);
}
}
}
void xunji()
{
while(1)
{
int a=digitalRead(12);
int b=digitalRead(5);
Serial.print(a);
Serial.print(b);
Serial.println();
if(a==1&&b==0)
{right1();delay(150);}
if(a==0&&b==1)
{left1();delay(150);}
if(a==0&&b==0)
{zhizou1();delay(150);}
if(a==1&&b==1)
{stop1();delay(100);}
}
}
void loop()
{
switch(Serial.read())
{
case 'w'://接收到W
zhizou();
break;
case 's'://接收到s
stop1();
break;
case 'd'://接收到d
right();
break;
case 'a'://接收到a
left();
break;
case 'q'://接收到q
back();
break;
case'e':
csbizhang();
break;
case'r':
xunji();
break;
case'f':
chaoshengbo();
break;
default:
break;
}
}