Parse Data Control Motor

Parse the data in the specified data format through the serial port, and use the data to control the state of the car: Here we demonstrate the serial port control of the car state (controlling the car state is to control 4 motors). The specific control of each car state will be introduced in detail in the following tutorials!

Device connection

Hardware connection

Use Type-B data cable to connect Arduino Uno and computer.

Software connection

Open the "Arduino IDE" software and select the model and serial port number corresponding to the development board.

Serial port parsing data

Control principle

Receive data through the serial port and determine whether the data is in the specified format, parse and store the specified data, and use the stored data to control the state of the car.

Control pin

Use the serial port on the Arduino IDE to send and parse data.

Serial portArduino Uno
RX0
TX1

Data format (communication protocol)

We only parse data in two data formats.

Data formatDescriptionExample
$Data header 
#Data tail 
Data,Data,DataData type 1Data represents only numbers: 1,0,0,0
Alphabet+DataData type 2Alphabet+Data represents a letter plus the following three digits: A100

Example:

Data format 1: $1,0,0,0#, $1,2,0,0#, $1,2,0,1#

Data format 2: $A100#, $A090#, $B120#

Note:

The data needs to be sent strictly in the above format.

Control the motor

We use the first digit in data format 1 to control the car status.

Data packetHeaderData: Control the carDelimiterData: Control RGBDelimiterData: Control the buzzerDelimiterDataTail
Car stops$0,0,0,0#
Car moves forward$1,0,0,0#
Car moves backward$2,0,0,0#
Car moves left$3,0,0,0#
Car moves right$4,0,0,0#
Car rotates left$5,0,0,0#
Car rotates right$6,0,0,0#
Car stops$Other,0,0,0#
Data packetHeaderServo numberData: Control servo angleTail
Control servo to rotate 45 degrees$A045#
Control servo to rotate 90 degrees$A090#
Control servo to rotate 135 degrees$A135#

Note: Due to the limited rotation angle of the camera, the control servo rotation range is 35°~145°. It will not rotate beyond this range and keep the boundary value.

Example:

Serial port controls the car to move forward: $1,0,0,0#

Code Analysis

Here we only briefly introduce the code content. For detailed code, please refer to the corresponding code file, which is provided in the download area!

Note: Receive data between $ and #

Experimental results

After compiling the program successfully, upload the code to the Arduino Uno development board.

After the program is started, send data in the specified format, and the serial port will return the parsed data and the car status!

If there is no display content, you can check whether the serial port baud rate is consistent with the code setting, and then press the RESET button on the development board.

image-20240612162036349