How to use the remote control to control the movement of the car

How to use the remote control to control the movement of the car

In this article, we’ll show you how to control the 4-wheel smart car to go forward, backward and veer with the two joysticks of the remote control. To realize the control, we just need to tackle the following steps: firstly achieve the communication between the remote control and the car; secondly read the data of the two joysticks on the remote control; thirdly use UNO to control the movement of the servo; fourthly use UNO to control the movement of two DC motors.

Now we solve them one by one.

Step one: achieve the communication between the remote control and the car. The 2.4G wireless communication module we provide to you is two nrf24l01 modules. These two modules can send and receive data. First, you need to add the rf24.zip function library to the Arduino IDE. Secondly, add the header file (#include “RF24.h”) to the code of the remote control and the car, and then configure the communication address of the two modules (byte addresses[5] = “00007”), only when using the same address information, the remote control and the car can realize the communication control. The remote control can send data to the car continuously by calling the radio.write(data, sizeof(data)) functions. And then next, only need to call the receiveData() function to read the data of two joysticks of the remote control.

Step two: read the analog data of the joysticks to obtain the status information through analogRead function. And store the data in the data array and then sent to the car.

Step three: use the #include <Servo.h> function library in the code of the car end, call the write function to control the angle of the servo.

Step four: use the digitalWrite function to control the motion direction of the motor of the car end, and the analogWrite function to control the speed of the motor.

The specific code of remote control is as follows:

File name:  AdeeptRemoteControl.ino
Description:  
1.Move the sticker rightside left and right to control the direction of the car, and forward and backward to control the left and right turning of the ultrasonic module.  
2.Move the sticker leftside forward and backward to control the speed of the car going forward and backward.
3.Spin the R1 potentiometer to control the startup speed of the DC motor. 
4.Spin the R6 potentiometer to fine tune the direction of the car. 
Website: www.adeept.com
E-mail: support@adeept.com
Author: Tom
Date: 2019/03/28 
***********************************************************/
#include <SPI.h>
#include "RF24.h"
RF24 radio(9, 10);            // define the object to control NRF24L01
byte addresses[5] = "00007";  // define communication address which should correspond to remote control
int data[9];                  // define array used to save the communication data
int mode = 0;
const int pot6Pin = 7;        // define R6
const int pot5Pin = 6;        // define R1
const int led1Pin = 6;        // define pin for LED1 which is close to NRF24L01 and used to indicate the state of NRF24L01
const int MotorPin = 2;       // define pin for direction X of joystick U2
const int ServoPin = 1;       // define pin for direction Y of joystick U1
const int ultrasonicPin = 3;  // define pin for direction x of joystick U2

void setup() {
  
  radio.begin();                      // initialize RF24
  radio.setRetries(0, 15);            // set retries times
  radio.setPALevel(RF24_PA_LOW);      // set power
  radio.openWritingPipe(addresses);   // open delivery channel
  radio.stopListening();              // stop monitoring
  pinMode(led1Pin, OUTPUT);   // set led1Pin to output mode 
  data[3] = 0;
  data[4] = 1;
}

void loop() {
  // put the values of rocker, switch and potentiometer into the array
  data[0] = analogRead(MotorPin);
  data[1] = analogRead(ServoPin);
  data[6] = analogRead(pot5Pin);
  data[7] = analogRead(pot6Pin);
  data[8] = analogRead(ultrasonicPin);
  // send array data. If the sending succeeds, open signal LED
  if (radio.write( data, sizeof(data) ))
  digitalWrite(led1Pin,HIGH);

  // delay for a period of time, then turn off the signal LED for next sending
  delay(2);
  digitalWrite(led1Pin,LOW);
}

The specific code of the car is as follows:

File name:  AdeeptMotor.ino
Description:  
Under remote control mode: 
The car is completely controlled by the remote control. 
Website: www.adeept.com
E-mail: support@adeept.com
Author: Tom
Date: 2019/03/28 
***********************************************************/
#include <SPI.h>
#include "RF24.h"
#include <Servo.h>

RF24 radio(9, 10);                // define the object to control NRF24L01
byte addresses[5] = "00007";      // define communication address which should correspond to remote control
int data[9]={512, 512, 0, 0, 1, 1, 512, 512, 512};  // define array used to save the communication data
int mode[1];

Servo dirServo;                  // define servo to control turning of smart car
int dirServoPin = 2;              // define pin for signal line of the last servo
float dirServoOffset = 6;         // define a variable for deviation(degree) of the servo
Servo ultrasonicServo;           // define servo to control turning of ultrasonic sensor
int ultrasonicPin = 3;            // define pin for signal line of the last servo
float ultrasonicServoOffset = 15; // define a variable for deviation(degree) of the servo

#define FORWARD HIGH
#define BACKWARD LOW

const int dirAPin = 7;    // define pin used to control rotational direction of motor A
const int pwmAPin = 6;    // define pin for PWM used to control rotational speed of motor A
const int dirBPin = 4;    // define pin used to control rotational direction of motor B
const int pwmBPin = 5;    // define pin for PWM used to control rotational speed of motor B

#define FORWARD LOW
#define BACKWARD HIGH

void setup() {
  radio.begin();                      // initialize RF24
  radio.setRetries(0, 15);            // set retries times
  radio.setPALevel(RF24_PA_LOW);      // set power
  radio.openReadingPipe(1, addresses);// open delivery channel
  radio.startListening();             // start monitoring

//  Serial.begin(9600); // initialize serial port
  dirServo.attach(dirServoPin);  // attaches the servo on servoDirPin to the servo object
  ultrasonicServo.attach(ultrasonicPin);  // attaches the servo on ultrasonicPin to the servo object
  pinMode(dirAPin, OUTPUT);   // set dirAPin to output mode
  pinMode(pwmAPin, OUTPUT);   // set pwmAPin to output mode
  pinMode(dirBPin, OUTPUT);   // set dirBPin to output mode
  pinMode(pwmBPin, OUTPUT);   // set pwmBPin to output mode
}

void loop()
{
    receiveData();
    //  radio.write( mode, sizeof(mode) );
      // calculate the steering angle of servo according to the direction joystick of remote control and the deviation
      int dirServoDegree = map(data[0], 0, 1023,135 ,45 ) - (data[7] - 512) / 25; 
      int ultrasonicServoDegree = map(data[8], 0, 1023, 45, 135) - (data[7] - 512) / 25; 
      // get the steering angle and speed of servo according to the speed joystick of remote control and the deviation
      int motorSpd = data[1] - 512 + (data[6] - 512) / 10;
      bool motorDir = motorSpd > 0 ? BACKWARD : FORWARD;
      motorSpd = abs(constrain(motorSpd, -512, 512));
      motorSpd = map(motorSpd, 0, 512, 0, 255);
      // control the steering and travelling of the smart car
      ctrlCar0(dirServoDegree,ultrasonicServoDegree, motorDir, motorSpd);
}
void receiveData(){
   if ( radio.available()) {             // if receive the data
    while (radio.available()) {         // read all the data
      radio.read( data, sizeof(data) ); // read data
    }
   }
}
void ctrlCar0(byte dirServoDegree,byte ultrasonicServoDegree, bool motorDir, byte motorSpd) {
  dirServo.write(dirServoDegree + dirServoOffset);
  ultrasonicServo.write(ultrasonicServoDegree + ultrasonicServoOffset);
  digitalWrite(dirAPin, motorDir);
  digitalWrite(dirBPin, motorDir);
  analogWrite(pwmAPin, motorSpd);
  analogWrite(pwmBPin, motorSpd);
}
void ctrlCar1(byte dirServoDegree, bool motorDir, byte motorSpd) {
  dirServo.write(dirServoDegree + dirServoOffset);
  digitalWrite(dirAPin, motorDir);
  digitalWrite(dirBPin, motorDir);
  analogWrite(pwmAPin, motorSpd);
  analogWrite(pwmBPin, motorSpd);
}

Leave a Reply