Writing a program for getting data from the ultrasonic sensor
In this section, you will learn how to write a program to gather data from the ultrasonic sensor. Let's start, as follows:
- First, we will define which pins of the STM32 Blue Pill card will be used to read the sensor data. Also, we will declare two variables to save the duration of the sound-wave travel and another for calculating the distance traveled, as illustrated in the following code snippet:
const int pinTrigger = PC14; const int pinEcho = PC13; long soundWaveTime; long distanceMeasurement;
The selected pins were the PC13 and PC14 pins (labeled C13 and C14 on the Blue Pill).
- Next, in the
setup()
function, begin the serial communication. You will set the trigger pin as an output pin and the echo pin as an input pin. We need to initialize the trigger in theLOW
value. The code is illustrated in the following snippet:void setup() { Serial.begin(9600); pinMode(pinTrigger, OUTPUT...