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

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

  1. Pingback: gift items online
  2. Pingback: cavapoo full grown
  3. Pingback: olansi k08a
  4. Pingback: jeremy hughes hsbc
  5. Pingback: what is nembutal
  6. Pingback: nembutal canada
  7. Pingback: floor marking tape
  8. Pingback: Go to This Website
  9. Pingback: Taylan Evrenler
  10. Pingback: bloom vape battery
  11. Pingback: daftar slot online
  12. Pingback: singapore pools
  13. Pingback: Visit Here
  14. Pingback: Skin Care Products
  15. Pingback: sollift machine
  16. Pingback: today news bihar
  17. Pingback: ceriabet
  18. Pingback: here wee read
  19. Pingback: Golf cart battery
  20. Pingback: cbd oil uses
  21. Pingback: webm to.mp4 api
  22. Pingback: photo funny love
  23. Pingback: bvlgari ring price
  24. Pingback: off white edibles
  25. Pingback: cbd oil
  26. Pingback: royal funny status
  27. Pingback: screw kit for pc
  28. Pingback: cam lock screws
  29. Pingback: self locking screw
  30. Pingback: world globe price
  31. Pingback: world globe price
  32. Pingback: ev battery cell
  33. Pingback: cheese price
  34. Pingback: best online casino
  35. Pingback: online casino
  36. Pingback: cbd products
  37. Pingback: dominoqq apk
  38. Pingback: howto site
  39. Pingback: 100% free essays
  40. Pingback: click here#
  41. Pingback: ev12-180x battery
  42. Pingback: write articles
  43. Pingback: media files
  44. Pingback: mp4 converter
  45. Pingback: mp3 converter app
  46. Pingback: pepperfry
  47. Pingback: sot gambling site
  48. Pingback: virtual visa card
  49. Pingback: vaping aerosol
  50. Pingback: layar kaca 21
  51. Pingback: cassino online
  52. Pingback: live casino online
  53. Pingback: judi bola 88
  54. Pingback: gout definition
  55. Pingback: get scholarship
  56. Pingback: 888 casino
  57. Pingback: Buy Oxycodone
  58. Pingback: dog care website
  59. Pingback: sheet mask at home
  60. Pingback: poker stars
  61. Pingback: sheet mask online
  62. Pingback: list of anime
  63. Pingback: buy osrs gold safe
  64. Pingback: sofradom
  65. Pingback: anime tv
  66. Pingback: dark web login
  67. Pingback: goa casino online
  68. Pingback: Find food for pet
  69. Pingback: buy dog supplies
  70. Pingback: lightsaber toy
  71. Pingback: ffp2 mask amazon
  72. Pingback: best pet food
  73. Pingback: pyrenees spain
  74. Pingback: lip balm himalaya
  75. Pingback: home cash app
  76. Pingback: dark web browser
  77. Pingback: healthy food
  78. Pingback: dewa4dku
  79. Pingback: pandora188
  80. Pingback: win88
  81. Pingback: Microswitch
  82. Pingback: mini-limit switch
  83. Pingback: PREMIUM CBD OIL
  84. Pingback: Testing57
  85. Pingback: Testing56
  86. Pingback: Testing 55
  87. Pingback: Zastave Arms CZ999
  88. Pingback: sahabatpoker
  89. Pingback: hondaqq
  90. Pingback: official site
  91. Pingback: tutoring online
  92. Pingback: homework helper
  93. Pingback: THC Carts for sale
  94. Pingback: buy DMT
  95. Pingback: Dylan Sellers
  96. Pingback: tor web pages
  97. Pingback: click here
  98. Pingback: care credit
  99. Pingback: sex toys
  100. Pingback: m88 bi chan
  101. Pingback: mansion88
  102. Pingback: lesbian strapon
  103. Pingback: keluaran hk pools
  104. Pingback: rajapoker88
  105. Pingback: DotNek Tech Blog
  106. Pingback: CBD Spray
  107. Pingback: 21 dukes
  108. Pingback: bitcoin price
  109. Pingback: debian vpn
  110. Pingback: togel seoul
  111. Pingback: ufabet
  112. Pingback: more
  113. Pingback: Car Rental Deals
  114. Pingback: seo cambridge
  115. Pingback: Nagad
  116. Pingback: verlobungsring
  117. Pingback: gclub
  118. Pingback: 먹튀검증
  119. Pingback: gclub

Leave a Reply