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);

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

  1. Pingback: sheet mask at home
  2. Pingback: poker stars
  3. Pingback: sheet mask online
  4. Pingback: list of anime
  5. Pingback: buy osrs gold safe
  6. Pingback: sofradom
  7. Pingback: anime tv
  8. Pingback: dark web login
  9. Pingback: goa casino online
  10. Pingback: Find food for pet
  11. Pingback: buy dog supplies
  12. Pingback: lightsaber toy
  13. Pingback: ffp2 mask amazon
  14. Pingback: best pet food
  15. Pingback: pyrenees spain
  16. Pingback: lip balm himalaya
  17. Pingback: home cash app
  18. Pingback: dark web browser
  19. Pingback: healthy food
  20. Pingback: dewa4dku
  21. Pingback: pandora188
  22. Pingback: win88
  23. Pingback: Microswitch
  24. Pingback: mini-limit switch
  25. Pingback: PREMIUM CBD OIL
  26. Pingback: Testing57
  27. Pingback: Testing56
  28. Pingback: Testing 55
  29. Pingback: Zastave Arms CZ999
  30. Pingback: sahabatpoker
  31. Pingback: hondaqq
  32. Pingback: official site
  33. Pingback: tutoring online
  34. Pingback: homework helper
  35. Pingback: THC Carts for sale
  36. Pingback: buy DMT
  37. Pingback: Dylan Sellers
  38. Pingback: tor web pages
  39. Pingback: click here
  40. Pingback: care credit
  41. Pingback: sex toys
  42. Pingback: m88 bi chan
  43. Pingback: mansion88
  44. Pingback: lesbian strapon
  45. Pingback: keluaran hk pools
  46. Pingback: rajapoker88
  47. Pingback: DotNek Tech Blog
  48. Pingback: CBD Spray
  49. Pingback: 21 dukes
  50. Pingback: bitcoin price
  51. Pingback: debian vpn
  52. Pingback: togel seoul
  53. Pingback: ufabet
  54. Pingback: more
  55. Pingback: Car Rental Deals
  56. Pingback: seo cambridge
  57. Pingback: Nagad
  58. Pingback: verlobungsring
  59. Pingback: gclub
  60. Pingback: 먹튀검증
  61. Pingback: gclub

Leave a Reply