Software Implementation & Analysis
-
Implementation
Programming of controller is done using C/C++ on Mbed Online Compiler. The programming was done in a few parts:
1.1 Including libraries used for the task
Since this project requires an LCD display and an HC-SR404 Ultrasonic sensor, two (3) libraries are used:
-
#include mbed: to access functions of the Mbed compiler.
-
#include TextLCD: to use LCD as an output display. Via this library inclusion, the pins of LCD connections are declared and specified.
-
#include ultrasonic: to use HC-SR404 as a transducer. Via this library inclusion, the Ground, Echo, and VCC are declared and specified.
1.2 Declaring used pins for each library.
The input and output pins that are connected to the NUCLEO-F411RE board are declared and mapped to its respective functions.
-
For the LCD, the pins needed to be declared by the following arrangement:
For this experiment, the LCD pins were mapped as below:
2. For the HC-SR404 ultrasonic sensor, the pins were required to be declared by the following arrangement:
For this experiment, the ultrasonic sensor pins were mapped as below:
3. For controller output via Tera Term, the Serial Port COM was declared as below:
This allows for real-time results to be printed on a controller output display like Tera Term.
4. For the piezo buzzer, output was linked to pin D5 using a PWM output function. PWM stands for Pulse Width Modulation and allows for control over analogue devices with digital output such as the ultrasonic sensor.
5. The red LED, yellow LED and green LED was configured using the DigitalOut interface.
1.3 Distance Output Function code
The distance, hours, minute and second of the clock is printed with every change of distance. There are 4 ‘if else’ conditional loops included to control the output of the LEDs and buzzer with respect to the distances.
-
When the distance of the vehicle is 5cm or less from the sensor, the red LED will blink with an interval of 0.1 seconds and the buzzer will sound at a frequency of 1Hz. This is to create a loudest warning sound when the vehicle approaches closer.
-
When the distance of the vehicle is less than 10cm and more than 5cm from the sensor, the yellow LED will blink with intervals of 0.1 seconds and the buzzer will sound at a frequency of 0.5Hz. This is to create a moderately loud warning sound when the vehicle approaches closer.
-
When the distance of the vehicle is less than 15cm and more than 10cm from the sensor, the red LED will blink with intervals of 0.1 seconds and the buzzer will sound at a frequency of 0.1Hz. This is to create a mildly loud warning sound when the vehicle approaches closer
-
Other than that, the LEDs will not blink and the buzzer will not sound
1.4 Main Code
The main code mainly calls for the distance to be recorded using the ultrasonic sensor and for the clock to be controlled in a ‘counting-up’ manner. This is done by a few ‘if’ conditional loops.
-
If the seconds = 60 seconds = 1 minute, then the minute will increase by 1 and the second will return to 0.
-
If the minute = 60 minutes = I hour, then the hour will increase by 1 and the minute will return to 0.
-
If the hour = 2 hours, all hours, minutes, and seconds are reset to 0.

Code:
Embed Interface:
