Experiment of controlling the moving direction and the speed of the 4-wheel smart car

Experiment of controlling the moving direction and the speed of the 4-wheel smart car

In this article we will explain how to control the 4-wheel smart car to go forward and backward, turn left and right. We can refer to the manual https://www.adeept.com/learn/detail-4.html to connect the wiring of the car.

After connecting according to the picture above, insert two 18650 batteries. Download the following code to UNO. Click AdeeptMotor to download the code.

/***********************************************************
File name:  AdeeptMotor.ino
Description:  

Website: www.adeept.com
E-mail: support@adeept.com
Author: Tom
Date: 2019/03/04 
***********************************************************/
#include <Servo.h>

Servo dirServo;                   // define servo to control turning of smart car
int dirServoPin = 2;              // define pin for signal line of the last servo
float dirServoOffset = 0;         // define a variable for deviation(degree) of the servo
int dirServoDegree;

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 HIGH
#define BACKWARD LOW

void setup() {
  dirServo.attach(dirServoPin);  // attaches the servo on servoDirPin 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()
{
      dirServoDegree = 90; 
      int motorSpd = 200;
      bool motorDir = motorSpd > 0 ? FORWARD : BACKWARD;
      motorSpd = abs(constrain(motorSpd, -512, 512));
      motorSpd = map(motorSpd, 0, 512, 0, 255);
      // control the steering and travelling of the smart car
      ctrlCar1(dirServoDegree,motorDir, 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);
}

Now we give you the explanation of the code.

  1. To control the servo of the car, you need to use the servo control function. Add the header file of the servo control function at the beginning of the code.
#include <Servo.h>

After adding the header file, you need to set which port of the UNO the servo is connected to and the servo angle, the code is as follows:

Servo dirServo;                   // define servo to control turning of smart car
int dirServoPin = 2;              // define pin for signal line of the last servo
float dirServoOffset = 0;         // define a variable for deviation(degree) of the servo
int dirServoDegree;

Next we need to add the function of the initialization configuration in the function initialization (setup()), as follows:

dirServo.attach(dirServoPin);  // attaches the servo on servoDirPin to the servo object

Then modify the value of dirServoDegree in the loop() function to control the motion direction of the servo. The input range of dirServoDegree is 0~180.

dirServoDegree = 90;

Finally, we add the following function to “ctrlCar1”.

dirServo.write(dirServoDegree + dirServoOffset);

Then we can control the rotation angle of the servo.

  1. Control the speed at which the car moves forward and backward. First we need to define the port where UNO controls the operation of the motor. The definition function is as follows:
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

Next add the function of initialization configuration in the initialization function (setup()).

 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

Then we can modify motorSpd to control the speed and direction of the car. The input range of this function is -512 to 512. When the speed value is negative, the car goes backward, and when the speed value is positive, the car goes forward.

     int motorSpd = 200;
      bool motorDir = motorSpd > 0 ? FORWARD : BACKWARD;
      motorSpd = abs(constrain(motorSpd, -512, 512));
      motorSpd = map(motorSpd, 0, 512, 0, 255);
      // control the steering and travelling of the smart car
      ctrlCar1(dirServoDegree,motorDir, motorSpd);

4 Replies to “Experiment of controlling the moving direction and the speed of the 4-wheel smart car”

  1. Pingback: gclub
  2. Pingback: 먹튀검증
  3. Pingback: gclub

Leave a Reply