YFRobot蓝牙小车之Arduino控制程序
适配V1.2 / V1.2.3版本手机app。 之前写的Arduino测试程序,也没有仔细优化,总之发出来给大家参考下吧!!! - /**
- * 蓝牙控制小车
- * BY YFRobot
- * //16进制数据(前进、后退、左转、右转、停止)
- * FORWARD = {(byte) 0xFF, 0x5A, 0x5B, 0x00, 0x01, (byte) 0xFF};
- * BACK = {(byte) 0xFF, 0x5A, 0x5B, 0x00, 0x05, (byte) 0xFF};
- * STOP = {(byte) 0xFF, 0x5A, 0x5B, 0x00, 0x03, (byte) 0xFF};
- * LEFT = {(byte) 0xFF, 0x5A, 0x5B, 0x00, 0x02, (byte) 0xFF};
- * RIGHT = {(byte) 0xFF, 0x5A, 0x5B, 0x00, 0x04, (byte) 0xFF};
- **/
- char BT_val;
- int BT_array[5];
- int E1 = 3;
- int M1 = 4;
- int E2 = 6;
- int M2 = 7;
- void setup(){
- Serial.begin(9600); //设置波特率为9600
- pinMode(M1, OUTPUT);
- pinMode(M2, OUTPUT);
- }
- /* 小车前进 */
- void Forward(){
- digitalWrite(M1,LOW);
- digitalWrite(E1, 1); //PWM调速
- digitalWrite(M2,LOW);
- digitalWrite(E2, 1); //PWM调速
- }
- /* 小车后退 */
- void Back(){
- digitalWrite(M1,HIGH);
- digitalWrite(E1, 1); //PWM调速
- digitalWrite(M2,HIGH);
- digitalWrite(E2, 1); //PWM调速
- }
- /* 小车左转 */
- void Left(){
- digitalWrite(M1,LOW);
- digitalWrite(E1, 1); //PWM调速
- digitalWrite(M2,HIGH);
- digitalWrite(E2, 1); //PWM调速
- }
- /* 小车右转 */
- void Right(){
- digitalWrite(M1,HIGH);
- digitalWrite(E1, 1); //PWM调速
- digitalWrite(M2,LOW);
- digitalWrite(E2, 1); //PWM调速
- }
- /* 小车停止 */
- void Stop(){
- digitalWrite(E1, 0);
- digitalWrite(E2, 0);
- }
- void cmd(){
- int n ;
- for(n=0;n<=5;n++){
- BT_val = Serial.read();
- BT_array[n]=BT_val;
- }
- }
- void loop(){
- cmd();
- Serial.println(BT_array[3],HEX);
- Serial.println(BT_array[4],HEX);
- if(BT_array[0]==0XFFFFFFFF && BT_array[1]==0x5A
- && BT_array[2]==0x5B && BT_array[5]==0XFFFFFFFF){
- switch (BT_array[3]=0x00) //根据键值不同,执行不同的内容
- {
- case 0x00:
- switch(BT_array[4]) //根据数据位的值来进行选择执行不同的动作
- {
- case 0x01:
- Forward(); break;
- case 2:
- Left(); break;
- case 3:
- Stop(); break;
- case 4:
- Right(); break;
- case 5:
- Back(); break;
- }
- break;
- }
- }
- }
复制代码
YFRobot_BT.zip
(1.09 KB, 下载次数: 429)
|