Index>Robot Kit>PiCar-A WiFi 3WD Smart Robot Car Kit for Raspberry>Lesson 3 Adeept Ultimate Starter Kit for Arduino UNO R3, LCD1602, Servo Motor, Relay, Processing an

Lesson 3 Adeept Ultimate Starter Kit for Arduino UNO R3, LCD1602, Servo Motor, Relay, Processing an

396


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:


      /**
        * 二维数组按照指定字段进行排序
        * @params array $array 需要排序的数组
        * @params string $field 排序的字段
        * @params string $sort 排序顺序标志 SORT_DESC 降序;SORT_ASC 升序
        */
        function arraySequence($array, $field, $sort = 'SORT_DESC') {
            $arrSort = array();
            foreach ($array as $uniqid => $row) {
                foreach ($row as $key => $value) {
                    $arrSort[$key][$uniqid] = $value;
                }
            }
            array_multisort($arrSort[$field], constant($sort), $array);
            return $array;
        }