|
#include <NewPing.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define LOGO16_GLCD_HEIGHT 16 //定义显示高度
#define LOGO16_GLCD_WIDTH 16 //定义显示宽度
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
#include <SoftwareSerial.h>
//pin 0为RX(接收),pin 1为TX(发送)
SoftwareSerial BlueTeeth(0,1);
char getstr; //定义蓝牙串口接收的字符串变量
int Left_motor_go= 8; //左电机前进(IN1)
int Left_motor_back=9; //左电机后退(IN2)
int Right_motor_go=10; // 右电机前进(IN3)
int Right_motor_back=11; // 右电机后退(IN4)
const int SensorRight = 2; //左边红外避障传感器()
const int SensorLeft = 3; //右边红外避障传感器()
int SR_2; //右边红外避障传感器状态
int SL_2; //左边红外避障传感器状态
int echo = 6;
int trig = 5;
int lled=13;
int rled=12;
float dis;
void full_show(char str[100]);
void run1();
void run2();
void run3();
void setup()
{
Serial.begin(9600);//初始化
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
// init done
display.clearDisplay();
full_show("Ready!!!");
pinMode(lled, OUTPUT);
pinMode(rled, OUTPUT);
pinMode(SensorRight, INPUT);
pinMode(SensorLeft, INPUT);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(Left_motor_go,OUTPUT); // PIN 8 (PWM)
pinMode(Left_motor_back,OUTPUT); // PIN 9 (PWM)
pinMode(Right_motor_go,OUTPUT);// PIN 10 (PWM)
pinMode(Right_motor_back,OUTPUT);// PIN 11 (PWM)
}
void led_shinning(int n){//闪烁n次
int i=0;
for(i=0;i<n;i++){
digitalWrite(lled,HIGH);
delay(200); //执行时间,可以调整
digitalWrite(lled,LOW);
digitalWrite(rled,HIGH);
delay(200); //执行时间,可以调整
digitalWrite(rled,LOW);
}
}
void full_show( char str[100])
{
display.clearDisplay();
display.setCursor(0,0); //设置字体的起始位置
display.setTextSize(2); //设置字体大小
display.setTextColor(WHITE); //设置字体白色
display.print(str) ;
display.display();
delay(100);
}
void show_dis()
{//显示距离
display.setCursor(60,40); //设置字体大小
display.setTextSize(1);
display.setTextColor(WHITE);
if(get_distance())
display.print(get_distance()),display.print("cm");
else {
//设置字体白色
display.print("Out of range!");
}
display.display(); //把缓存的都显示
delay(100);
}
void show(char str[30])
{
//在指定位置显示str字符串
display.clearDisplay();
display.setCursor(0,0); //设置字体的起始位置
display.setTextSize(2); //设置字体大小
display.setTextColor(WHITE); //设置字体白色
display.print("Mode:") ;
display.setCursor(0,40); //设置字体的起始位置
display.setTextSize(1); //设置字体大小
display.setTextColor(WHITE); //设置字体白色
display.print("Distance=");
display.setCursor(60,20); //设置字体的起始位置
display.setTextSize(1); //设置字体大小
display.setTextColor(WHITE); //设置字体白色
display.println(str); //输出字符
display.display(); //把缓存的都显示
}
float get_distance() //用超声波模块获取的障碍物距离
{
float rcm;
digitalWrite(trig, LOW); //低高低电平发一个短时间脉冲
delayMicroseconds(2); //更小时间延时更准确
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
rcm = pulseIn(echo, HIGH)/58.0; //时间换算成cm
Serial.print("rcm=");
Serial.println(rcm);
return rcm;
}
void run(int time) // 前进
{
show("Running!");
digitalWrite(Right_motor_go,HIGH);
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,150);
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,HIGH);
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,150);
analogWrite(Left_motor_back,0);
delay(time*100); //执行时间,可以调整
}
void brake(int time) //刹车,停车
{
show("Stop");
digitalWrite(Right_motor_go,LOW);
digitalWrite(Right_motor_back,LOW);
digitalWrite(Left_motor_go,LOW);
digitalWrite(Left_motor_back,LOW);
delay(time*5) ;//执行时间,可以调整
}
void left(int time) //左转(左轮慢,右轮快)
{
show("Turn Left");
digitalWrite(lled,HIGH);
digitalWrite(Right_motor_go,HIGH);
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,255);
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,LOW);
digitalWrite(Left_motor_back,HIGH);
analogWrite(Left_motor_go,150);
analogWrite(Left_motor_back,0);
delay(time*5 );
digitalWrite(lled,LOW);
}
void spin_left(int time) //左转(左轮不动,右轮前进)
{ show("Spin_left!");
digitalWrite(lled,HIGH);
digitalWrite(Right_motor_go,HIGH);
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,190);
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,HIGH);
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,0);
analogWrite(Left_motor_back,0);
delay(time*5); //执行时间,可以调整
digitalWrite(lled,LOW);
}
void spin_right(int time) //右旋转(右轮不动,左轮前进)
{ show("Spin_right!");
digitalWrite(rled,HIGH);
digitalWrite(Right_motor_go,HIGH);
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,0);
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,LOW);
digitalWrite(Left_motor_back,HIGH);
analogWrite(Left_motor_go,190);
analogWrite(Left_motor_back,0);//PWM比例0~255调速
delay(time*5 ); //执行时间,可以调整
digitalWrite(rled,LOW);
}
void right(int time) //右转(右轮慢,左轮子快)
{
show("Turn Right");
digitalWrite(rled,HIGH);
digitalWrite(Right_motor_go,HIGH);
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,120);
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,LOW);
digitalWrite(Left_motor_back,HIGH);
analogWrite(Left_motor_go,255);
analogWrite(Left_motor_back,0);//PWM比例0~255调速
delay(time*5 ); //执行时间,可以调整
digitalWrite(rled,LOW);
}
void back(int time) //后退
{
show("Back");
digitalWrite(Right_motor_go,LOW);
digitalWrite(Right_motor_back,HIGH);
analogWrite(Right_motor_go,0);
analogWrite(Right_motor_back,200);
digitalWrite(Left_motor_go,HIGH);
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,0);
analogWrite(Left_motor_back,200);//PWM比例0~255调速
delay(time*100); //执行时间,可以调整
}
void run1(int time) //1档
{
digitalWrite(Right_motor_go,HIGH); // 右电机前进
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,130);//PWM比例0~255调速,左右轮差异略增减
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,HIGH); // 左电机前进
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,130); //PWM比例0~255调速,左右轮差异略增减
analogWrite(Left_motor_back,0);
delay(time*100); //执行时间,可以调整
}
void run2(int time) // 2档
{
digitalWrite(Right_motor_go,HIGH); // 右电机前进
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,180);//PWM比例0~255调速,左右轮差异略增减
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,HIGH); // 左电机前进
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,180); //PWM比例0~255调速,左右轮差异略增减
analogWrite(Left_motor_back,0);
delay(time*100); //执行时间,可以调整
}
void run3(int time) // 3档
{
digitalWrite(Right_motor_go,HIGH); // 右电机前进
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,255);//PWM比例0~255调速,左右轮差异略增减
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,HIGH); // 左电机前进
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,255); //PWM比例0~255调速,左右轮差异略增减
analogWrite(Left_motor_back,0);
delay(time*100); //执行时间,可以调整
}
void loop(){
getstr=Serial.read();//根据接收到的字符指令进行动作
if(Serial.available())
{
int flag=0;
if(getstr=='a')//进入自动避障
{
full_show("Switching mode....");
led_shinning(3);
delay(100);
while(1)
{
int dis=get_distance();
SR_2 = digitalRead(SensorRight);
SL_2 = digitalRead(SensorLeft);
if(dis<=13&&SL_2 == LOW )/// 都是且左边有障碍物, 后退后右旋转
{
back(600);
Serial.println("back");
delay(600);
spin_right(200);
Serial.println("spin_right");
delay(200);
}
else if(dis<=13){//前面或者前面和右边都有障碍,后退后左旋
back(600);
Serial.println("back");
delay(600);
spin_left(200);
Serial.println("spin_left");
delay(600);
}
else if (SL_2 == HIGH&&SR_2==HIGH)
{
run(50); //调用前进函
Serial.println("run");
}
else if (SL_2 == HIGH && SR_2 == LOW)// 右边探测到有障碍物,有信号返回,向左转
{
spin_left(80);
Serial.println("spin_left");
}
else if (SR_2 == HIGH && SL_2 == LOW) //左边探测到有障碍物,有信号返回,向右转
{
spin_right(80);
Serial.println("spin_right");
show_dis();
}
getstr=BlueTeeth.read();
if(getstr=='t')//退出自动避障
{
full_show("Exiing the mode....t");
led_shinning(2);
brake(50);
Serial.println(" brake");
break;
}
}
}
getstr=Serial.read();//根据接收到的字符指令进行动作
if(getstr=='g')
{
full_show("run");
run(50);
Serial.println("run");
}
if(getstr=='b')
{
back(50);
show("back");
Serial.println("back");
}
if(getstr=='L')
{
left(50);
show("left");
Serial.println("left");
}
if(getstr=='r')
{
right(50);
show("right");
Serial.println("right");
}
if(getstr=='s')
{
brake(50);
show("right");
Serial.println("brake");
}
if(getstr=='1')
{
spin_left(50);
show("spin_left");
Serial.println("spin_left");
}
if(getstr=='0')
{
spin_right(50);
show("spin_right");
Serial.println("spin_right");
}
if(getstr=='7')
{
show("First gear");
run1(300);
Serial.println("run1");
}
if(getstr=='8')
{
show("Second gear");
run2(300);
Serial.println("run2");
}
if(getstr=='9')
{
show("Third gear");
run3(300);
Serial.println("run3");
}
}
}
|
|