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

主裝置或操縱桿上的代碼非常簡單。 我們只需要讀取操縱桿的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. // Read the incoming data from the Joystick, or the master Bluetooth device
  3. while (Serial.available() >= 2) {
  4. x = Serial.read();
  5. delay(10);
  6. y = Serial.read();
  7. }

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

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

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

完成HC-05從代碼:

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

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

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

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

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

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

接收碼:

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

使用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. == Transmitter – Joystick ==
  4. by Dejan Nedelkovski, www.HowToMechatronics.com
  5. */
  6. int xAxis, yAxis;
  7. void setup() {
  8. Serial.begin(9600); // Default communication rate of the Bluetooth module
  9. }
  10. void loop() {
  11. xAxis = analogRead(A0); // Read Joysticks X-axis
  12. yAxis = analogRead(A1); // Read Joysticks Y-axis
  13. // Send the values via the serial port to the slave HC-05 Bluetooth device
  14. Serial.write(xAxis/4); // Dividing by 4 for converting from 0 – 1023 to 0 – 256, (1 byte) range
  15. Serial.write(yAxis/4);
  16. delay(20);
  17. }

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

接收碼:

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

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

Random Posts

發佈留言