//----------引脚设置----------//
#define left_motor_Go 9 //左电机前进
#define left_motor_Back 3 //左电机后退
#define right_motor_Go 10 //右电机前进
#define right_motor_Back 11 //右电机后退
#define led1 7
#define led2 8
#define buzzer1 A0
#define buzzer2 A3
#define Hw A4 //定义红外 A4 接口
//----------函数声明----------//
void motor_init(void);//电机初始化
void forward(void);//控制小车前进
void back(void); //控制小车后退
void turnRightforward(void);//控制小车右转
void turnLeftforward(void);//控制小车左转
void addSpeed(void);//加速
void subSpeed(void);//减速
void Stop(void);//控制小车停车
void obstacleAvoidance(void);//避障
//----------参数设置----------//
int incomingByte = 0; // 传入的串行数据
int first_speed = 150; //初速度
int val; // val接收避障参数
void setup()
{
Serial.begin(9600);
motor_init();//电机初始化
}
void loop()
{
// 当接收数据时
if (Serial.available() > 0) {
//判断返回串口缓冲区中当前剩余的字符个数是否大于零
// 读取传入的数据:
incomingByte = Serial.parseInt();
switch(incomingByte)
{
case 1://前进
{
forward();
break;
}
case 2://后退
{
back();
break;
}
case 3://停
{
Stop();
break;
}
case 4://左转
{
turnLeftforward();
delay(500);
forward();
break;
}
case 5://右转
{
turnRightforward();
delay(500);
forward();
break;
}
case 6://加速
{
addSpeed();
break;
}
case 7://减速
{
subSpeed();
break;
}
case 8://停
{
Stop();
break;
}
}
}
val = digitalRead(Hw);
obstacleAvoidance();
}
void motor_init(void)
{
//电机初始化,两个电机需要四个PWM。
pinMode (left_motor_Go,OUTPUT); //设置引脚为输出引脚
pinMode (left_motor_Back,OUTPUT); //设置引脚为输出引脚
pinMode (right_motor_Go,OUTPUT); //设置引脚为输出引脚
pinMode (right_motor_Back,OUTPUT); //设置引脚为输出引脚
//设定避障input
pinMode (Hw,INPUT); //设置红外传感器为输入
pinMode (led1,OUTPUT);
pinMode (led2,OUTPUT);
analogWrite(led1,0);
analogWrite(led2,0);
}
void obstacleAvoidance(void)//避障
{
if(val==1)//无障碍物
{
analogWrite(led1,0);
analogWrite(led2,0);
analogWrite(buzzer1,0);
analogWrite(buzzer2,0);
}
else if(val==0)//遇到障碍物
{
analogWrite(buzzer1,255);
analogWrite(buzzer2,0);
analogWrite(led1,255);
analogWrite(led2,0);
back();
delay(1000);
Stop();
}
else//异常情况
{
Stop();
delay(1000);
}
}
//----------小车动作----------//
void forward(void)
{
analogWrite(left_motor_Go,200);
analogWrite(left_motor_Back,10);
analogWrite(right_motor_Go,220);
analogWrite(right_motor_Back,10);
}
void back(void)
{
analogWrite(left_motor_Go,10);
analogWrite(left_motor_Back,200);
analogWrite(right_motor_Go,10);
analogWrite(right_motor_Back,200);
}
void turnRightforward(void)
{
analogWrite(left_motor_Go,250);
analogWrite(left_motor_Back,10);
analogWrite(right_motor_Go,150);
analogWrite(right_motor_Back,10);
}
void turnLeftforward(void)
{
analogWrite(left_motor_Go,150);
analogWrite(left_motor_Back,10);
analogWrite(right_motor_Go,250);
analogWrite(right_motor_Back,10);
}
void addSpeed(void)//加速
{
analogWrite(left_motor_Go,250);
analogWrite(left_motor_Back,10);
analogWrite(right_motor_Go,250);
analogWrite(right_motor_Back,10);
}
void subSpeed(void)//减速
{
analogWrite(left_motor_Go,150);
analogWrite(left_motor_Back,10);
analogWrite(right_motor_Go,150);
analogWrite(right_motor_Back,10);
}
void Stop(void)
{
analogWrite(left_motor_Go,10);
analogWrite(left_motor_Back,10);
analogWrite(right_motor_Go,10);
analogWrite(right_motor_Back,10);
}
透明灯发出光线(不可见),黑色的接收
VCC接电源 GND接地(我们是接在面包板上了,uno板上不够用了 )
(现在回头发现没有必要…接在面包板上…是我傻了)
OUT接了A4端口
这里led灯正极接7 负极接8
蜂鸣器正极接A0 负极接A3
(因为用了两个端口 所以才说面包板上面的正负极没有用上)
(也不知道为什么要用两个端口)
#define led1 7
#define led2 8
#define buzzer1 A0
#define buzzer2 A3
VCC接电源 GND接地
TXD接uno板上的RX RXD接uno板上的TX
四个逻辑通道从左到右()分别接5、6、9、10
输出A接左边电机,输出B接右边电机
下载一个蓝牙串口助手APP
这个自己网上查吧
一开始我们小车转速总是很慢,必须推着才能走。
后来我们发现是电压不足,我们一开始用的是四块电池,但是加起来输出才6V,后来又加了一个充电宝(如图)。