Arduino Roboterauto kabellos steuern mit HC-05 Bluetooth, NRF24L01 und HC-12 Transceiver Modulen
In diesem Tutorial lernen wir, wie man das Arduino Roboterauto, das wir im vorherigen Video gebaut haben, kabellos steuert. Ich zeige Ihnen drei verschiedene Methoden der drahtlosen Steuerung: mit dem HC-05 Bluetooth-Modul, dem NRF24L01 Transceiver-Modul und dem HC-12 Long-Range Wireless Modul sowie mit einem Smartphone und einer benutzerdefinierten Android-Anwendung. Sie können das folgende Video ansehen oder die schriftliche Anleitung unten lesen für weitere Details. Ich habe bereits Tutorials dazu, wie man jedes dieser Module mit dem Arduino Board verbindet und verwendet. Wenn Sie mehr Details benötigen, können Sie diese immer überprüfen. Die Links zu jedem davon finden Sie unten im Artikel. Wir beginnen mit der Bluetooth-Kommunikation. Dafür benötigen wir zwei HC-05 Bluetooth-Module, die als Master- und Slave-Geräte konfiguriert werden müssen. Das können wir einfach mit AT-Befehlen tun. Ich habe den Joystick als Master und das Arduino Roboterauto als Slave eingestellt. Hier ist der vollständige Schaltplan für dieses Beispiel: Sie erhalten die für dieses Beispiel benötigten Komponenten über die Links unten: Wir verwenden denselben Code aus dem vorherigen Tutorial, wo wir das Arduino Roboterauto direkt mit dem Joystick steuern, und nehmen einige Änderungen daran vor. Der Code am Master-Gerät (Joystick) ist ziemlich einfach. Wir lesen einfach die X- und Y-Werte des Joysticks, die tatsächlich die Motorgeschwindigkeit regeln, und senden sie über den seriellen Port an das Slave HC-05 Bluetooth-Gerät. Beachten Sie, dass die analogen Werte des Joysticks von 0 bis 1023 in Werte von 0 bis 255 umgewandelt werden, indem wir sie durch 4 teilen. Wir tun dies, weil dieser Bereich (0 bis 255) als 1 Byte über das Bluetooth-Gerät gesendet werden kann, was auf der anderen Seite (am Arduino Roboterauto) leichter zu empfangen ist. Hier, wenn der serielle Port die 2 Bytes (X- und Y-Werte) empfangen hat, lesen wir sie mit der Serial.read()-Funktion. Jetzt müssen wir die Werte nur noch in den Bereich 0 bis 1023 zurückkonvertieren, der für den Motorkontrollcode unten geeignet ist, den wir bereits im vorherigen Video erklärt haben. Ein kurzer Hinweis: Beim Hochladen des Codes müssen wir die RX- und TX-Pins des Arduino-Boards trennen. Als Nächstes sehen wir, wie wir unser Arduino Roboterauto mit einer benutzerdefinierten Android-App steuern können. Der Schaltplan des Roboterautos ist genau derselbe wie im vorherigen Beispiel, mit dem HC-05 Bluetooth-Modul als Slave-Gerät. Andererseits erstellen wir mit dem MIT App Inventor Online-Tool unsere eigene Android-App, und so sieht sie aus. Grundlegend simuliert die App einen Joystick, dessen Erscheinung aus zwei Bildern (Image Sprites) besteht. Schauen wir uns die Blöcke dieser App an: Wenn der Joystick-Sprite gezogen wird, wird das Bild des Joystick-Balls an die aktuelle Position unseres Fingers bewegt, und gleichzeitig senden wir die X- und Y-Werte über Bluetooth an das Arduino-Auto. Diese Werte werden vom Arduino auf die gleiche Weise wie im vorherigen Beispiel akzeptiert, mit der Serial.read-Funktion. Was wir hier zusätzlich tun müssen, ist, die empfangenen X- und Y-Werte vom Smartphone in den 0-bis-1023-Bereich umzurechnen, der für den Motorkontrollcode unten geeignet ist. Diese Werte hängen von der Canvas-Größe ab. Die X- und Y-Werte, die ich von meiner App erhielt, lagen zwischen 60 und 220, die ich mit der map()-Funktion leicht umrechnete. In den Anwendungsblöcken sehen wir auch, dass beim Loslassen des Image Sprites der Joystick-Ball zurück in die Mitte des Canvas bewegt wird und entsprechende Werte an das Auto gesendet werden, um anzuhalten. Sie können diese App im Website-Artikel finden und herunterladen, sowie die beiden Bilder des Joysticks, um Ihre eigene App zu bauen oder diese zu modifizieren. Sie können die Android-App unten herunterladen, sowie die beiden Bilder für den Joystick: Jetzt können wir zur nächsten Methode übergehen: kabellose Steuerung des Arduino Roboterautos mit NRF24L01 Transceiver-Modulen. Hier ist der Schaltplan. Wir können feststellen, dass diese Module die SPI-Kommunikation verwenden, daher musste ich im Vergleich zum vorherigen Beispiel die Enable-A- und Enable-B-Pins des L298N-Treibers auf die Pins 2 und 3 des Arduino-Boards verlegen. Sie erhalten das NRF24L01 Modul über den folgenden Amazon-Link. Für dieses Beispiel müssen wir die RF24-Bibliothek installieren. Ähnlich wie im vorherigen Beispiel definieren wir nach der Definition einiger Pins und der Einrichtung des Moduls als Sender die X- und Y-Werte des Joysticks und senden sie an das andere NRF24L01-Modul am Arduino Roboterauto. Zuerst können wir feststellen, dass die analogen Messwerte Strings sind, die mit der string.toCharArray()-Funktion in ein Zeichen-Array eingefügt werden. Dann senden wir mit der radio.write()-Funktion dieses Zeichen-Array-Daten an das andere Modul. Auf der anderen Seite, am Arduino Roboterauto, definieren wir das Modul als Empfänger und akzeptieren Daten mit der radio.read()-Funktion. Dann wandeln wir die empfangenen Daten – die X- und Y-Werte des Joysticks – mit der atoi()-Funktion in Integer-Werte um, die für den Motorkontrollcode unten geeignet sind. Das ist so einfach, aber wie ich bereits sagte, wenn Sie mehr Details zur Verbindung und Einrichtung der Module benötigen, können Sie immer mein spezielles Tutorial dazu überprüfen. Für die letzte Methode der kabellosen Steuerung des Arduino Roboterautos verwenden wir die HC-12 Long Range Transceiver-Module. Diese Module können bis zu 1,8 km Entfernung miteinander kommunizieren. Der Schaltplan für dieses Beispiel ist fast derselbe wie der für die HC-05 Bluetooth-Module, da sie die gleiche Kommunikationsmethode mit dem Arduino über den seriellen Port verwenden. Sie erhalten das HC-12 Transceiver Modul über den folgenden Amazon-Link. Der Joystick-Code ist genau derselbe wie für die Bluetooth-Kommunikation. Wir lesen einfach die analogen Werte des Joysticks und senden sie mit der Serial.write()-Funktion an das andere Modul. Auf der anderen Seite warten wir mit der while()-Schleife, bis die Daten eintreffen, lesen sie dann mit der Serial.read()-Funktion und wandeln sie zurück in den 0-bis-1023-Bereich, der für den Motorkontrollcode unten geeignet ist. Das ist im Wesentlichen alles für dieses Tutorial. Stellen Sie gerne Fragen im Kommentarbereich unten.Arduino Roboterauto Steuerung mit HC-05 Bluetooth Modul


Quellcode
HC-05 Master Code (Joystick)
/* 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 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 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;Vollständiger HC-05 Slave Code (Roboterauto)
/* 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
}Arduino Roboterauto Steuerung mit Smartphone und benutzerdefinierter Android App



// Read the incoming data from the Smartphone Android App
while (Serial.available() >= 2) {
x = Serial.read();
delay(10);
y = Serial.read();
}// Makes sure we receive correct 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);
}
Arduino_Robot_Car_Joystick_App.apk
1 file(s) 1.62 MB Download
Arduino_Robot_Car_Joystick_App_aia_file
1 file(s) 171.20 KB Download
Joystick App Images
1 file(s) 44.36 KB Download
Vollständiger Arduino Code (Smartphone)
/* 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 correct 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
}Arduino Roboterauto Kabellose Steuerung mit NRF24L01 Transceiver Modul


Quellcode
Sender Code (Joystick)
/* 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 module
// Y value
yAxis.toCharArray(xyData, 5);
radio.write(&xyData, sizeof(xyData));
delay(20);
}// 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);
}Empfänger Code (Roboterauto)
/* 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
}Arduino Roboterauto Kabellose Steuerung mit HC-12 Long Range Transceiver

Quellcode
Sender Code (Joystick)
/* 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);
}Empfänger Code (Roboterauto)
/* 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
}
Herstellungsprozess
- Bluetooth-gesteuerter Raspberry Pi Roboter mit Audio-Feedback bauen
- Universelle Android-Fernbedienung mit Arduino, 1Sheeld & IR-Steuerung
- Intelligentes Voltmeter mit Arduino & Smartphone bauen – Einfaches DIY-Projekt
- LED per Bluetooth mit Arduino steuern – Einfache DIY-Anleitung
- Arduino Rover per Firmata & Xbox One Gamepad steuern
- Intelligenter Parkplatzzähler: Echtzeit-Fahrzeugverfolgung mit Arduino, Processing & PHP
- TV per Alexa steuern mit Arduino IoT Cloud – Schritt-für-Schritt
- Kompaktes FM-Radio mit Arduino Nano und RDA5807M selbst bauen
- Drahtloses Arduino-Programmier-Shield mit HC-05 Bluetooth – Kein USB-Kabel nötig
- Arduino Uno & Bluetooth: Fernsteuerung eines Autos – Schritt-für-Schritt-Anleitung