標(biāo)題: arduino藍(lán)牙小車 [打印本頁]

作者: 小楊8986    時間: 2018-6-14 18:31
標(biāo)題: arduino藍(lán)牙小車
首先,需要的哪些工具:
1、        arduino UNO R3  x1
2、        L298N電機驅(qū)動模塊 x1
3、        HC06 藍(lán)牙模塊 x1
4、        小車底盤 x1
5、        12V電源(7~9V)x1
6、        導(dǎo)線 若干
7、        藍(lán)牙遙控app



第二:L298模塊介紹




第三:App的下載
http://shouji.baidu.com/software/11629425.html
第四:HC-06模塊的介紹:

說一下具體電路連接:
第一步:12V 18650充電電池 接到 L298N 電機驅(qū)動模塊的 +12V 以及 GND,+5V 電源輸出給Arduino UNO R3板子供電。有些人可能說如果電機供電以及板子供電是同一個電源,可能電機轉(zhuǎn)動會影響到板子供電。目前我測試是沒有發(fā)現(xiàn)問題?赡茉蚴潜緛戆遄雍碾娏恳膊皇呛艽螅⑶12V電池本身電量還是足夠,不會出現(xiàn)供電不足問題。

第二步:左電機連接到OUT3和OUT4,右電機連接到OUT1和OUT2
第三步:就是L298N的IN控制引腳和Arduino板子的連接。IN1連接到引腳6,IN2連接到引腳7,IN3連接到引腳4,IN4連接到引腳5.

藍(lán)牙測試代碼
功能:藍(lán)牙小車測試按鍵值程序
  1. 藍(lán)牙測試代碼
  2. 功能:藍(lán)牙小車測試按鍵值程序

  3. void setup() {
  4.   // put your setup code here, to run once:
  5.   Serial.begin(9600);
  6. }

  7. void loop() {
  8.   // put your main code here, to run repeatedly:
  9.   if(Serial.available()>0){
  10.       char ch = Serial.read();
  11.       if(ch == '1'){
  12.          //前進(jìn)
  13.          Serial.println("up");
  14.       }else if(ch == '2'){
  15.          //后退
  16.          Serial.println("back");
  17.       }else if(ch == '3'){
  18.          //左轉(zhuǎn)
  19.          Serial.println("left");
  20.       }else if(ch == '4'){
  21.         //右轉(zhuǎn)
  22.          Serial.println("right");
  23.       }else if(ch=='0'){
  24.         //停車
  25.         Serial.println("stop");
  26.       }else{
  27.         //其他編碼
  28.         Serial.println(ch);
  29.       }
  30.     }
  31. }

  32. 藍(lán)牙小車代碼:

  33. #define IN1 6 // 7 6 右輪
  34. #define IN2 7
  35. #define IN3 4 // 5 4 左輪
  36. #define IN4 5

  37. #define LEFT '3' //左轉(zhuǎn)編碼
  38. #define RIGHT '4'//右轉(zhuǎn)編碼
  39. #define GO '1'//前進(jìn)編碼
  40. #define BACK '2'//后退編碼
  41. #define STOP '0'//停止編碼

  42. void setup() {
  43.   // put your setup code here, to run once:
  44.   Serial.begin(9600);
  45.   pinMode(IN1,OUTPUT);
  46.   pinMode(IN2,OUTPUT);
  47.   pinMode(IN3,OUTPUT);
  48.   pinMode(IN4,OUTPUT);
  49.   initCar();
  50. }

  51. void loop() {
  52.   // put your main code here, to run repeatedly:
  53.   if(Serial.available()>0){
  54.       char ch = Serial.read();
  55.       if(ch == GO){
  56.          //前進(jìn)
  57.          go();
  58.       }else if(ch == BACK){
  59.          //后退
  60.          back();
  61.       }else if(ch == LEFT){
  62.          //左轉(zhuǎn)
  63.          turnLeft();
  64.       }else if(ch == RIGHT){
  65.         //右轉(zhuǎn)
  66.         turnRight();
  67.       }else if(ch=='0'){
  68.         //停車
  69.         stopCar();
  70.       }
  71.     }
  72. }

  73. void initCar(){
  74.   //默認(rèn)全是低電平 停止?fàn)顟B(tài)
  75.   digitalWrite(IN1,LOW);      
  76.   digitalWrite(IN2,LOW);
  77.   digitalWrite(IN3,LOW);   
  78.   digitalWrite(IN4,LOW);
  79. }

  80. /**
  81. * 左轉(zhuǎn)
  82. */
  83. void turnLeft(){
  84.   digitalWrite(IN1,HIGH);      
  85.   digitalWrite(IN2,LOW);         //右輪前進(jìn)
  86.   digitalWrite(IN3,LOW);      
  87.   digitalWrite(IN4,LOW);         //左輪不動
  88. }

  89. /**
  90. * 右轉(zhuǎn)
  91. */
  92. void turnRight(){
  93.   digitalWrite(IN1,LOW);      
  94.   digitalWrite(IN2,LOW);         //右輪不動
  95.   digitalWrite(IN3,HIGH);      
  96.   digitalWrite(IN4,LOW);         //左輪前進(jìn)
  97. }

  98. /**
  99. * 前進(jìn)
  100. */
  101. void go(){
  102.   digitalWrite(IN1,HIGH);      
  103.   digitalWrite(IN2,LOW);         //右輪前進(jìn)
  104.   digitalWrite(IN3,HIGH);      
  105.   digitalWrite(IN4,LOW);         //左輪前進(jìn)
  106. }

  107. /**
  108. * 倒車
  109. */
  110. void back(){
  111.   digitalWrite(IN1,LOW);      
  112.   digitalWrite(IN2,HIGH);        //右輪后退
  113.   digitalWrite(IN3,LOW);      
  114.   digitalWrite(IN4,HIGH);        //左輪后退
  115. }

  116. /**
  117. * 停車
  118. */
  119. void stopCar(){
  120.   initCar();
  121. }
復(fù)制代碼


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial.available()>0){
      char ch = Serial.read();
      if(ch == '1'){
         //前進(jìn)
         Serial.println("up");
      }else if(ch == '2'){
         //后退
         Serial.println("back");
      }else if(ch == '3'){
         //左轉(zhuǎn)
         Serial.println("left");
      }else if(ch == '4'){
        //右轉(zhuǎn)
         Serial.println("right");
      }else if(ch=='0'){
        //停車
        Serial.println("stop");
      }else{
        //其他編碼
        Serial.println(ch);
      }
    }
}

藍(lán)牙小車.rar

1.66 MB, 下載次數(shù): 27, 下載積分: 黑幣 -5


作者: hs88    時間: 2018-8-25 23:33
才十米距離,有什么意義




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1