Spirometer | UNIHIKER Tutorial: Learn Python with Graphical Programming for Beginners 10
Spirometry is a type of pulmonary function test. It determines how well your lungs work by measuring how much air goes into and out of your lungs when you breathe. In this lesson, we will use the UNIHIKER to simulate a spirometer.
Goal
Press button A on the UNIHIKER to begin the measurement. Blow forcefully at the UNIHIKER to simulate the spirometry test process. As you blow, the progress bar will display your lung capacity status in real time. After multiple tests, the screen will display the last three measured data.
Note: This spirometer is only a simple simulation of a spirometry test process, and cannot be used as a medical tool to measure lung capacity.
Get to Know
1. The basic usage of the list in the program
2. The conditional loop statements in programs
3. The if-then-else-if-then-else statement in programs
4. How to draw line segments on the UNIHIKER screen
5. How to obtain sound intensity with the UNIHIKER
Preparations
Hardware:
Software: Mind+
Download address: https://www.mindplus.cc
Hands-on Practice
In this lesson, we will turn the UNIHIKER into a spirometer that can display the lung capacity status by counting the blowing duration and automatically record the measured data. The project will be completed in two tasks.
Task 1: Obtain lung capacity data
In this task, we will learn how to use the microphone on the UNIHIKER board to simulate spirometry tests, and then display the lung capacity status through a progress bar.
Task 2: Record the measured data automatically
In task 2, we will learn the basic operations of the list, and then record and display the lung capacity data in multiple measurements.
Task 1: Obtain lung capacity data
1. Hardware Connection
Connect UNIHIKER to the computer via a USB cable.
2. Software Preparation
Open Mind+, and complete the software preparation according to the figure below.
3. Write a Program
During a lung function test, we need to hold the spirometer and blow air into it. But how can we detect blowing and obtain lung capacity data using the UNIHIKER board? Let's figure it out together.
(1) Obtain lung capacity data
(a) Detect blowing
We can detect blowing by measuring the sound intensity. The command for reading the microphone sound intensity is Read microphone sound level, which can be found in the "Onboard Sensors" under the "UNIHIKER" category in the command area.
Note: The microphone is located on the top left of the UNIHIKER, as shown below. For more information about the UNIHIKER microphone, refer to the "Knowledge Base".
Use the command to display the sound intensity value on the UNIHIKER screen in real time and observe the numerical changes to determine if blowing is occurring.
Note: You can use Remote Desktop Connection to connect to UNIHIKER for conveniently observing data changes.
When there is no blowing, what you see is the current ambient sound intensity. Then, blow to the position where the microphone icon is located on the board, then the sound intensity value goes above 40.
Therefore, we can determine whether blowing is occurring through the sound intensity value.
(b) Obtain lung capacity data
Lung capacity data is obtained by continuous blowing, so we need to use variables to record the blowing status. After starting to blow, the variable increases continuously until the blowing stops. Therefore, we can use the repeat until block to control the variable Lung capacity data, continuously increasing it until the sound level is less than 40, which indicates the end of data collection.
Note: The repeat until is a conditional loop statement located under the "Control" category in the command area. For more information about this command, please refer to the "Knowledge Base".
To start a measurement by pressing button A, we will use the command Button A was pressed, which can be found in the "Onboard Sensors" under the UNIHIKER category in the command area. If the button is pressed, the lung capacity measurement will start.
(2) Display the lung capacity status
The standard for lung capacity varies across different age groups in actual spirometry tests. In this project, we'll use a progress bar to display the lung capacity status, and add a "target line" to indicate whether one has passed the test. The figure below shows the composition of the progress bar.
The "target line" can be displayed using the "line segment display" command Object name display line segment start point X0Y0 end point X240Y320 line width 1 color blue, which is located above the "displays rectangle" block. Also, you can find it by searching for the "line segment" keyword.
Then, we can calculate the rectangle-related parameters and the coordinates of the start point and end point of the line segment based on the composition and position of the progress bar. The "target line" position can be calculated by setting a ratio, for example, set it to 60%, then, calculate the position as follows:
Now, you can adjust the width of the progress bar (filled rectangle) according to the lung capacity data. And you can also add some prompts. The complete reference code is as follows:
4. Run the Program
Click Run, and the UNIHIKER screen displays the text "Lung capacity data" and value 0. Press and hold button A, and a prompt tone will sound to indicate that the lung function test has started. Blow into the UNIHIKER microphone while observing the changes in the progress bar.
5. Have a Try
The progress bar's outer frame width is known to be 200, and we assume the corresponding lung capacity data is 2000. But when the data exceeds 2000, the progress bar extends beyond the outer frame, as shown below. So try modifying the program to keep the progress bar staying within the frame when the lung capacity data exceeds 2000.
Task 2: Record the measured data automatically
1. Write a Program
In Task 1, we learned how to measure lung capacity data. But how can we record and display the measured data on the UNIHIKER? Let’s try them in this task.
(1) Record measured data
To record the measured data, we can use the variable "list", which can be understood as a "table". The relevant operation blocks are under the "List" category in the command area.
Note: For more introduction to the "list", see the "Knowledge Base".
To add the historical data to the "list", we need to first create an empty "list" variable, that is, create a data table variable and assign initialization list [] to it. Remember to delete the content in the block.
Then we need to add the lung capacity data to the list after a lung function test is over. The command for adding data to the list is list[] adds 0 to the end. When using it, we just need to fill in the variable data table and Lung capacity data respectively.
(2) Display measured data
First, create three objects to display the measured data.
Next, display the data, here we need to take the data out of the list using the command list[] index 0 value. It should be noted that the index of the list indicates the Nth element in the list, and an index starts from 0, so the index of the first element in the list is 0.
There are three conditions for displaying measured data. For one measurement, only the first data in the list is displayed with an index of 0; for two measurements, the first two data are displayed with indexes of 0 and 1 respectively; for three or above measurements, the last three data are displayed.
To achieve that, we need to use the length of the list[] command to determine how many elements there are in the current list and update data content accordingly.
To display the last three data in the list, we can get their indexes by subtracting 1, 2, and 3 from the length of the list respectively. You can use the length of the list[] block to get and update the data display object.
To set up three conditions, we need to use the if-then statement (with "+"). Click the plus sign twice to set a multi-condition statement.
Note: For an introduction to the if-then-else-if-then-else statement, see the "Knowledge Base".
Finally, complete the data display of the three conditions and add corresponding prompts to the data. The complete reference code is as follows:
2. Run the Program
Press button A to measure lung capacity, and the last three measured data will be displayed on the screen if multiple tests are implemented.
Knowledge Base
1. What is a list in coding?
A list is a data type used to store multiple data, where the position of each data in the list is identified by its "index". You can conceptualize a list as a table, where you can perform various operations like reading, adding, deleting, and modifying its content. But unlike a table, the first data element's index in a list is usually 0.
In Python, a list is created using "[]" with nothing inside, which is called an "empty list". If there are two or more data elements in the list, the elements need to be separated by commas, such as [0, "Mind+", 12.5, "UNIHIKER"].
Additionally, list and numeric are both basic data types in Python, and other basic data types such as string and dictionary will be introduced in future lessons.
You can find command blocks for list operations under the "List" category in the command area, such as the block initialization list [] and list[] adds 0 to the end. And you can try using them or search online to figure out their meaning and usage.
2. Conditional loop statements in Programs
The conditional loop statement refers to the repeat until command, which is one of the loop structure statements. During execution, it will first evaluate if the condition after "until" is met, if met, it will stop executing the code statement inside the "repeat". The code execution process can be described below.
3. If-then-else-if-then-else statement
The if-then-else-if-then-else statement is a type of conditional statement used to evaluate two or more conditions. This statement allows you to add or reduce conditions by clicking the "+" or "-" sign respectively.
For example, in a three-condition case like the figure on the left, the statement will first evaluate if condition 1 is true. If it is true, code statement 1 will be executed; otherwise, it will then evaluate if condition 2 is true. If it is true, code statement 2 will be executed; otherwise, it will execute code statement 3. The image on the right illustrates the code execution process.
4. On-Board Microphone
The onboard microphone is located on the top left of the UNIHIKER board. It is a capacitive silicon microphone that is small and highly sensitive and can be used to obtain sound in the environment.
Along with the accelerometer and light sensor, the onboard microphone also belongs to the onboard sensors. Therefore, you can use the Read microphone sound level command in the "Onboard Sensors" under the "UNIHIKER" category to obtain the ambient sound level.
The recording sound level of the microphone is expressed as a percentage value within the range of 0 to 100 according to its sensitivity. The minimum volume is 0 and the maximum is 100. If the volume exceeds the maximum value, it will be recorded as the maximum volume. It should be noted that the onboard microphone can only provide a quantitative analysis of the volume level based on a percentage value.
5. Command Learning
In this project, we mainly use commands for reading microphone sound level, list operations, etc. Now let’s get to know them in detail.
Challenge
In this project, you have already completed the lung function test and the display of historical data. However, spirometry tests for students in a class usually require recording the measured data for each student. Now, try enriching your spirometer by completing the tasks below:
(1) Design a start-up page for your spirometer, which includes a function for selecting the student ID with clickable "+"/"-" buttons provided, and text for operation instructions, like the one shown below:
(2) Implement multiple tests. Press button A to start a test by entering the test page, which includes a progress bar, the student ID and corresponding historical data. Press button B to return to the student ID selection page and you can select a new ID.
Table of Contents for this Tutorial Series:
03 Journey to the West Stage Play
12 Vital Capacity Data Visualization