Latest News

首頁 » 數位製造 » 【Arduino機器人車載無線控制】使用HC-05藍牙,NRF24L01和HC-12收發器模塊

【Arduino機器人車載無線控制】使用HC-05藍牙,NRF24L01和HC-12收發器模塊

在本教學中,我們將學習如何無線控制我們在以前的視訊中製作的Arduino機器人車。
我將向您展示三種不同的無線控制方法,使用HC-05藍牙模組,NRF24L01收發模組和HC-12遠端無線模組,以及使用智慧手機和定制Android應用程式。
您可以觀看以下視訊或閱讀下面的書面教學瞭解更多詳情。

我已經有關於如何使用Arduino板連線和使用這些模組的教學,所以如果你需要更多的細節,你可以隨時檢查它們。
他們中的每一個的連結可以在文中找到。

使用HC-05藍牙模組的Arduino機器人車控制

我們將從藍牙通信開始,為此,我們需要兩個需要配置為主裝置和從裝置的HC-05藍牙模組。

Arduino Robot Car HC-05 Bluetooth Control

我們可以通過使用AT指令輕鬆實現,我將操縱桿設定為主人,Arduino機器人車成為奴隸。
以下是此示例的完整電路原理圖:

Arduino Robot Car HC-05 Bluetooth Control Circuit Schematic

您可以從以下連結取得此示例所需的元件:

  • HC-05 Bluetooth Module …………………………… Amazon
  • Joystick Module ………………………………………… Amazon
  • 18650 Batteries ………………………………………… Amazon
  • L298N Driver ……………………………………………. Amazon
  • 12V High Torque DC Motor ……………………….. Amazon
  • Arduino Board ………………………………………….. Amazon

*請注意:這些是聯盟鏈接。 如果您通過這些鏈接購買組件,我可以進行佣金。
我將以這種方式感謝您的支持!

源代碼

我們將使用上一個教程中使用相同的代碼,我們直接使用操縱桿來控制Arduino機器人車,我們將對其進行一些修改。

HC-05主碼:

  1. /*
  2. Arduino Robot Car Wireless Control using the HC-05 Bluetooth
  3.  
  4. == MASTER DEVICE – Joystick ==
  5.  
  6. by Dejan Nedelkovski, www.HowToMechatronics.com
  7. */
  8.  
  9. int xAxis, yAxis;
  10.  
  11. void setup() {
  12. Serial.begin(38400); // Default communication rate of the Bluetooth module
  13. }
  14.  
  15. void loop() {
  16. xAxis = analogRead(A0); // Read Joysticks X-axis
  17. yAxis = analogRead(A1); // Read Joysticks Y-axis
  18.  
  19. // Send the values via the serial port to the slave HC-05 Bluetooth device
  20. Serial.write(xAxis/4); // Dividing by 4 for converting from 0 – 1023 to 0 – 256, (1 byte) range
  21. Serial.write(yAxis/4);
  22. delay(20);
  23. }

主裝置或操縱桿上的代碼非常簡單。 我們只需要讀取操縱桿的X和Y值,實際上可以調節電機的速度,並通過序列連接埠將其傳送到到從機HC-05藍牙裝置。 我們可以在這裡註意,從0到1023的操縱桿的模擬值通過將它們潛水4轉換為從0到255的值。

我們這樣做,因為從0到255的範圍可以通過藍牙裝置傳送到,在另一邊或Arduino機器人車上更容易被接受為1個位元組。

所以在這裡,如果序列接收到2個位元組,X和Y值,使用Serial.read()函數,我們將讀取它們。

  1. // Code from the Arduino Robot Car
  2.  
  3. // Read the incoming data from the Joystick, or the master Bluetooth device
  4. while (Serial.available() >= 2) {
  5. x = Serial.read();
  6. delay(10);
  7. y = Serial.read();
  8. }

現在我們只需要將值轉換回0到1023之間的範圍,適用於下面的電機控制代碼,我們已經解釋了它在前一個視頻中的工作原理。

  1. // Code from the Arduino Robot Car
  2.  
  3. // Convert back the 0 – 255 range to 0 – 1023, suitable for motor control code below
  4. xAxis = x*4;
  5. yAxis = y*4;

只需一個簡單的注意事項,當上傳代碼時,我們需要斷開Arduino板的RX和TX引腳。

完成HC-05從代碼:

  1. /*
  2. Arduino Robot Car Wireless Control using the HC-05 Bluetooth
  3.  
  4. == SLAVE DEVICE – Arduino robot car ==
  5.  
  6. by Dejan Nedelkovski, www.HowToMechatronics.com
  7. */
  8.  
  9. #define enA 9
  10. #define in1 4
  11. #define in2 5
  12. #define enB 10
  13. #define in3 6
  14. #define in4 7
  15.  
  16. int xAxis, yAxis;
  17. unsigned int x = 0;
  18. unsigned int y = 0;
  19.  
  20. int motorSpeedA = 0;
  21. int motorSpeedB = 0;
  22.  
  23. void setup() {
  24. pinMode(enA, OUTPUT);
  25. pinMode(enB, OUTPUT);
  26. pinMode(in1, OUTPUT);
  27. pinMode(in2, OUTPUT);
  28. pinMode(in3, OUTPUT);
  29. pinMode(in4, OUTPUT);
  30.  
  31. Serial.begin(38400); // Default communication rate of the Bluetooth module
  32. }
  33.  
  34. void loop() {
  35. // Default value – no movement when the Joystick stays in the center
  36. x = 510 / 4;
  37. y = 510 / 4;
  38.  
  39. // Read the incoming data from the Joystick, or the master Bluetooth device
  40. while (Serial.available() >= 2) {
  41. x = Serial.read();
  42. delay(10);
  43. y = Serial.read();
  44. }
  45. delay(10);
  46. // Convert back the 0 – 255 range to 0 – 1023, suitable for motor control code below
  47. xAxis = x*4;
  48. yAxis = y*4;
  49.  
  50. // Y-axis used for forward and backward control
  51. if (yAxis < 470) {
  52. // Set Motor A backward
  53. digitalWrite(in1, HIGH);
  54. digitalWrite(in2, LOW);
  55. // Set Motor B backward
  56. digitalWrite(in3, HIGH);
  57. digitalWrite(in4, LOW);
  58. // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
  59. motorSpeedA = map(yAxis, 470, 0, 0, 255);
  60. motorSpeedB = map(yAxis, 470, 0, 0, 255);
  61. }
  62. else if (yAxis > 550) {
  63. // Set Motor A forward
  64. digitalWrite(in1, LOW);
  65. digitalWrite(in2, HIGH);
  66. // Set Motor B forward
  67. digitalWrite(in3, LOW);
  68. digitalWrite(in4, HIGH);
  69. // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
  70. motorSpeedA = map(yAxis, 550, 1023, 0, 255);
  71. motorSpeedB = map(yAxis, 550, 1023, 0, 255);
  72. }
  73. // If joystick stays in middle the motors are not moving
  74. else {
  75. motorSpeedA = 0;
  76. motorSpeedB = 0;
  77. }
  78.  
  79. // X-axis used for left and right control
  80. if (xAxis < 470) {
  81. // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
  82. int xMapped = map(xAxis, 470, 0, 0, 255);
  83. // Move to left – decrease left motor speed, increase right motor speed
  84. motorSpeedA = motorSpeedA – xMapped;
  85. motorSpeedB = motorSpeedB + xMapped;
  86. // Confine the range from 0 to 255
  87. if (motorSpeedA < 0) {
  88. motorSpeedA = 0;
  89. }
  90. if (motorSpeedB > 255) {
  91. motorSpeedB = 255;
  92. }
  93. }
  94. if (xAxis > 550) {
  95. // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
  96. int xMapped = map(xAxis, 550, 1023, 0, 255);
  97. // Move right – decrease right motor speed, increase left motor speed
  98. motorSpeedA = motorSpeedA + xMapped;
  99. motorSpeedB = motorSpeedB – xMapped;
  100. // Confine the range from 0 to 255
  101. if (motorSpeedA > 255) {
  102. motorSpeedA = 255;
  103. }
  104. if (motorSpeedB < 0) {
  105. motorSpeedB = 0;
  106. }
  107. }
  108. // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
  109. if (motorSpeedA < 70) {
  110. motorSpeedA = 0;
  111. }
  112. if (motorSpeedB < 70) {
  113. motorSpeedB = 0;
  114. }
  115. analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
  116. analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
  117. }

使用智慧手機和自訂構建 Android 應用程式的 Arduino 機器人車控制

接下來,我們來看看如何使用自訂構建 Android 應用程式來控制我們的 Arduino 機器人車。
機器人車的電路原理圖與上一個例子完全相同,HC-05 藍牙模式設定為從裝置。

Arduino Robot Car Android Smartphone Control

另一方面,使用麻省理工學院 App Inventor 線上應用程式,我們將構建自己的 Android 應用程式,以及它的外觀。

所以基本上應用程式模擬一個操縱桿,哪個外觀是由兩個圖像或圖像精靈組成的。

MIT App Inventor Joystick Application Arduino Robot Car Control Tutorial

如果我們來看看這個應用程序的塊,我們可以看到當拖動操縱桿小精靈時,操縱桿球的圖像被移動到我們手指的當前位置,同時我們發送X和Y 藍牙到Arduino車的價值。MIT App Inventor Joystick Application Blocks

Arduino 以與上一個示例中相同的方式使用 Serial.read 函數來接受這些值。

  1. // Read the incoming data from the Smartphone Android App
  2. while (Serial.available() >= 2) {
  3. x = Serial.read();
  4. delay(10);
  5. y = Serial.read();
  6. }

我們此處需要另外做的是將接收到的X和Y值從智慧手機轉換為0到1023範圍,適用於下面的電機控制代碼。
這些值取決於畫布大小,我從我的應用程式得到的X和Y值是從60到220,使用map()函數我很容易地轉換它們。

  1. // Makes sure we receive corrent values
  2. if (x > 60 & x < 220) {
  3. xAxis = map(x, 220, 60, 1023, 0); // Convert the smartphone X and Y values to 0 – 1023 range, suitable motor for the motor control code below
  4. }
  5. if (y > 60 & y < 220) {
  6. yAxis = map(y, 220, 60, 0, 1023);
  7. }

在應用程式塊,我們還可以看到,當圖像精靈被觸摸時,操縱桿球移回畫布的中心,並且適當的值被傳送到到汽車以停止搬移。
您可以在網站文章中找到並下載此應用程式,以及操縱桿的兩個圖像,以便您可以自行構建或修改此應用程式。

您可以下載下面的Android應用程式,以及操縱桿的兩個圖像:

  1. /*
  2. Arduino Robot Car Wireless Control using the HC-05 Bluetooth and custom-build Android app
  3.  
  4. == SLAVE DEVICE – Arduino robot car ==
  5.  
  6. by Dejan Nedelkovski, www.HowToMechatronics.com
  7. */
  8.  
  9. #define enA 9
  10. #define in1 4
  11. #define in2 5
  12. #define enB 10
  13. #define in3 6
  14. #define in4 7
  15.  
  16. int xAxis, yAxis;
  17. int x = 0;
  18. int y = 0;
  19.  
  20. int motorSpeedA = 0;
  21. int motorSpeedB = 0;
  22.  
  23. void setup() {
  24. pinMode(enA, OUTPUT);
  25. pinMode(enB, OUTPUT);
  26. pinMode(in1, OUTPUT);
  27. pinMode(in2, OUTPUT);
  28. pinMode(in3, OUTPUT);
  29. pinMode(in4, OUTPUT);
  30.  
  31. Serial.begin(38400); // Default communication rate of the Bluetooth module
  32. }
  33.  
  34. void loop() {
  35. // Default value – no movement when the Joystick stays in the center
  36. xAxis = 510;
  37. yAxis = 510;
  38.  
  39. // Read the incoming data from the Smartphone Android App
  40. while (Serial.available() >= 2) {
  41. x = Serial.read();
  42. delay(10);
  43. y = Serial.read();
  44. }
  45. delay(10);
  46.  
  47. // Makes sure we receive corrent values
  48. if (x > 60 & x < 220) {
  49. xAxis = map(x, 220, 60, 1023, 0); // Convert the smartphone X and Y values to 0 – 1023 range, suitable motor for the motor control code below
  50. }
  51. if (y > 60 & y < 220) {
  52. yAxis = map(y, 220, 60, 0, 1023);
  53. }
  54.  
  55. // Y-axis used for forward and backward control
  56. if (yAxis < 470) {
  57. // Set Motor A backward
  58. digitalWrite(in1, HIGH);
  59. digitalWrite(in2, LOW);
  60. // Set Motor B backward
  61. digitalWrite(in3, HIGH);
  62. digitalWrite(in4, LOW);
  63. // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
  64. motorSpeedA = map(yAxis, 470, 0, 0, 255);
  65. motorSpeedB = map(yAxis, 470, 0, 0, 255);
  66. }
  67. else if (yAxis > 550) {
  68. // Set Motor A forward
  69. digitalWrite(in1, LOW);
  70. digitalWrite(in2, HIGH);
  71. // Set Motor B forward
  72. digitalWrite(in3, LOW);
  73. digitalWrite(in4, HIGH);
  74. // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
  75. motorSpeedA = map(yAxis, 550, 1023, 0, 255);
  76. motorSpeedB = map(yAxis, 550, 1023, 0, 255);
  77. }
  78. // If joystick stays in middle the motors are not moving
  79. else {
  80. motorSpeedA = 0;
  81. motorSpeedB = 0;
  82. }
  83.  
  84. // X-axis used for left and right control
  85. if (xAxis < 470) {
  86. // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
  87. int xMapped = map(xAxis, 470, 0, 0, 255);
  88. // Move to left – decrease left motor speed, increase right motor speed
  89. motorSpeedA = motorSpeedA – xMapped;
  90. motorSpeedB = motorSpeedB + xMapped;
  91. // Confine the range from 0 to 255
  92. if (motorSpeedA < 0) {
  93. motorSpeedA = 0;
  94. }
  95. if (motorSpeedB > 255) {
  96. motorSpeedB = 255;
  97. }
  98. }
  99. if (xAxis > 550) {
  100. // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
  101. int xMapped = map(xAxis, 550, 1023, 0, 255);
  102. // Move right – decrease right motor speed, increase left motor speed
  103. motorSpeedA = motorSpeedA + xMapped;
  104. motorSpeedB = motorSpeedB – xMapped;
  105. // Confine the range from 0 to 255
  106. if (motorSpeedA > 255) {
  107. motorSpeedA = 255;
  108. }
  109. if (motorSpeedB < 0) {
  110. motorSpeedB = 0;
  111. }
  112. }
  113. // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
  114. if (motorSpeedA < 70) {
  115. motorSpeedA = 0;
  116. }
  117. if (motorSpeedB < 70) {
  118. motorSpeedB = 0;
  119. }
  120. analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
  121. analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
  122. }
 

使用NRF24L01收發器模組的Arduino機器人車無線控制

現在我們可以繼續下一個方法,使用NRF24L01收發模組對Arduino機器人車進行無線控制。

Arduino Robot Car NRF24L01 Transceiver Module Tutorial

這是電路原理圖。
我們可以注意到,這些模組使用SPI通信,因此與上一個示例相比,我將L298N驅動的Enable A和Enable B引腳搬移到Arduino板的2號和3號引腳。NRF24L01 Wireless Arduino Robot Car Control - Circuit Schematic

您可以在以下亞馬遜連結上獲得NRF24L01模組。
原始碼

對於這個例子,我們需要安裝RF24庫。 以與前一個例子相似的模式,在定義一些引腳並將模組設定為發射器後,我們讀取操縱桿的X和Y值,並將其傳送到到Arduino機器人車上的另一個NRF24L01模組。

首先我們可以注意到,模擬讀數是Strings,它使用string.toCharArray()函數被放入一個字元陣列中。 然後使用radio.write()函數,我們將該字元陣列資料傳送到到另一個模組。

變送器代碼:

  1. /*
  2. Arduino Robot Car Wireless Control using the NRF24L01 Transceiver module
  3.  
  4. == Transmitter – Joystick ==
  5.  
  6. by Dejan Nedelkovski, www.HowToMechatronics.com
  7.  
  8. Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
  9. */
  10.  
  11. #include <SPI.h>
  12. #include <nRF24L01.h>
  13. #include <RF24.h>
  14.  
  15. RF24 radio(8, 9); // CE, CSN
  16. const byte address[6] = "00001";
  17.  
  18. char xyData[32] = "";
  19. String xAxis, yAxis;
  20.  
  21. void setup() {
  22. Serial.begin(9600);
  23. radio.begin();
  24. radio.openWritingPipe(address);
  25. radio.setPALevel(RF24_PA_MIN);
  26. radio.stopListening();
  27. }
  28.  
  29. void loop() {
  30.  
  31. xAxis = analogRead(A0); // Read Joysticks X-axis
  32. yAxis = analogRead(A1); // Read Joysticks Y-axis
  33. // X value
  34. xAxis.toCharArray(xyData, 5); // Put the String (X Value) into a character array
  35. radio.write(&xyData, sizeof(xyData)); // Send the array data (X value) to the other NRF24L01 modile
  36. // Y value
  37. yAxis.toCharArray(xyData, 5);
  38. radio.write(&xyData, sizeof(xyData));
  39. delay(20);
  40. }

另一方面。
在Arduino機器人車上,在將模組定義為接收器之後,我們使用radio.read()函數接受資料。
然後使用atoi()函數,將接收到的資料,或操縱桿的X和Y值轉換為

  1. // Code from the Arduino Robot Car – NRF24L01 example
  2.  
  3. if (radio.available()) { // If the NRF240L01 module received data
  4. radio.read(&receivedData, sizeof(receivedData)); // Read the data and put it into character array
  5. xAxis = atoi(&receivedData[0]); // Convert the data from the character array (received X value) into integer
  6. delay(10);
  7. radio.read(&receivedData, sizeof(receivedData));
  8. yAxis = atoi(&receivedData[0]);
  9. delay(10);
  10. }

這很簡單,但當然如我已經說過的,如果你需要更多的細節,如何連線和設定模組,你可以隨時檢查我的特定教學。

接收碼:

  1. /*
  2. Arduino Robot Car Wireless Control using the NRF24L01 Transceiver module
  3.  
  4. == Receiver – Arduino robot car ==
  5.  
  6. by Dejan Nedelkovski, www.HowToMechatronics.com
  7.  
  8. Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
  9. */
  10. #include <SPI.h>
  11. #include <nRF24L01.h>
  12. #include <RF24.h>
  13.  
  14. #define enA 2 // Note: Pin 9 in previous video ( pin 10 is used for the SPI communication of the NRF24L01)
  15. #define in1 4
  16. #define in2 5
  17. #define enB 3 // Note: Pin 10 in previous video
  18. #define in3 6
  19. #define in4 7
  20.  
  21. RF24 radio(8, 9); // CE, CSN
  22. const byte address[6] = "00001";
  23.  
  24. char receivedData[32] = "";
  25. int xAxis, yAxis;
  26.  
  27. int motorSpeedA = 0;
  28. int motorSpeedB = 0;
  29.  
  30. void setup() {
  31. pinMode(enA, OUTPUT);
  32. pinMode(enB, OUTPUT);
  33. pinMode(in1, OUTPUT);
  34. pinMode(in2, OUTPUT);
  35. pinMode(in3, OUTPUT);
  36. pinMode(in4, OUTPUT);
  37. Serial.begin(9600);
  38. radio.begin();
  39. radio.openReadingPipe(0, address);
  40. radio.setPALevel(RF24_PA_MIN);
  41. radio.startListening();
  42. }
  43.  
  44. void loop() {
  45.  
  46. if (radio.available()) { // If the NRF240L01 module received data
  47. radio.read(&receivedData, sizeof(receivedData)); // Read the data and put it into character array
  48. xAxis = atoi(&receivedData[0]); // Convert the data from the character array (received X value) into integer
  49. delay(10);
  50. radio.read(&receivedData, sizeof(receivedData));
  51. yAxis = atoi(&receivedData[0]);
  52. delay(10);
  53. }
  54.  
  55. // Y-axis used for forward and backward control
  56. if (yAxis < 470) {
  57. // Set Motor A backward
  58. digitalWrite(in1, HIGH);
  59. digitalWrite(in2, LOW);
  60. // Set Motor B backward
  61. digitalWrite(in3, HIGH);
  62. digitalWrite(in4, LOW);
  63. // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
  64. motorSpeedA = map(yAxis, 470, 0, 0, 255);
  65. motorSpeedB = map(yAxis, 470, 0, 0, 255);
  66. }
  67. else if (yAxis > 550) {
  68. // Set Motor A forward
  69. digitalWrite(in1, LOW);
  70. digitalWrite(in2, HIGH);
  71. // Set Motor B forward
  72. digitalWrite(in3, LOW);
  73. digitalWrite(in4, HIGH);
  74. // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
  75. motorSpeedA = map(yAxis, 550, 1023, 0, 255);
  76. motorSpeedB = map(yAxis, 550, 1023, 0, 255);
  77. }
  78. // If joystick stays in middle the motors are not moving
  79. else {
  80. motorSpeedA = 0;
  81. motorSpeedB = 0;
  82. }
  83.  
  84. // X-axis used for left and right control
  85. if (xAxis < 470) {
  86. // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
  87. int xMapped = map(xAxis, 470, 0, 0, 255);
  88. // Move to left – decrease left motor speed, increase right motor speed
  89. motorSpeedA = motorSpeedA – xMapped;
  90. motorSpeedB = motorSpeedB + xMapped;
  91. // Confine the range from 0 to 255
  92. if (motorSpeedA < 0) {
  93. motorSpeedA = 0;
  94. }
  95. if (motorSpeedB > 255) {
  96. motorSpeedB = 255;
  97. }
  98. }
  99. if (xAxis > 550) {
  100. // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
  101. int xMapped = map(xAxis, 550, 1023, 0, 255);
  102. // Move right – decrease right motor speed, increase left motor speed
  103. motorSpeedA = motorSpeedA + xMapped;
  104. motorSpeedB = motorSpeedB – xMapped;
  105. // Confine the range from 0 to 255
  106. if (motorSpeedA > 255) {
  107. motorSpeedA = 255;
  108. }
  109. if (motorSpeedB < 0) {
  110. motorSpeedB = 0;
  111. }
  112. }
  113. // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
  114. if (motorSpeedA < 70) {
  115. motorSpeedA = 0;
  116. }
  117. if (motorSpeedB < 70) {
  118. motorSpeedB = 0;
  119. }
  120. analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
  121. analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
  122. }

使用HC-12長距離收發器的Arduino機器人車無線控制

對於Arduino機器人車的無線控制的最後一種方法,我們將使用HC-12遠程收發器模塊。 這些模塊可以相互通信,距離可達1.8公里。

該示例的電路原理圖與HC-05藍牙模塊的電路原理圖幾乎相同,因為它們通過串行端口使用與Arduino進行通信的相同方法。

 

Arduino Robot Car Wireless Control Using HC-12 Long Range Transceiver

您可以在以下亞馬遜連結上取得HC-12收發器模組。
原始碼

操縱桿代碼與藍牙通信的代碼完全相同。 我們只是讀取操縱桿的模擬值,並使用Serial.write()函數將它們傳送到到另一個模組。

變送器代碼:

  1. /*
  2. Arduino Robot Car Wireless Control using the HC-12 long range wireless module
  3.  
  4. == Transmitter – Joystick ==
  5.  
  6. by Dejan Nedelkovski, www.HowToMechatronics.com
  7. */
  8.  
  9. int xAxis, yAxis;
  10.  
  11. void setup() {
  12. Serial.begin(9600); // Default communication rate of the Bluetooth module
  13. }
  14.  
  15. void loop() {
  16. xAxis = analogRead(A0); // Read Joysticks X-axis
  17. yAxis = analogRead(A1); // Read Joysticks Y-axis
  18.  
  19. // Send the values via the serial port to the slave HC-05 Bluetooth device
  20. Serial.write(xAxis/4); // Dividing by 4 for converting from 0 – 1023 to 0 – 256, (1 byte) range
  21. Serial.write(yAxis/4);
  22. delay(20);
  23. }

另一方面,使用while()迴圈,我們等待資料到達,然後使用Serial.read()函數讀取它,並將其轉換回0到1023範圍,適用於下面的電機控制代碼。

接收碼:

  1. /*
  2. Arduino Robot Car Wireless Control using the HC-12 long range wireless module
  3.  
  4. == Receiver – Arduino robot car ==
  5.  
  6. by Dejan Nedelkovski, www.HowToMechatronics.com
  7. */
  8.  
  9. #define enA 9
  10. #define in1 4
  11. #define in2 5
  12. #define enB 10
  13. #define in3 6
  14. #define in4 7
  15.  
  16. int xAxis, yAxis;
  17. int x = 0;
  18. int y = 0;
  19.  
  20. int motorSpeedA = 0;
  21. int motorSpeedB = 0;
  22.  
  23. void setup() {
  24. pinMode(enA, OUTPUT);
  25. pinMode(enB, OUTPUT);
  26. pinMode(in1, OUTPUT);
  27. pinMode(in2, OUTPUT);
  28. pinMode(in3, OUTPUT);
  29. pinMode(in4, OUTPUT);
  30.  
  31. Serial.begin(9600); // Default communication rate of the Bluetooth module
  32. }
  33.  
  34. void loop() {
  35. // Default value – no movement when the Joystick stays in the center
  36. xAxis = 510;
  37. yAxis = 510;
  38.  
  39. // Read the incoming data from the
  40. while (Serial.available() == 0) {}
  41. x = Serial.read();
  42. delay(10);
  43. y = Serial.read();
  44. delay(10);
  45.  
  46. // Convert back the 0 – 255 range to 0 – 1023, suitable for motor control code below
  47. xAxis = x * 4;
  48. yAxis = y * 4;
  49.  
  50. // Y-axis used for forward and backward control
  51. if (yAxis < 470) {
  52. // Set Motor A backward
  53. digitalWrite(in1, HIGH);
  54. digitalWrite(in2, LOW);
  55. // Set Motor B backward
  56. digitalWrite(in3, HIGH);
  57. digitalWrite(in4, LOW);
  58. // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
  59. motorSpeedA = map(yAxis, 470, 0, 0, 255);
  60. motorSpeedB = map(yAxis, 470, 0, 0, 255);
  61. }
  62. else if (yAxis > 550) {
  63. // Set Motor A forward
  64. digitalWrite(in1, LOW);
  65. digitalWrite(in2, HIGH);
  66. // Set Motor B forward
  67. digitalWrite(in3, LOW);
  68. digitalWrite(in4, HIGH);
  69. // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
  70. motorSpeedA = map(yAxis, 550, 1023, 0, 255);
  71. motorSpeedB = map(yAxis, 550, 1023, 0, 255);
  72. }
  73. // If joystick stays in middle the motors are not moving
  74. else {
  75. motorSpeedA = 0;
  76. motorSpeedB = 0;
  77. }
  78.  
  79. // X-axis used for left and right control
  80. if (xAxis < 470) {
  81. // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
  82. int xMapped = map(xAxis, 470, 0, 0, 255);
  83. // Move to left – decrease left motor speed, increase right motor speed
  84. motorSpeedA = motorSpeedA – xMapped;
  85. motorSpeedB = motorSpeedB + xMapped;
  86. // Confine the range from 0 to 255
  87. if (motorSpeedA < 0) {
  88. motorSpeedA = 0;
  89. }
  90. if (motorSpeedB > 255) {
  91. motorSpeedB = 255;
  92. }
  93. }
  94. if (xAxis > 550) {
  95. // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
  96. int xMapped = map(xAxis, 550, 1023, 0, 255);
  97. // Move right – decrease right motor speed, increase left motor speed
  98. motorSpeedA = motorSpeedA + xMapped;
  99. motorSpeedB = motorSpeedB – xMapped;
  100. // Confine the range from 0 to 255
  101. if (motorSpeedA > 255) {
  102. motorSpeedA = 255;
  103. }
  104. if (motorSpeedB < 0) {
  105. motorSpeedB = 0;
  106. }
  107. }
  108. // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
  109. if (motorSpeedA < 70) {
  110. motorSpeedA = 0;
  111. }
  112. if (motorSpeedB < 70) {
  113. motorSpeedB = 0;
  114. }
  115. analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
  116. analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
  117. }

所以這幾乎是本教學的一切。
這是有趣的
使用HC-05藍牙,NRF24L01和HC-12收發器模塊【Arduino機器人車載無線控制】。

關於

發佈留言


【網路技術密訓基地】莫忘記初衷! (堅持!直到成功!)

【洪總教頭】曾說:過去的理念已經實現的叫做:【成就】,而還未實現的就叫:【夢想】!我們從來沒因已有的【成就】而放棄【夢想】!
唯有你我知道的秘密,就不可能到處去說給別人聽,因為:到處宣揚的就不叫做【密技】!

E-Mail:ster168ster@gmail.com