找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 4265|回復(fù): 1
收起左側(cè)

arduino藍(lán)牙小車

[復(fù)制鏈接]
ID:241112 發(fā)表于 2018-6-14 18:31 | 顯示全部樓層 |閱讀模式
首先,需要的哪些工具:
1、        arduino UNO R3  x1
2、        L298N電機(jī)驅(qū)動(dòng)模塊 x1
3、        HC06 藍(lán)牙模塊 x1
4、        小車底盤(pán) x1
5、        12V電源(7~9V)x1
6、        導(dǎo)線 若干
7、        藍(lán)牙遙控app

115006wtt50ovjfmjlgtgg.png

第二:L298模塊介紹

123726t97v968bpg9n9cxy.jpg


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

說(shuō)一下具體電路連接:
第一步:12V 18650充電電池 接到 L298N 電機(jī)驅(qū)動(dòng)模塊的 +12V 以及 GND,+5V 電源輸出給Arduino UNO R3板子供電。有些人可能說(shuō)如果電機(jī)供電以及板子供電是同一個(gè)電源,可能電機(jī)轉(zhuǎn)動(dòng)會(huì)影響到板子供電。目前我測(cè)試是沒(méi)有發(fā)現(xiàn)問(wèn)題。可能原因是本來(lái)板子耗電量也不是很大,并且12V電池本身電量還是足夠,不會(huì)出現(xiàn)供電不足問(wèn)題。

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

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

  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);         //左輪不動(dòng)
  88. }

  89. /**
  90. * 右轉(zhuǎn)
  91. */
  92. void turnRight(){
  93.   digitalWrite(IN1,LOW);      
  94.   digitalWrite(IN2,LOW);         //右輪不動(dòng)
  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

評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

ID:104477 發(fā)表于 2018-8-25 23:33 來(lái)自手機(jī) | 顯示全部樓層
才十米距離,有什么意義
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表