一.所需器件:1.HC05蓝牙模块 2 .arduino板 3.连接线 4.电脑
二.所需软件:1.arduinoIDE编译环境 2.蓝牙串口app
三.连接方式:RXD-TX TXD-RX VCC-VCC GND-GND
四.代码
#define STOP 0
#define GO 1
#define RIGHT 2
#define LIFT 3
#define BACK 4
void setup()
{
Serial.begin(9600); //设置波
//设置马达
pinMode(lift1,OUTPUT);
pinMode(lift2,OUTPUT);
pinMode(right1,OUTPUT);
pinMode(right2,OUTPUT);
pinMode(liftpwmA,OUTPUT);
pinMode(rightpwmB,OUTPUT);
}
void loop()
{
Bluetooth();
}
void move1(int kind,int speed1)//移动,参数:1.运动模式,2.速度
{
analogWrite(liftpwm,speed1);
analogWrite(rightpwm,speed1);
switch(kind)
{
case GO:
Serial.println("向前");
digitalWrite(lift1,HIGH);
digitalWrite(lift2,LOW);
digitalWrite(right1,HIGH);
digitalWrite(right2,LOW);
break;
case RIGHT:
Serial.println("右转");
digitalWrite(lift1,HIGH);
digitalWrite(lift2,LOW);
digitalWrite(right1,LOW);
digitalWrite(right2,HIGH);
break;
case LIFT:
Serial.println("左转");
digitalWrite(lift1,LOW);
digitalWrite(lift2,HIGH);
digitalWrite(right1,HIGH);
digitalWrite(right2,LOW);
break;
case BACK:
Serial.println("后退");
digitalWrite(lift1,LOW);
digitalWrite(lift2,HIGH);
digitalWrite(right1,LOW);
digitalWrite(right2,HIGH);
break;
default:
Serial.println("停"); //输出状态
digitalWrite(lift1, LOW);
digitalWrite(lift2, LOW);
digitalWrite(right1, LOW);
digitalWrite(right2, LOW);
break;
}
}
void Bluetooth()//蓝牙模块
{
Serial.println("Hello World!");
if(Serial.available())
{
i=Serial.parseInt();
}
Serial.println(i,DEC);
switch(i)
{
case a:
{
move1(GO,200);
Serial.println("前进");
break;
}
case b:
{
move1(LIFT,250);
Serial.println("左转");
break;
}
case c:
{
move1(RIGHT,250);
Serial.println("右转");
break;
}
case d:
{
move1(BACK,200);
Serial.println("后退");
break;
}
case e:
{
move1(STOP,0);
Serial.println("STOP!!!");
break;
}
}
}
五.补充:推荐此款http://app.mi.com/details?id=com.shenyaocn.android.BlueSPP 蓝牙串口app。
- 软件安装完毕后,点击右上方“连接”连接arduino小车的蓝牙;
- 连接完毕后将软件切换为“键盘”模式,选择编辑模式
- 在按钮文本处输入该按钮代表的功能;
- 将后面的选项改为字符,并输入该功能要发送的信息;
- 退出编辑模式,即可控制小车