close



ESP32 控制 TB6612FNG 直流馬達驅動∕控制板 請看這篇

 

使用Android手機如何用Arduino藍芽連線ESP32控制蜘蛛機器人
需要使用雙電源
如果使用單電源,電流會被馬達抽走
ESP32晶片電流不足會無法正常運作


材料:
18650電池*2
ESP32*1
TB6612FNG*1
蜘蛛機器人*2


 

1.jpg

2.jpg




AUDINO 程式碼

  1. // Include necessary libraries
  2. #include <BLEDevice.h>
  3. #include <BLEServer.h>
  4. #include <BLEUtils.h>
  5.  
  6. // 定義 UUIDs (注意要與App Inventor內容對應)
  7. #define SERVICE_UUID            "C6FBDD3C-7123-4C9E-86AB-005F1A7EDA01"
  8. #define CHARACTERISTIC_UUID_RX  "B88E098B-E464-4B54-B827-79EB2B150A9F"
  9. #define CHARACTERISTIC_UUID_TX  "D769FACF-A4DA-47BA-9253-65359EE480FB"
  10.  
  11. String BLE_Code;
  12. BLECharacteristic *pCharacteristic;
  13. bool deviceConnected = false;
  14.  
  15. int directState = 0; // 動作
  16. int speed_c = 192;
  17. // PWM, INA, INB 與LED接腳變數
  18. int PWMA = 13;
  19. int INA1 = 12;
  20. int INA2 = 14;
  21. int STBY = 27;
  22. int INB1 = 25;
  23. int INB2 = 26;
  24. int PWMB = 33;
  25. const int ledPin = 2;
  26.  
  27. const int freq = 10000;
  28. const int resolution = 8;
  29.  
  30.  
  31.  
  32. // 設定 callbacks onConnect & onDisconnect函數
  33. class MyServerCallbacks: public BLEServerCallbacks {
  34.   void onConnect(BLEServer* pServer) {
  35.     deviceConnected = true;
  36.   };
  37.   void onDisconnect(BLEServer* pServer) {
  38.     deviceConnected = false;
  39.   }
  40. };
  41.  
  42. // 設定 callback function 當收到新的資訊 (from the Android application)
  43. class MyCallbacks: public BLECharacteristicCallbacks {
  44.   void onWrite(BLECharacteristic *pCharacteristic) {
  45.     std::string rxValue = pCharacteristic->getValue();
  46.     BLE_Code="";
  47.     if(rxValue.length() > 0) {
  48.       Serial.print("接收資料為 : ");
  49.       for(int i = 0; i < rxValue.length(); i++) {
  50.         BLE_Code+=rxValue[i];
  51.         Serial.print(rxValue[i]);
  52.       }
  53.       Serial.println();
  54.       BLE_Code.toUpperCase();
  55.       Serial.println(BLE_Code);
  56.         if(BLE_Code.indexOf("GO")!=-1)
  57.         {
  58.           Serial.println("GO");
  59.           directState=1;
  60.         } else if(BLE_Code.indexOf("BACK")!=-1) {
  61.           Serial.println("BACK");
  62.           directState=2;
  63.         } else if(BLE_Code.indexOf("LEFT")!=-1) {
  64.           Serial.println("LEFT");
  65.           directState=3;
  66.         } else if(BLE_Code.indexOf("RIGHT")!=-1) {
  67.           Serial.println("RIGHT");
  68.           directState=4;
  69.         } else if(BLE_Code.indexOf("RELEASE")!=-1) {
  70.           Serial.println("RELEASE");
  71.           directState=5;
  72.         }
  73.         if(BLE_Code.indexOf("192")!=-1)
  74.         {
  75.           speed_c=192;
  76.         } else if(BLE_Code.indexOf("255")!=-1) {
  77.           speed_c=255;
  78.         }
  79.     }
  80.   }
  81. };
  82.  
  83. void setup() {
  84.   Serial.begin(115200);
  85.   pinMode(ledPin, OUTPUT); //設定腳位為輸出
  86.   pinMode(INA1,OUTPUT);
  87.   pinMode(INA2,OUTPUT);
  88.   pinMode(PWMA,OUTPUT);
  89.   pinMode(STBY,OUTPUT);
  90.   pinMode(INB1,OUTPUT);
  91.   pinMode(INB2,OUTPUT);
  92.   pinMode(PWMB,OUTPUT);
  93.   //digital output test
  94.   digitalWrite(INA1,HIGH); //設定腳位HIGH LOW
  95.   digitalWrite(INA2,LOW);
  96.   digitalWrite(PWMA,LOW);
  97.   digitalWrite(STBY,HIGH);
  98.   digitalWrite(INB1,HIGH);
  99.   digitalWrite(INB2,LOW);
  100.   digitalWrite(PWMB,LOW);
  101.   delay(1000);
  102.  
  103.   //analog output(PWM) test 設定LED Channel PWM 頻率
  104.   ledcSetup(0, freq, resolution);
  105.   ledcSetup(1, freq, resolution);
  106.   ledcSetup(2, freq, resolution);
  107.   ledcSetup(3, freq, resolution);
  108.   ledcSetup(4, freq, resolution);
  109.   ledcSetup(5, freq, resolution);
  110.   ledcSetup(6, freq, resolution);
  111.   //設定腳位Channel
  112.   ledcAttachPin(INA1, 0);
  113.   ledcAttachPin(INA2, 1);
  114.   ledcAttachPin(PWMA, 2);
  115.   ledcAttachPin(STBY, 3);
  116.   ledcAttachPin(INB1, 4);
  117.   ledcAttachPin(INB2, 5);
  118.   ledcAttachPin(PWMB, 6);
  119.   
  120.   // 建立BLE Device
  121.   BLEDevice::init("ESP32_WeMos1");
  122.  
  123.   // 建立BLE Server
  124.   BLEServer *pServer = BLEDevice::createServer();
  125.   pServer->setCallbacks(new MyServerCallbacks());
  126.  
  127.   // 建立BLE Service
  128.   BLEService *pService = pServer->createService(SERVICE_UUID);
  129.  
  130.   // 建立BLE Characteristic
  131.   pCharacteristic = pService->createCharacteristic(
  132.                       CHARACTERISTIC_UUID_TX,
  133.                       BLECharacteristic::PROPERTY_NOTIFY);                     
  134. //  pCharacteristic->addDescriptor(new BLE2902());
  135.   BLECharacteristic *pCharacteristic = pService->createCharacteristic(
  136.                                          CHARACTERISTIC_UUID_RX,
  137.                                          BLECharacteristic::PROPERTY_WRITE);
  138. pCharacteristic->setCallbacks(new MyCallbacks());
  139.  
  140.   // 開始(起)service
  141.   pService->start();
  142.  
  143.   // 開始(起)advertising
  144.   pServer->getAdvertising()->start();
  145.   Serial.println("等待BLE手機連線....");
  146.   
  147.   digitalWrite(ledPin,LOW);
  148.   delay(500);
  149.   digitalWrite(ledPin,HIGH);
  150.   delay(500);
  151.   digitalWrite(ledPin,LOW);
  152. }
  153.  
  154. void loop() {
  155.     // Check received message and control output accordingly
  156.     if (directState==1){
  157.         move(speed_c, 1); // full speed, go
  158.         Serial.println("前進");
  159.     } else if(directState==2) {
  160.         move(speed_c, 2); // full speed, down
  161.         Serial.println("後退");
  162.     } else if(directState==3) {
  163.         move(speed_c, 3); // full speed, left
  164.         Serial.println("左轉");
  165.     } else if(directState==4) {
  166.         move(speed_c, 4); // full speed, right
  167.         Serial.println("右轉");
  168.     } else if(directState==5) {
  169.       stop(); //stop
  170.       Serial.println("stop");
  171.     }
  172.   delay(100); //go for 1 second
  173. }
  174.  
  175. void move( int speed, int direction){
  176. //Move specific motor at speed and direction
  177. //speed: 0 is off, and 255 is full speed
  178. //direction: 0 clockwise, 1 counter-clockwise
  179.  
  180.   ledcWrite(3, 255); //STBY disable standby
  181.   Serial.println(direction);
  182.   if(direction == 1){
  183.     //設定馬達1為正轉
  184.     ledcWrite(0, 255); //INA1
  185.     ledcWrite(1, 0);   //INA2
  186.     ledcWrite(2, speed);   //PWMA
  187.     //設定馬達2為正轉
  188.     ledcWrite(4, 255); //INB1
  189.     ledcWrite(5, 0); //INB2
  190.     ledcWrite(6, speed);   //PWMB
  191.     digitalWrite(ledPin,HIGH);
  192.     delay(500);
  193.     digitalWrite(ledPin,LOW);
  194.   } else if (direction == 2){
  195.     //設定馬達1為反轉
  196.     ledcWrite(0, 0);   //INA1
  197.     ledcWrite(1, 255); //INA2
  198.     ledcWrite(2, speed);   //PWMA
  199.     //設定馬達2為反轉
  200.     ledcWrite(4, 0);   //INB1
  201.     ledcWrite(5, 255); //INB2
  202.     ledcWrite(6, speed);   //PWMB
  203.     digitalWrite(ledPin,HIGH);
  204.     delay(500);
  205.     digitalWrite(ledPin,LOW);
  206.   } else if (direction == 3){
  207.     //設定馬達1為正轉
  208.     ledcWrite(0, 255); //INA1
  209.     ledcWrite(1, 0);   //INA2
  210.     ledcWrite(2, speed);   //PWMA
  211.     //設定馬達2為反轉
  212.     ledcWrite(4, 0);   //INB1
  213.     ledcWrite(5, 255); //INB2
  214.     ledcWrite(6, speed);   //PWMB
  215.     digitalWrite(ledPin,HIGH);
  216.     delay(500);
  217.     digitalWrite(ledPin,LOW);
  218.   } else if (direction == 4){
  219.     //設定馬達1為反轉
  220.     ledcWrite(0, 0);   //INA1
  221.     ledcWrite(1, 255); //INA2
  222.     ledcWrite(2, speed);   //PWMA
  223.     //設定馬達2為正轉
  224.     ledcWrite(4, 255); //INB1
  225.     ledcWrite(5, 0); //INB2
  226.     ledcWrite(6, speed);   //PWMB
  227.     digitalWrite(ledPin,HIGH);
  228.     delay(500);
  229.     digitalWrite(ledPin,LOW);
  230.   }
  231.   Serial.println(speed);
  232. }
  233.  
  234. void stop(){
  235. //enable standby  
  236.   ledcWrite(3, 0); //STBY enable standby
  237.   //Serial.println("stop");
  238. }
複製代碼


 

3.png

4.png

5.png

6.png

7.png





ARDUINO 程式
 ESP32_BLE_car_20200713.rar (2.12 KB, 下載次數: 0, 售價: 10 金T幣)

ANDROID APK檔案
 BluetoothControl_2020071301.rar (1.95 MB, 下載次數: 2, 售價: 10 金T幣)
文章出處:網頁設計,網站架設 ,網路行銷,網頁優化,SEO - NetYea 網頁設計

arrow
arrow
    創作者介紹
    創作者 NetYea 網頁設計 的頭像
    NetYea 網頁設計

    NetYea 網頁設計

    NetYea 網頁設計 發表在 痞客邦 留言(0) 人氣()