Manufaktur industri
Industri Internet of Things | bahan industri | Pemeliharaan dan Perbaikan Peralatan | Pemrograman industri |
home  MfgRobots >> Manufaktur industri >  >> Manufacturing Technology >> Proses manufaktur

Kendalikan Robot Car Arduino Secara Nirkabel dengan HC-05, NRF24L01, dan HC-12

Dalam tutorial ini kita akan mempelajari cara mengontrol robot car Arduino yang telah kita buat dalam video sebelumnya secara nirkabel. Saya akan menunjukkan tiga metode kontrol nirkabel yang berbeda, menggunakan modul Bluetooth HC-05, modul transceiver NRF24L01 dan modul transceiver jarak jauh HC-12, serta menggunakan Smartphone dan aplikasi Android buatan sendiri. Anda dapat menonton video berikut atau membaca tutorial tertulis di bawahnya untuk lebih banyak detail.

Saya sudah memiliki tutorial tentang cara menghubungkan dan menggunakan masing-masing modul ini dengan papan Arduino, jadi jika Anda memerlukan lebih banyak detail Anda dapat selalu memeriksa mereka. Tautan ke masing-masing dapat ditemukan di bawah dalam artikel.

Kontrol Robot Car Arduino Menggunakan Modul Bluetooth HC-05

Kita akan memulai dengan komunikasi Bluetooth, dan untuk tujuan itu kita memerlukan dua modul Bluetooth HC-05 yang perlu dikonfigurasi sebagai perangkat master dan slave.

Kendalikan Robot Car Arduino Secara Nirkabel dengan HC-05, NRF24L01, dan HC-12

Kita dapat dengan mudah melakukannya dengan menggunakan perintah AT, dan saya mengatur joystick sebagai master dan robot car Arduino sebagai slave. Berikut adalah skema rangkaian lengkap untuk contoh ini:

Kendalikan Robot Car Arduino Secara Nirkabel dengan HC-05, NRF24L01, dan HC-12

Anda dapat mendapatkan komponen yang diperlukan untuk contoh ini dari tautan di bawah ini:

  • Modul Bluetooth HC-05 ………….…
  • Modul Joystick ………………………….
  • Baterai 18650 …………………………..
  • Charger Baterai 18650…………………
  • Driver L298N ……………………………..
  • Motor DC Torsi Tinggi 12V …………
  • Papan Arduino ……………………………

Kode Sumber

Kita akan menggunakan kode yang sama dari tutorial sebelumnya, tempat kita mengontrol robot car Arduino secara langsung menggunakan joystick, dan kita akan melakukan beberapa modifikasi padanya.

Kode Master HC-05:

/* Arduino Robot Car Wireless Control using the HC-05 Bluetooth == MASTER DEVICE - Joystick == by Dejan Nedelkovski, www.HowToMechatronics.com */ int xAxis, yAxis; void setup() { Serial.begin(38400); // Default communication rate of the Bluetooth module } void loop() { xAxis = analogRead(A0); // Read Joysticks X-axis yAxis = analogRead(A1); // Read Joysticks Y-axis // Send the values via the serial port to the slave HC-05 Bluetooth device Serial.write(xAxis/4); // Dividing by 4 for converting from 0 - 1023 to 0 - 256, (1 byte) range Serial.write(yAxis/4); delay(20); }Code language: Arduino (arduino)

Kode pada perangkat master, atau joystick cukup sederhana. Kita hanya perlu membaca nilai X dan Y joystick, yang sebenarnya mengatur kecepatan motor, dan mengirimkannya melalui port serial ke perangkat Bluetooth HC-05 slave. Kita dapat mencatat di sini bahwa nilai analog joystick dari 0 hingga 1023 dikonversi menjadi nilai dari 0 hingga 255 dengan membaginya dengan 4.

Kita melakukan ini karena rentang itu, dari 0 hingga 255, dapat dikirim melalui perangkat Bluetooth sebagai 1 byte yang lebih mudah diterima di sisi lain, atau di robot car Arduino.

Jadi di sini, jika serial telah menerima 2 byte, nilai X dan Y, menggunakan fungsi Serial.read() kita akan membaca keduanya.

// Code from the Arduino Robot Car // Read the incoming data from the Joystick, or the master Bluetooth device while (Serial.available() >= 2) { x = Serial.read(); delay(10); y = Serial.read(); }Code language: Arduino (arduino)

Sekarang kita hanya perlu mengonversi nilai-nilai kembali ke rentang 0 hingga 1023, yang cocok untuk kode kontrol motor di bawah ini, yang sudah kita jelaskan cara kerjanya dalam video sebelumnya.

// Code from the Arduino Robot Car // Convert back the 0 - 255 range to 0 - 1023, suitable for motor control code below xAxis = x*4; yAxis = y*4;Code language: Arduino (arduino)

Catatan cepat bahwa saat mengunggah kode, kita perlu memutuskan pin RX dan TX dari papan Arduino.

Kode Lengkap HC-05 Slave:

/* Arduino Robot Car Wireless Control using the HC-05 Bluetooth == SLAVE DEVICE - Arduino robot car == by Dejan Nedelkovski, www.HowToMechatronics.com */ #define enA 9 #define in1 4 #define in2 5 #define enB 10 #define in3 6 #define in4 7 int xAxis, yAxis; unsigned int x = 0; unsigned int y = 0; int motorSpeedA = 0; int motorSpeedB = 0; void setup() { pinMode(enA, OUTPUT); pinMode(enB, OUTPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); Serial.begin(38400); // Default communication rate of the Bluetooth module } void loop() { // Default value - no movement when the Joystick stays in the center x = 510 / 4; y = 510 / 4; // Read the incoming data from the Joystick, or the master Bluetooth device while (Serial.available() >= 2) { x = Serial.read(); delay(10); y = Serial.read(); } delay(10); // Convert back the 0 - 255 range to 0 - 1023, suitable for motor control code below xAxis = x*4; yAxis = y*4; // Y-axis used for forward and backward control if (yAxis < 470) { // Set Motor A backward digitalWrite(in1, HIGH); digitalWrite(in2, LOW); // Set Motor B backward digitalWrite(in3, HIGH); digitalWrite(in4, LOW); // 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 motorSpeedA = map(yAxis, 470, 0, 0, 255); motorSpeedB = map(yAxis, 470, 0, 0, 255); } else if (yAxis > 550) { // Set Motor A forward digitalWrite(in1, LOW); digitalWrite(in2, HIGH); // Set Motor B forward digitalWrite(in3, LOW); digitalWrite(in4, HIGH); // 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 motorSpeedA = map(yAxis, 550, 1023, 0, 255); motorSpeedB = map(yAxis, 550, 1023, 0, 255); } // If joystick stays in middle the motors are not moving else { motorSpeedA = 0; motorSpeedB = 0; } // X-axis used for left and right control if (xAxis < 470) { // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value int xMapped = map(xAxis, 470, 0, 0, 255); // Move to left - decrease left motor speed, increase right motor speed motorSpeedA = motorSpeedA - xMapped; motorSpeedB = motorSpeedB + xMapped; // Confine the range from 0 to 255 if (motorSpeedA < 0) { motorSpeedA = 0; } if (motorSpeedB > 255) { motorSpeedB = 255; } } if (xAxis > 550) { // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value int xMapped = map(xAxis, 550, 1023, 0, 255); // Move right - decrease right motor speed, increase left motor speed motorSpeedA = motorSpeedA + xMapped; motorSpeedB = motorSpeedB - xMapped; // Confine the range from 0 to 255 if (motorSpeedA > 255) { motorSpeedA = 255; } if (motorSpeedB < 0) { motorSpeedB = 0; } } // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70) if (motorSpeedA < 70) { motorSpeedA = 0; } if (motorSpeedB < 70) { motorSpeedB = 0; } analogWrite(enA, motorSpeedA); // Send PWM signal to motor A analogWrite(enB, motorSpeedB); // Send PWM signal to motor B }Code language: Arduino (arduino)

Kontrol Robot Car Arduino Menggunakan Smartphone dan Aplikasi Android Buatan Sendiri

Selanjutnya, mari kita lihat bagaimana kita dapat mengontrol robot car Arduino kita menggunakan aplikasi Android buatan sendiri. Skema rangkaian robot car sama persis dengan contoh sebelumnya, dengan mode HC-05 Bluetooth diatur sebagai perangkat slave.

Kendalikan Robot Car Arduino Secara Nirkabel dengan HC-05, NRF24L01, dan HC-12

Di sisi lain, menggunakan aplikasi online MIT App Inventor kita akan membangun aplikasi Android kita sendiri, dan ini adalah tampilannya.

Jadi pada dasarnya aplikasi ini mensimulasikan joystick, yang penampilannya dibuat dari dua gambar, atau image sprites.

Kendalikan Robot Car Arduino Secara Nirkabel dengan HC-05, NRF24L01, dan HC-12
Kendalikan Robot Car Arduino Secara Nirkabel dengan HC-05, NRF24L01, dan HC-12

Jika kita melihat blok dari aplikasi ini, kita dapat melihat bahwa ketika sprite joystick di seret, gambar bola joystick dipindahkan ke lokasi jari kita saat ini, dan pada saat yang sama kita mengirim nilai X dan Y melalui Bluetooth ke mobil Arduino.

Nilai-nilai ini diterima oleh Arduino dengan cara yang sama seperti contoh sebelumnya, menggunakan fungsi Serial.read.

// Read the incoming data from the Smartphone Android App while (Serial.available() >= 2) { x = Serial.read(); delay(10); y = Serial.read(); }Code language: Arduino (arduino)

Yang perlu kita lakukan tambahan di sini adalah mengonversi nilai X dan Y yang diterima dari smartphone ke rentang 0 hingga 1023, yang cocok untuk kode kontrol motor di bawah ini. Nilai-nilai ini tergantung pada ukuran kanvas, dan nilai X dan Y yang saya peroleh dari aplikasi saya adalah dari 60 hingga 220, yang dengan mudah saya konversi menggunakan fungsi map().

// Makes sure we receive corrent values if (x > 60 & x < 220) { 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 } if (y > 60 & y < 220) { yAxis = map(y, 220, 60, 0, 1023); }Code language: Arduino (arduino)

Di blok aplikasi, kita juga dapat melihat bahwa ketika sprite image disentuh bola joystick kembali ke tengah kanvas dan nilai yang sesuai dikirim ke mobil untuk berhenti bergerak. Anda dapat menemukan dan mengunduh aplikasi ini di artikel situs web, serta dua gambar joystick sehingga Anda dapat membangun aplikasi Anda sendiri atau memodifikasi aplikasi ini.

Anda dapat mengunduh aplikasi Android di bawah ini, serta dua gambar untuk joystick:

Kendalikan Robot Car Arduino Secara Nirkabel dengan HC-05, NRF24L01, dan HC-12

Arduino_Robot_Car_Joystick_App.apk

1 file(s) 1.62 MB Download Kendalikan Robot Car Arduino Secara Nirkabel dengan HC-05, NRF24L01, dan HC-12

Arduino_Robot_Car_Joystick_App_aia_file

1 file(s) 171.20 KB Download Kendalikan Robot Car Arduino Secara Nirkabel dengan HC-05, NRF24L01, dan HC-12

Joystick App Images

1 file(s) 44.36 KB Download

Kode Arduino lengkap:

/* Arduino Robot Car Wireless Control using the HC-05 Bluetooth and custom-build Android app == SLAVE DEVICE - Arduino robot car == by Dejan Nedelkovski, www.HowToMechatronics.com */ #define enA 9 #define in1 4 #define in2 5 #define enB 10 #define in3 6 #define in4 7 int xAxis, yAxis; int x = 0; int y = 0; int motorSpeedA = 0; int motorSpeedB = 0; void setup() { pinMode(enA, OUTPUT); pinMode(enB, OUTPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); Serial.begin(38400); // Default communication rate of the Bluetooth module } void loop() { // Default value - no movement when the Joystick stays in the center xAxis = 510; yAxis = 510; // Read the incoming data from the Smartphone Android App while (Serial.available() >= 2) { x = Serial.read(); delay(10); y = Serial.read(); } delay(10); // Makes sure we receive corrent values if (x > 60 & x < 220) { 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 } if (y > 60 & y < 220) { yAxis = map(y, 220, 60, 0, 1023); } // Y-axis used for forward and backward control if (yAxis < 470) { // Set Motor A backward digitalWrite(in1, HIGH); digitalWrite(in2, LOW); // Set Motor B backward digitalWrite(in3, HIGH); digitalWrite(in4, LOW); // 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 motorSpeedA = map(yAxis, 470, 0, 0, 255); motorSpeedB = map(yAxis, 470, 0, 0, 255); } else if (yAxis > 550) { // Set Motor A forward digitalWrite(in1, LOW); digitalWrite(in2, HIGH); // Set Motor B forward digitalWrite(in3, LOW); digitalWrite(in4, HIGH); // 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 motorSpeedA = map(yAxis, 550, 1023, 0, 255); motorSpeedB = map(yAxis, 550, 1023, 0, 255); } // If joystick stays in middle the motors are not moving else { motorSpeedA = 0; motorSpeedB = 0; } // X-axis used for left and right control if (xAxis < 470) { // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value int xMapped = map(xAxis, 470, 0, 0, 255); // Move to left - decrease left motor speed, increase right motor speed motorSpeedA = motorSpeedA - xMapped; motorSpeedB = motorSpeedB + xMapped; // Confine the range from 0 to 255 if (motorSpeedA < 0) { motorSpeedA = 0; } if (motorSpeedB > 255) { motorSpeedB = 255; } } if (xAxis > 550) { // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value int xMapped = map(xAxis, 550, 1023, 0, 255); // Move right - decrease right motor speed, increase left motor speed motorSpeedA = motorSpeedA + xMapped; motorSpeedB = motorSpeedB - xMapped; // Confine the range from 0 to 255 if (motorSpeedA > 255) { motorSpeedA = 255; } if (motorSpeedB < 0) { motorSpeedB = 0; } } // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70) if (motorSpeedA < 70) { motorSpeedA = 0; } if (motorSpeedB < 70) { motorSpeedB = 0; } analogWrite(enA, motorSpeedA); // Send PWM signal to motor A analogWrite(enB, motorSpeedB); // Send PWM signal to motor B }Code language: Arduino (arduino)

Kontrol Robot Car Arduino Secara Nirkabel Menggunakan Modul Transceiver NRF24L01

Sekarang kita dapat melanjutkan ke metode berikutnya, kontrol nirkabel robot car Arduino menggunakan modul transceiver NRF24L01.

Kendalikan Robot Car Arduino Secara Nirkabel dengan HC-05, NRF24L01, dan HC-12
Kendalikan Robot Car Arduino Secara Nirkabel dengan HC-05, NRF24L01, dan HC-12

Berikut adalah skema rangkaian. Kita dapat mencatat bahwa modul ini menggunakan komunikasi SPI, jadi dibandingkan dengan contoh sebelumnya, saya harus memindahkan pin Enable A dan Enable B driver L298N ke pin nomor 2 dan 3 dari papan Arduino. Anda dapat mendapatkan modul NRF24L01 di tautan Amazon berikut.

Kode Sumber

Untuk contoh ini kita perlu menginstal library RF24. Dengan cara yang sama seperti contoh sebelumnya, setelah menentukan beberapa pin dan menyiapkan modul sebagai transmitter, kita membaca nilai X dan Y joystick dan mengirimkannya ke modul NRF24L01 lainnya di robot car Arduino.

Pertama kita dapat mencatat bahwa pembacaan analog adalah String, yang menggunakan fungsi string.toCharArray() dimasukkan ke dalam array karakter. Kemudian menggunakan fungsi radio.write() kita mengirim data array karakter tersebut ke modul lainnya.

Kode Transmitter:

/* Arduino Robot Car Wireless Control using the NRF24L01 Transceiver module == Transmitter - Joystick == by Dejan Nedelkovski, www.HowToMechatronics.com Library: TMRh20/RF24, https://github.com/tmrh20/RF24/ */ #include <SPI.h> #include <nRF24L01.h> #include <RF24.h> RF24 radio(8, 9); // CE, CSN const byte address[6] = "00001"; char xyData[32] = ""; String xAxis, yAxis; void setup() { Serial.begin(9600); radio.begin(); radio.openWritingPipe(address); radio.setPALevel(RF24_PA_MIN); radio.stopListening(); } void loop() { xAxis = analogRead(A0); // Read Joysticks X-axis yAxis = analogRead(A1); // Read Joysticks Y-axis // X value xAxis.toCharArray(xyData, 5); // Put the String (X Value) into a character array radio.write(&xyData, sizeof(xyData)); // Send the array data (X value) to the other NRF24L01 modile // Y value yAxis.toCharArray(xyData, 5); radio.write(&xyData, sizeof(xyData)); delay(20); }Code language: Arduino (arduino)

Di sisi lain, di robot car Arduino, setelah menentukan modul sebagai receiver, kita menerima data menggunakan fungsi radio.read(). Kemudian menggunakan fungsi atoi() kita mengonversi data yang diterima, atau nilai X dan Y dari joystick, menjadi nilai integer, yang cocok untuk kode kontrol motor di bawah ini.

// Code from the Arduino Robot Car - NRF24L01 example if (radio.available()) { // If the NRF240L01 module received data radio.read(&receivedData, sizeof(receivedData)); // Read the data and put it into character array xAxis = atoi(&receivedData[0]); // Convert the data from the character array (received X value) into integer delay(10); radio.read(&receivedData, sizeof(receivedData)); yAxis = atoi(&receivedData[0]); delay(10); }Code language: Arduino (arduino)

Itu sederhana, tetapi tentu saja seperti yang sudah saya katakan, jika Anda memerlukan lebih banyak detail cara menghubungkan dan menyiapkan modul, Anda dapat selalu memeriksa tutorial saya yang terkait.

Kode Receiver:

/* Arduino Robot Car Wireless Control using the NRF24L01 Transceiver module == Receiver - Arduino robot car == by Dejan Nedelkovski, www.HowToMechatronics.com Library: TMRh20/RF24, https://github.com/tmrh20/RF24/ */ #include <SPI.h> #include <nRF24L01.h> #include <RF24.h> #define enA 2 // Note: Pin 9 in previous video ( pin 10 is used for the SPI communication of the NRF24L01) #define in1 4 #define in2 5 #define enB 3 // Note: Pin 10 in previous video #define in3 6 #define in4 7 RF24 radio(8, 9); // CE, CSN const byte address[6] = "00001"; char receivedData[32] = ""; int xAxis, yAxis; int motorSpeedA = 0; int motorSpeedB = 0; void setup() { pinMode(enA, OUTPUT); pinMode(enB, OUTPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); Serial.begin(9600); radio.begin(); radio.openReadingPipe(0, address); radio.setPALevel(RF24_PA_MIN); radio.startListening(); } void loop() { if (radio.available()) { // If the NRF240L01 module received data radio.read(&receivedData, sizeof(receivedData)); // Read the data and put it into character array xAxis = atoi(&receivedData[0]); // Convert the data from the character array (received X value) into integer delay(10); radio.read(&receivedData, sizeof(receivedData)); yAxis = atoi(&receivedData[0]); delay(10); } // Y-axis used for forward and backward control if (yAxis < 470) { // Set Motor A backward digitalWrite(in1, HIGH); digitalWrite(in2, LOW); // Set Motor B backward digitalWrite(in3, HIGH); digitalWrite(in4, LOW); // 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 motorSpeedA = map(yAxis, 470, 0, 0, 255); motorSpeedB = map(yAxis, 470, 0, 0, 255); } else if (yAxis > 550) { // Set Motor A forward digitalWrite(in1, LOW); digitalWrite(in2, HIGH); // Set Motor B forward digitalWrite(in3, LOW); digitalWrite(in4, HIGH); // 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 motorSpeedA = map(yAxis, 550, 1023, 0, 255); motorSpeedB = map(yAxis, 550, 1023, 0, 255); } // If joystick stays in middle the motors are not moving else { motorSpeedA = 0; motorSpeedB = 0; } // X-axis used for left and right control if (xAxis < 470) { // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value int xMapped = map(xAxis, 470, 0, 0, 255); // Move to left - decrease left motor speed, increase right motor speed motorSpeedA = motorSpeedA - xMapped; motorSpeedB = motorSpeedB + xMapped; // Confine the range from 0 to 255 if (motorSpeedA < 0) { motorSpeedA = 0; } if (motorSpeedB > 255) { motorSpeedB = 255; } } if (xAxis > 550) { // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value int xMapped = map(xAxis, 550, 1023, 0, 255); // Move right - decrease right motor speed, increase left motor speed motorSpeedA = motorSpeedA + xMapped; motorSpeedB = motorSpeedB - xMapped; // Confine the range from 0 to 255 if (motorSpeedA > 255) { motorSpeedA = 255; } if (motorSpeedB < 0) { motorSpeedB = 0; } } // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70) if (motorSpeedA < 70) { motorSpeedA = 0; } if (motorSpeedB < 70) { motorSpeedB = 0; } analogWrite(enA, motorSpeedA); // Send PWM signal to motor A analogWrite(enB, motorSpeedB); // Send PWM signal to motor B }Code language: Arduino (arduino)

Kontrol Robot Car Arduino Secara Nirkabel Menggunakan Transceiver Jarak Jauh HC-12

Untuk metode terakhir kontrol nirkabel robot car Arduino, kita akan menggunakan modul transceiver jarak jauh HC-12. Modul ini dapat berkomunikasi satu sama lain dengan jarak hingga 1,8 km.

Skema rangkaian untuk contoh ini hampir sama dengan yang untuk modul Bluetooth HC-05, karena mereka menggunakan metode yang sama untuk komunikasi dengan Arduino, melalui port serial.

Kendalikan Robot Car Arduino Secara Nirkabel dengan HC-05, NRF24L01, dan HC-12

Anda dapat mendapatkan modul Transceiver HC-12 di tautan Amazon berikut.

Kode Sumber

Kode Joystick sama persis dengan yang untuk komunikasi Bluetooth. Kita hanya membaca nilai analog joystick dan mengirimkannya ke modul lain menggunakan fungsi Serial.write().

Kode Transmitter:

/* Arduino Robot Car Wireless Control using the HC-12 long range wireless module == Transmitter - Joystick == by Dejan Nedelkovski, www.HowToMechatronics.com */ int xAxis, yAxis; void setup() { Serial.begin(9600); // Default communication rate of the Bluetooth module } void loop() { xAxis = analogRead(A0); // Read Joysticks X-axis yAxis = analogRead(A1); // Read Joysticks Y-axis // Send the values via the serial port to the slave HC-05 Bluetooth device Serial.write(xAxis/4); // Dividing by 4 for converting from 0 - 1023 to 0 - 256, (1 byte) range Serial.write(yAxis/4); delay(20); }Code language: Arduino (arduino)

Di sisi lain, dengan loop while() kita menunggu sampai data tiba, lalu membacanya menggunakan fungsi Serial.read() dan mengonversinya kembali ke rentang 0 hingga 1023, yang cocok untuk kode kontrol motor di bawah ini.

Kode Receiver:

/* Arduino Robot Car Wireless Control using the HC-12 long range wireless module == Receiver - Arduino robot car == by Dejan Nedelkovski, www.HowToMechatronics.com */ #define enA 9 #define in1 4 #define in2 5 #define enB 10 #define in3 6 #define in4 7 int xAxis, yAxis; int x = 0; int y = 0; int motorSpeedA = 0; int motorSpeedB = 0; void setup() { pinMode(enA, OUTPUT); pinMode(enB, OUTPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); Serial.begin(9600); // Default communication rate of the Bluetooth module } void loop() { // Default value - no movement when the Joystick stays in the center xAxis = 510; yAxis = 510; // Read the incoming data from the while (Serial.available() == 0) {} x = Serial.read(); delay(10); y = Serial.read(); delay(10); // Convert back the 0 - 255 range to 0 - 1023, suitable for motor control code below xAxis = x * 4; yAxis = y * 4; // Y-axis used for forward and backward control if (yAxis < 470) { // Set Motor A backward digitalWrite(in1, HIGH); digitalWrite(in2, LOW); // Set Motor B backward digitalWrite(in3, HIGH); digitalWrite(in4, LOW); // 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 motorSpeedA = map(yAxis, 470, 0, 0, 255); motorSpeedB = map(yAxis, 470, 0, 0, 255); } else if (yAxis > 550) { // Set Motor A forward digitalWrite(in1, LOW); digitalWrite(in2, HIGH); // Set Motor B forward digitalWrite(in3, LOW); digitalWrite(in4, HIGH); // 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 motorSpeedA = map(yAxis, 550, 1023, 0, 255); motorSpeedB = map(yAxis, 550, 1023, 0, 255); } // If joystick stays in middle the motors are not moving else { motorSpeedA = 0; motorSpeedB = 0; } // X-axis used for left and right control if (xAxis < 470) { // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value int xMapped = map(xAxis, 470, 0, 0, 255); // Move to left - decrease left motor speed, increase right motor speed motorSpeedA = motorSpeedA - xMapped; motorSpeedB = motorSpeedB + xMapped; // Confine the range from 0 to 255 if (motorSpeedA < 0) { motorSpeedA = 0; } if (motorSpeedB > 255) { motorSpeedB = 255; } } if (xAxis > 550) { // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value int xMapped = map(xAxis, 550, 1023, 0, 255); // Move right - decrease right motor speed, increase left motor speed motorSpeedA = motorSpeedA + xMapped; motorSpeedB = motorSpeedB - xMapped; // Confine the range from 0 to 255 if (motorSpeedA > 255) { motorSpeedA = 255; } if (motorSpeedB < 0) { motorSpeedB = 0; } } // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70) if (motorSpeedA < 70) { motorSpeedA = 0; } if (motorSpeedB < 70) { motorSpeedB = 0; } analogWrite(enA, motorSpeedA); // Send PWM signal to motor A analogWrite(enB, motorSpeedB); // Send PWM signal to motor B }Code language: Arduino (arduino)

Itu kira-kira semua untuk tutorial ini. Jangan ragu untuk bertanya di bagian komentar di bawah.


Proses manufaktur

  1. Membuat Robot Raspberry Pi yang Dikontrol Bluetooth dengan Umpan Balik Suara
  2. Membuat Remote Android Universal dengan Arduino, 1Sheeld & Kontrol IR
  3. Buat Voltmeter Cerdas dengan Arduino & Smartphone – Proyek DIY Mudah
  4. Kontrol LED melalui Bluetooth dengan Arduino – Panduan DIY Sederhana
  5. Kendalikan Rover Arduino dengan Mulus menggunakan Firmata dan Gamepad Xbox One
  6. Smart Parking Counter: Pelacakan Kendaraan Real-Time dengan Arduino, Processing, dan PHP
  7. Kendalikan TV Anda dengan Alexa melalui Arduino IoT Cloud – Panduan Langkah demi Langkah
  8. Membuat Radio FM Kompak dengan Arduino Nano dan RDA8057M
  9. Shield Pemrograman Arduino Nirkabel dengan HC-05 Bluetooth – Tanpa USB
  10. Kendali Jarak Jauh Mobil Menggunakan Arduino Uno & Bluetooth: Panduan Langkah demi Langkah