演示视频:https://www.bilibili.com/video/BV1CM41187oh/?share_source=copy_web&vd_source=d43eaf7cae2ed3677cb93f05f1110b0f
前言:
这是本人大一寒假期间,在某宝上捡了个蓝牙模块,于是心血来潮想把前阵子做的循迹小车改成可以蓝牙遥控且有自动避障功能的小车。ps:本人作为大一咸鱼一条,刚接触这个领域,水平不高,希望站内大佬多给建议。
,
“家里杜邦线不够了又没面包板所以拆东头接西头,接的线很乱”
材料:
控制模块:Arduino UNO;
电机驱动模块: L298N;
循迹模块: 红外传感器;
电源:两节3.7V的18650;
蓝牙:BT08-E;
小车主体;
概述:
将循迹用的四路红外传感器掰下来装到前后左右四个方向,通过传感器传出的信号判断周围是否有障碍物再作出相应运动,从而达到避障效果,再配合蓝牙模块实现远程遥控。
电源用的两节18650,因为L298N内置稳压所以我电源给L298N供电,再由L298N给arduino供电,最后arduino再给蓝牙模块以及红外传感器供电。
注意共地!!!注意共地!!!注意共地!!!
具体接法:
电源正极接L298N的12v输入;
arduino uno 的vin引脚接L298N的5v输入;
arduino uno 、红外传感器、BT08-E、L298N的GND串联起来接到电源负极;
值得注意的是,arduino发送接收的电压跟蓝牙BT08-E发送接收的电压不同,所以保险起见最好连个分压电路。不连其实也没太大关系(至少我到现在还没烧)。
代码部分:
#define LeftMotor1 9
#define LeftMotor2 6
#define RightMotor1 10
#define RightMotor2 11
#define track1 3
#define track2 4
#define track3 12
#define track4 13
bool mode;
bool A;
bool B;
bool C;
bool D;
char serial_data;
int Sensor[4] = {1, 1, 1, 1}; //初始化循迹的值
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //串口初始化
Track_Init(); //循迹模块初始化
Motor_Init(); //电机引脚初始化
}
void loop() {
Sensor_Read();
if( mode == 1 ){
avoiding(); //手柄模式
if( A == 1){
RUN2();}
else if (B == 1){
BACK2();}
else if (C == 1){
LEFT2();}
else if (D == 1){
RIGHT2();}
else if (A == 0 || B == 0 || C == 0 || D == 0){
STOP();}}
else {
self_avoiding();}}//自动模式
void Track_Init()
{
pinMode(track1, INPUT);
pinMode(track2, INPUT);
pinMode(track3, INPUT);
pinMode(track4, INPUT);
}
void Motor_Init()
{
pinMode(LeftMotor1, OUTPUT);
pinMode(LeftMotor2, OUTPUT);
pinMode(RightMotor1, OUTPUT);
pinMode(RightMotor2, OUTPUT);
}
void Sensor_Read()
{
Sensor [0] = digitalRead(track1);
Sensor [1] = digitalRead(track2);
Sensor [2] = digitalRead(track3);
Sensor [3] = digitalRead(track4);
}
void Motor_Speed(int Left1_Speed, int Left2_Speed, int Right1_Speed, int Right2_Speed)
{
analogWrite(LeftMotor1, Left1_Speed);
analogWrite(LeftMotor2, Left2_Speed);
analogWrite(RightMotor1, Right1_Speed);
analogWrite(RightMotor2, Right2_Speed);
}
void RUN2 ()
{
Motor_Speed(200,0,200,0);
delay(15);
}
void BACK2 ()
{
Motor_Speed(0,200,0,200);
delay(15);
}
void LEFT2 ()
{
Motor_Speed(0,100,200,0);
delay(15);
}
void RIGHT2 ()
{
Motor_Speed(200,0,0,100);
delay(15);
}
void RUN ()
{
Motor_Speed(100,0,100,0);
delay(15);
}
void BACK ()
{
Motor_Speed(0,100,0,100);
delay(15);
}
void RIGHT ()
{
Motor_Speed(100,0,0,0);
delay(15);
}
void LEFT ()
{
Motor_Speed(0,0,100,0);
delay(15);
}
void STOP()
{
Motor_Speed(0,0,0,0);
delay(15);
}
void avoiding()
{
if (Sensor [0] == 1 && Sensor [1] == 0 && Sensor [2] == 1 && Sensor [3] == 1) //1-0-1-1 后退
{ BACK2();
delay(300);}
else if (Sensor [0] == 1 && Sensor [1] == 1 && Sensor [2] == 0 && Sensor [3] == 1) //1-1-0-1 前进
{ RUN2();
delay(300);}
else if (Sensor [0] == 1 && Sensor [1] == 1 && Sensor [2] == 1 && Sensor [3] == 0) //1-1-1-0 右转
{ RIGHT2();}
else if (Sensor [0] == 0 && Sensor [1] == 1 && Sensor [2] == 1 && Sensor [3] == 1) //0-1-1-1 左转
{ LEFT2();}
else if (Sensor [0] == 0 && Sensor [1] == 0 && Sensor [2] == 1 && Sensor [3] == 0) //0-0-1-0 急退
{ BACK2();
delay(300);}
else if (Sensor [0] == 0 && Sensor [1] == 0 && Sensor [2] == 1 && Sensor [3] == 1) //0-0-1-1 左转2
{ LEFT2();}
else if (Sensor [0] == 1 && Sensor [1] == 0 && Sensor [2] == 0 && Sensor [3] == 1) //1-0-0-1 右转2
{ RIGHT2();}
else if (Serial.available()>0)
{serial_data = Serial.read();
Serial.print("serial_data = ");
Serial.println(serial_data);
Serial.print("mode = ");
Serial.println(mode);
Serial.print("ABCD = ");
Serial.print(A);
Serial.print(B);
Serial.print(C);
Serial.println(D);
if (serial_data == 'w' )
{ A = 1;}
else if (serial_data == 's' )
{ B = 1;}
else if (serial_data == 'a' )
{ C = 1;}
else if (serial_data == 'd' )
{ D = 1;}
else if (serial_data == 'p')
{ A = 0 ;
B = 0;
C = 0;
D = 0;}
else if (serial_data == 'm' )
{ mode = 0;
Serial.println("self_avoiding on");
Serial.print("mode = ");
Serial.print(mode);
Serial.print("serial_data = ");
Serial.println(serial_data);}}
else
{ STOP();}
}
void self_avoiding()
{
if (Sensor [0] == 1 && Sensor [1] == 0 && Sensor [2] == 1 && Sensor [3] == 1) //1-0-1-1 后退
{ BACK();
delay(1000);
LEFT();
delay(500);}
else if (Sensor [0] == 1 && Sensor [1] == 1 && Sensor [2] == 0 && Sensor [3] == 1) //1-1-0-1 前进
{ RUN();}
else if (Sensor [0] == 1 && Sensor [1] == 1 && Sensor [2] == 1 && Sensor [3] == 0) //1-1-1-0 右转
{ RIGHT();}
else if (Sensor [0] == 0 && Sensor [1] == 1 && Sensor [2] == 1 && Sensor [3] == 1) //0-1-1-1 左转
{ LEFT();}
else if (Sensor [0] == 0 && Sensor [1] == 0 && Sensor [2] == 1 && Sensor [3] == 0) //0-0-1-0 急退
{ BACK();
delay(1000);
RIGHT();
delay(500);}
else if (Sensor [0] == 0 && Sensor [1] == 0 && Sensor [2] == 1 && Sensor [3] == 1) //0-0-1-1 左转2
{ LEFT();}
else if (Sensor [0] == 1 && Sensor [1] == 0 && Sensor [2] == 0 && Sensor [3] == 1) //1-0-0-1 右转2
{ RIGHT();}
else if (Serial.available()>0)
{serial_data = Serial.read();
Serial.print("serial_data = ");
Serial.println(serial_data);
Serial.print("mode = ");
Serial.println(mode);
if (serial_data == 'n')
{ mode = 1;
Serial.println("avoiding on");
Serial.print("mode = ");
Serial.println(mode);
Serial.print("serial_data = ");
Serial.println(serial_data);}}
else
{ RUN();}
}
提示:完整的代码可以直接烧录Arduino运行,不过地图不同,执行效果也不同,完全copy就能用,就不要想了(摘自无言侠)
注意:因为占用了硬件串口,所以给arduino录程序的时候记得把引脚1引脚0的杜邦线拔掉。
当然嫌麻烦也可以启用软件串口:
#include <SoftwareSerial.h>
SoftwareSerial BTserial(2, 3);
最后打开蓝牙调试器,设置相关按键就完成啦
蓝牙调试器按键设置:前进:按下“w”松开“p” ; 后退:按下“s”松开“p” ; 向左:按下“a”松开“p” ; 向右:按下“d”松开“p”;自动模式:“m” ; 遥控模式:“n”;
蓝牙调试器:文章来源:https://www.toymoban.com/news/detail-413142.html
链接:https://pan.baidu.com/s/1fH1YznS0gcOV-CmjFipVxA
提取码:13j7文章来源地址https://www.toymoban.com/news/detail-413142.html
到了这里,关于将循迹小车改为蓝牙控制自动避障小车#arduino的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!