.. note:: Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts. **Why Join?** - **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team. - **Learn & Share**: Exchange tips and tutorials to enhance your skills. - **Exclusive Previews**: Get early access to new product announcements and sneak peeks. - **Special Discounts**: Enjoy exclusive discounts on our newest products. - **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions. 👉 Ready to explore and create with us? Click [|link_sf_facebook|] and join today! .. _ar_ultrasonic: 5.8 User-defined Function ====================================== In c, we can divide a large program into the basic building blocks known as function. The function contains the set of programming statements enclosed by {}. A function can be called multiple times to provide reusability and modularity to the C program. In other words, we can say that the collection of functions creates a program. The function is also known as procedureor subroutinein other programming languages. There are the following advantages of functions. * By using functions, we can avoid rewriting same logic/code again and again in a program. * We can call C functions any number of times in a program and from any place in a program. * We can track a large C program easily when it is divided into multiple functions. * Reusability is the main achievement of C functions. * However, Function calling is always a overhead in a C program. There are two types of functions in C programming: * **Library Functions**: the functions which are declared in the C header files. * **User-defined functions**: the functions which are created by the C programmer, so that he/she can use it many times. It reduces the complexity of a big program and optimizes the code. In this project, define a function to read the value of the ultrasonic module. **Required Components** In this project, we need the following components. It's definitely convenient to buy a whole kit, here's the link: .. list-table:: :widths: 20 20 20 :header-rows: 1 * - Name - ITEMS IN THIS KIT - LINK * - 3 in 1 Starter Kit - 380+ - |link_3IN1_kit| You can also buy them separately from the links below. .. list-table:: :widths: 30 20 :header-rows: 1 * - COMPONENT INTRODUCTION - PURCHASE LINK * - :ref:`cpn_uno` - |link_Uno_R3_buy| * - :ref:`cpn_wires` - |link_wires_buy| * - :ref:`cpn_ultrasonic` - |link_ultrasonic_buy| **Schematic** .. image:: img/circuit_6.3_ultrasonic.png **Wiring** .. image:: img/ultrasonic_bb.jpg :width: 600 :align: center **Code** .. note:: * Open the ``5.8.user_function.ino`` file under the path of ``3in1-kit\basic_project\5.8.user_function``. * Or copy this code into **Arduino IDE**. * Or upload the code through the `Arduino Web Editor `_. .. raw:: html After the code is successfully uploaded, the serial monitor will print out the distance between the ultrasonic sensor and the obstacle ahead. **How it works?** About the application of ultrasonic sensor, we can directly check the subfunction. .. code-block:: arduino float readSensorData(){// ...} The ``trigPin`` of the ultrasonic module transmits a 10us square wave signal every 2us .. code-block:: arduino digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); The ``echoPin`` receives a high level signal if there is an obstacle within the range and use the ``pulseIn()`` function to record the time from sending to receiving. .. code-block:: arduino microsecond=pulseIn(echoPin, HIGH); The speed of sound is 340 m/s or 29 microseconds per centimeter. This gives the distance travelled by the square wave, outbound and return, so we divide by 2 to get the distance of the obstacle. .. code-block:: arduino float distance = microsecond / 29.00 / 2; Note that the ultrasonic sensor will pause the program when it is working, which may cause some lagging when writing complex projects.