07 SmartRobotCarV4.0 Others
07 SmartRobotCarV4.0 Others
Introduction:
In this lesson, we will teach you the functions and specific usages of all
the remaining components.
Preparation:
The specific process is to use the Wi-Fi module of the ESP32 to open the
Wi-Fi hotspot, and send the information through the APP after the phone
has connected to the hotspot. The information was sent out through the
Wi-Fi module of the phone and then received by the Wi-Fi module of the
ESP32. Since we have set the Wi-Fi module to pass-through mode before
leaving the factory, the acquired data received by the Wi-Fi module of the
ESP32 is transferred to the serial port of the ESP32. And since the serial port
on ESP32 is connected to the serial port on UNO, we just need to read the
serial port on the UNO to get the data from the APP.
As for the program of UNO, we obtain the string by reading the data
received in the serial buffer, and then parse the string. The key information
is extracted through the fixed format of the string, and the flag bit is set
according to different key information, and different functions are selected
to be called to realize the corresponding function.
The format of the string (json data format) is as follows: {"N": 2, "D1": 1}, the
parameters corresponding to "N" represent different functions, and the
parameters such as "D1" and "D2" represent the parameters that need to
be modified and adjusted when implementing different functions.
Let’s first take a look at the definition of the relative functions of serial
port data reading.
// in ApplicationFunctionSet_xxx0.h
class ApplicationFunctionSet
{
public:
void ApplicationFunctionSet_Init(void);
void ApplicationFunctionSet_SerialPortDataAnalysis(void);
String CommandSerialNumber;
};
Then we need to add the JSON library to analyse the JSON data send
from the APP.
// in ApplicationFunctionSet_xxx0.cpp
#include "ArduinoJson-v6.11.1.h" //ArduinoJson
// in ApplicationFunctionSet_xxx0.cpp
void ApplicationFunctionSet::ApplicationFunctionSet_Init(void)
{
Serial.begin(9600);
}
Access an complete instruction from the serial port.
// in ApplicationFunctionSet_xxx0.cpp
void
ApplicationFunctionSet::ApplicationFunctionSet_SerialPortDataAnalysis(void)
{
static String SerialPortData = "";
uint8_t c = "";
if (Serial.available() > 0)
{
while (c != '}' && Serial.available() > 0)
{
c = Serial.read();
SerialPortData += (char)c;}}
.......
}
Since the format of the string (JSON data format) is as follows: {" N ": 2,
" D1 ": 1}, we only need to determine whether the ending character is "} "to
know whether we have received the entire command.
// in ApplicationFunctionSet_xxx0.cpp
void
ApplicationFunctionSet::ApplicationFunctionSet_SerialPortDataAnalysis(void)
{
.....
if (c == '}')
{
}
.......
}
After obtaining the command successfully, the command needs to be
parsed.
// in ApplicationFunctionSet_xxx0.cpp
void
ApplicationFunctionSet::ApplicationFunctionSet_SerialPortDataAnalysis(void)
{
.....
else if (!error)
{
int control_mode_N = doc["N"];
char buf[3];
uint8_t temp = doc["H"];
sprintf(buf, "%d", temp);
CommandSerialNumber = buf;
.......
}
Each case represents one mode, and we just need to put in the functions
we have implemented in the previous few lessons to realize the APP
switching function.
// in ApplicationFunctionSet_xxx0.cpp
void
ApplicationFunctionSet::ApplicationFunctionSet_SerialPortDataAnalysis(void)
{
.....
switch (control_mode_N)
{
case 1:
break;
case 2:
.......
}
Upload program. (Please toggle the“Upload-Cam”button to“Upload”
when uploading the program.) After the program has been uploaded
successfully, please do not disconnect the USB cable. Turn the "Upload-Cam"
button to "Cam", and then open mobile’s APP and connect to the car. When
the connection is complete, operate the button in the APP casually. By this
time, open the serial port monitor in the Arduino IDE and you will find that
every operation you do on the APP will send a string to the car, and the car
will perform the corresponding function according to these instructions.
Please open the folder in the previous level for more details: Related chip
information -> SmartRobot-Shield.pdf
Let’s first look at the definition of the relative pins and variables:
// in DeviceDriverSet_xxx0.h
/*Key Detection*/
class DeviceDriverSet_Key
{
public:
void DeviceDriverSet_Key_Init(void);
#if _Test_DeviceDriverSet
void DeviceDriverSet_Key_Test(void);
#endif
void DeviceDriverSet_key_Get(uint8_t *get_keyValue);
public:
#define PIN_Key 2
#define keyValue_Max 4
public:
static uint8_t keyValue;
};
// in DeviceDriverSet_xxx0.h
#include "PinChangeInt.h"
Then, define the key pin and add the interrupt function to it.
We will set it as falling edge trigger. When the key is pressed, the level
changes, and the falling edge will trigger the interrupt function.
// in DeviceDriverSet_xxx0.cpp
void DeviceDriverSet_Key::DeviceDriverSet_Key_Init(void)
{
pinMode(PIN_Key, INPUT_PULLUP);
attachPinChangeInterrupt
(PIN_Key, attachPinChangeInterrupt_GetKeyValue, FALLING);
}
And we need to record how many times the key is pressed. We can only
record it as the fourth time at most cause we only have 4 modes in total.
// in DeviceDriverSet_xxx0.cpp
// in ApplicationFunctionSet_xxx0.cpp
void ApplicationFunctionSet::ApplicationFunctionSet_KeyCommand(void)
Let’s take a look at the definition of the relative pins and variables:
// in DeviceDriverSet_xxx0.h
class DeviceDriverSet_IRrecv
{
public:
void DeviceDriverSet_IRrecv_Init(void);
bool DeviceDriverSet_IRrecv_Get(uint8_t *IRrecv_Get /*out*/);
void DeviceDriverSet_IRrecv_Test(void);
public:
unsigned long IR_PreMillis;
private:
#define RECV_PIN 9
......
};
// in DeviceDriverSet_xxx0.h
bool DeviceDriverSet_IRrecv::DeviceDriverSet_IRrecv_Get
(uint8_t *IRrecv_Get /*out*/)
// in ApplicationFunctionSet_xxx0.cpp
void ApplicationFunctionSet::ApplicationFunctionSet_IRrecv(void)
Upload program. (Please toggle the“Upload-Cam”button to“Upload”
when uploading the program.) After the program has been uploaded
successfully, please open the serial port and press the key, you will see the
statement that switches to different functions is displayed on the serial port.
Since our focus in this section is on the use of keys, we only print out the
current switched mode statement on the serial port and do not implement
the corresponding specific functions, but you can add different functions
according to the previous lessons.
void voltageInit()
{
pinMode(VOL_MEASURE_PIN, INPUT);
}
Vout =(Vin×R2)
(/ R1 + R2)
Since the data might be overlooked by the software and errors might
occur if the decimal is too large when the data is dividing by 1024, we’d
better divide the data by 1024 at the end.
Battery voltage V = A3 read value *(5.00)*((R1 + R2)/R2)/ 1024.00
void Voltage_Measure()
{
if (millis() - vol_measure_time > 1000) //Measured every 1000 milliseconds
{
vol_measure_time = millis();
float voltage =
(analogRead(VOL_MEASURE_PIN) * 5 ) * ((10 + 1.5) / 1.5)/ 1024;
//Read voltage value
//float voltage = (analogRead(VOL_MEASURE_PIN) * 0.0375);
voltage = voltage + (voltage * 0.08);
Serial.print("Current voltage value : ");
Serial.println(voltage);
if(voltage>7.8)
Serial.println("The battery is fully charged");
else
Serial.println("Low battery");
}
}
Upload program.(Please toggle the“Upload-Cam”button to“Upload”
when uploading the program.)After the program has been uploaded
successfully, please open the serial port and you will see the current voltage
value.
We will teach you how to turn on the colored light on the car and change
its brightness and color.
In our kit, the realization of lighting effects uses the WS2812B module.
WS2812B is an intelligent externally controlled LED light source integrating a
control circuit and a light-emitting circuit. The data protocol adopts a
single-wire return-to-zero code communication method. After the pixel is
powered on and reset, the DIN terminal receives the data transmitted from
the controller.
The first 24bit data sent over is extracted by the first pixel and
sent to the data latch inside the pixel, the remaining data is shaped and
amplified by the internal shaping processing circuit and then forwarded
through the DO port to start outputting to the next cascade of pixel, and the
signal is reduced by 24bit for each pixel that passes through the transmission.
The pixel adopts the automatic shaping and forwarding technology, so that
the number of cascades of the pixel is not limited by the signal transmission,
but only by the signal transmission speed requirements. LED has the
advantages of low voltage drive, environmental protection and
energy-saving, high brightness, large scattering angle, good consistency,
ultra-low power, and ultra-long life. Integrating the control circuit on the LED
makes the circuit simpler and smaller in size, and easier to install.
Please open the folder in the previous level for more details: Related chip
information -> SmartRobot-Shield
It can be seen from the figure that the RGB colored light is connected to
the D4 pin of UNO
#include "FastLED.h"
#define PIN_RBGLED 4
#define NUM_LEDS 1
CRGB leds[NUM_LEDS];
Initial WS2812 and set the brightness of the LED light.
void setup() {
Serial.begin(9600);
FastLED.addLeds<NEOPIXEL, PIN_RBGLED>(leds, NUM_LEDS);
FastLED.setBrightness(20);
}
Void myColor()
http://www.elegoo.com