蓝牙遥控小车设计(二)——车体搭建和利用串口遥控小车

上周回家参加同学婚礼,刚回来就感冒,害我一周都不敢出宿舍门.....

先上两张图,车体早已搭好

 

 

基本功能以实现,能利用PC串口工具对小车进行遥控,亦能利用Amrino APP用手机蓝牙进行遥控(发送字符遥控或者利用重力感应遥控)

 

 

我先把利用串口工具发送字符遥控的代码贴出来

void setup() 
{
  Serial.begin(9600);
  pinMode(4, OUTPUT);  //M1
  pinMode(5, OUTPUT);  //E1
  pinMode(6, OUTPUT);  //E2
  pinMode(7, OUTPUT);  //M2
}
 
void loop() 
{
  char i;
  if (Serial.available()) 
  {
    i = Serial.read();    //read
    switch (i)
    {
      case 'w':          //forward
        Serial.println("forward");
        digitalWrite(4, HIGH);
        digitalWrite(5, LOW);
        digitalWrite(6, HIGH);
        digitalWrite(7, LOW);
    
        break;
      case 's':          //back
        Serial.println("back");
        digitalWrite(4, LOW);
        digitalWrite(5, HIGH);
        digitalWrite(6, LOW);
        digitalWrite(7, HIGH);
        break;
      case 'a':          //left
        Serial.println("left");
        digitalWrite(4, HIGH);
        digitalWrite(5, LOW);
        digitalWrite(6, HIGH);
        digitalWrite(7, HIGH);
        break;
      case 'd':          //right
        Serial.println("right");
        digitalWrite(4, HIGH);
        digitalWrite(5, HIGH);
        digitalWrite(6, HIGH);
        digitalWrite(7, LOW);
        break;
      case 't':          //stop
        Serial.println("stop");
        digitalWrite(4, HIGH);
        digitalWrite(5, HIGH);
        digitalWrite(6, HIGH);
        digitalWrite(7, HIGH);
        break;         
      default:
         break;
    }
  }
}

实现效果:输入w小车前进  输入s小车后退  输入a左转  输入d右转   输入t停止

posted @ 2014-04-19 13:04  iulo  阅读(1050)  评论(1编辑  收藏  举报