Arduino Robot Car Wireless Control Utilizando HC
Arduino Robot Car Wireless Control Utilizando HC
Código fuente
Usaremos el mismo código del tutorial anterior , donde controlamos el
coche robot Arduino directamente usando el joystick, y le haremos algunas
modificaciones.
/*
*/
void setup() {
}
void loop() {
// Send the values via the serial port to the slave HC-05 Bluetooth device
Serial.write(yAxis/4);
delay(20);
Hacemos esto porque ese rango, de 0 a 255, se puede enviar, a través del
dispositivo Bluetooth, como 1 byte, que es más fácil de aceptar en el otro
lado, o en el automóvil robot Arduino.
Así que aquí, si el serial ha recibido los 2 bytes, los valores X e Y, usando la
función Serial.read() leeremos ambos.
// Read the incoming data from the Joystick, or the master Bluetooth device
x = Serial.read();
delay(10);
y = Serial.read();
// Convert back the 0 - 255 range to 0 - 1023, suitable for motor control code
below
xAxis = x*4;
yAxis = y*4;
/*
*/
#define enA 9
#define in1 4
#define in2 5
#define enB 10
#define in3 6
#define in4 7
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);
}
void loop() {
x = 510 / 4;
y = 510 / 4;
// Read the incoming data from the Joystick, or the master Bluetooth device
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;
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
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
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
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
else {
motorSpeedA = 0;
motorSpeedB = 0;
// Move to left - decrease left motor speed, increase right motor speed
if (motorSpeedA < 0) {
motorSpeedA = 0;
motorSpeedB = 255;
// Convert the increasing X-axis readings from 550 to 1023 into 0 to 255
value
// Move right - decrease right motor speed, increase left motor speed
motorSpeedA = 255;
}
if (motorSpeedB < 0) {
motorSpeedB = 0;
motorSpeedA = 0;
motorSpeedB = 0;
x = Serial.read();
delay(10);
y = Serial.read();
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
Arduino_Robot_Car_Joystick_App.apk
1 archivo(s)1,62 MB
DESCARGAR
Arduino_Robot_Car_Joystick_App_aia_file
1 archivo(s)171,20 KB
DESCARGAR
Imágenes de la aplicación Joystick
1 archivo(s)44.36KB
DESCARGAR
Código Arduino completo:
/*
Arduino Robot Car Wireless Control using the HC-05 Bluetooth and custom-build
Android app
*/
#define enA 9
#define in1 4
#define in2 5
#define enB 10
#define in3 6
#define in4 7
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);
void loop() {
xAxis = 510;
yAxis = 510;
x = Serial.read();
delay(10);
y = Serial.read();
delay(10);
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
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
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
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
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
else {
motorSpeedA = 0;
motorSpeedB = 0;
if (motorSpeedA < 0) {
motorSpeedA = 0;
motorSpeedB = 255;
// Convert the increasing X-axis readings from 550 to 1023 into 0 to 255
value
// Move right - decrease right motor speed, increase left motor speed
motorSpeedA = 255;
if (motorSpeedB < 0) {
motorSpeedB = 0;
}
motorSpeedA = 0;
motorSpeedB = 0;
Código fuente
Para este ejemplo, necesitamos instalar la biblioteca RF24. De forma similar
al ejemplo anterior, después de definir algunos pines y configurar el módulo
como transmisor, leemos los valores X e Y del joystick y los enviamos al otro
módulo NRF24L01 en el coche robot Arduino.
Primero, podemos notar que las lecturas analógicas son cadenas, que al usar
la función string.toCharArray() se colocan en una matriz de caracteres. Luego,
usando la función radio.write(), enviamos esa matriz de datos de caracteres
al otro módulo.
/*
Arduino Robot Car Wireless Control using the NRF24L01 Transceiver module
== Transmitter - Joystick ==
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
void setup() {
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
void loop() {
// X value
// Y value
yAxis.toCharArray(xyData, 5);
radio.write(&xyData, sizeof(xyData));
delay(20);
}
delay(10);
radio.read(&receivedData, sizeof(receivedData));
yAxis = atoi(&receivedData[0]);
delay(10);
/*
Arduino Robot Car Wireless Control using the NRF24L01 Transceiver module
*/
#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 in3 6
#define in4 7
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() {
delay(10);
radio.read(&receivedData, sizeof(receivedData));
yAxis = atoi(&receivedData[0]);
delay(10);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
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
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
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
else {
motorSpeedA = 0;
motorSpeedB = 0;
// Move to left - decrease left motor speed, increase right motor speed
if (motorSpeedA < 0) {
motorSpeedA = 0;
motorSpeedB = 255;
}
// Convert the increasing X-axis readings from 550 to 1023 into 0 to 255
value
// Move right - decrease right motor speed, increase left motor speed
motorSpeedA = 255;
if (motorSpeedB < 0) {
motorSpeedB = 0;
motorSpeedA = 0;
motorSpeedB = 0;
}
analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
El esquema del circuito para este ejemplo es casi el mismo que el de los
módulos Bluetooth HC-05, ya que utilizan el mismo método para
comunicarse con Arduino, a través del puerto serie.
Puede obtener el módulo transceptor HC-12 en el siguiente enlace de
Amazon .
Código fuente
El código del Joystick es exactamente el mismo que el de la comunicación
Bluetooth. Simplemente leemos los valores analógicos del joystick y los
enviamos al otro módulo usando la función Serial.write().
/*
Arduino Robot Car Wireless Control using the HC-12 long range wireless module
== Transmitter - Joystick ==
*/
void setup() {
void loop() {
// Send the values via the serial port to the slave HC-05 Bluetooth device
Serial.write(yAxis/4);
delay(20);
/*
Arduino Robot Car Wireless Control using the HC-12 long range wireless module
*/
#define enA 9
#define in1 4
#define in2 5
#define enB 10
#define in3 6
#define in4 7
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);
void loop() {
xAxis = 510;
yAxis = 510;
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;
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
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
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
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
else {
motorSpeedA = 0;
motorSpeedB = 0;
// Move to left - decrease left motor speed, increase right motor speed
if (motorSpeedA < 0) {
motorSpeedA = 0;
motorSpeedB = 255;
}
// Convert the increasing X-axis readings from 550 to 1023 into 0 to 255
value
// Move right - decrease right motor speed, increase left motor speed
motorSpeedA = 255;
if (motorSpeedB < 0) {
motorSpeedB = 0;
motorSpeedA = 0;
motorSpeedB = 0;
}
analogWrite(enA, motorSpeedA); // Send PWM signal to motor A