As we all know, exercising regularly can help shape your body and maintain good health. Among many fitness methods, running is widely loved by people as a simple and effective way to stay fit. However, the most difficult part of fitness is to stick with it. Today, we will use the UNIHIKER to make a fitness attendance tracker, set a goal, and record your running progress with the UNIHIKER.
The UNIHIKER counts the running steps and shows your progress on the screen in real time.
1. How to create a progress bar
2. How to use the accelerator sensor on the UNIHIKER
Download address: https://www.mindplus.cc
The main function of the fitness attendance tracker is to count steps and display running progress, which will be completed in two tasks.
Task 1: Get the number of steps
We will first learn how to use the accelerator sensor on UNIHIKER to count steps and show the
data on the screen.
Task 2: Display a progress bar
Then we will make a progress bar by drawing rectangles to display the running progress, which allows us to see how close we are to reaching our fitness goals.
Connect UNIHIKER to the computer via a USB cable.
Open Mind+, and complete the software preparation according to the figure below.
The step counting function on a mobile phone is usually based on the detection of motion like shaking of the phone. And the UNIHIKER has a similar function. Now, let’s see more details about it.
（1）Detecting the shaking of UNIHIKER
Shaking is basically a process in which objects suddenly change from the state of rest to the state of motion. How does the UNIHIKER know itself is being shaken now? Well, with its onboard accelerator sensor, that’s easy. We can obtain its acceleration value using the block Read acceleration value x, which can be found in the “Onboard Sensors” under the category of “UNIHIKER”. Drag it out and change “x” to “strength”.
Note: For more information about the UNIHIKER accelerator sensor, please refer to the Knowledge Base.
Now display the obtained acceleration values on the UNIHIKER screen in real time, and use the wait command Wait 0.2 seconds to slow down the data change for facilitating observation of the data before and after shaking.
When the UNIHIKER is in a still state, its acceleration strength stays around 1; When the UNIHIKER is being shaken, the data goes beyond 1.5.
Therefore, we can make it a rule that when the acceleration strength is detected to be over 1.5, it means the UNIHIKER is in motion and the number of steps should be added by 1.
Since we have learned how to determine if the UNIHIKER is being shaken, now let’s see how to count and display steps. First of all, create a variable and name it Actual steps. And set the actual step variable to 0 at the beginning of the program. When it is determined that the UNIHIKRE is being shaken, increment the variable and display its value on the display.
To make it clear, we can add a line of text above the step value. Adjust its coordinates, fonts, etc., to fit the whole scene. The complete reference code is as follows:
Click Run and then the words “Completed steps” and step numbers will be shown on the UNIHIKER screen. Shake the UNIHIKER board, and the number increases.
The step-counting function has been implemented in task 1. Next, let's display the sports progress bar to see how close you are to reaching your fitness goals. The progress bar consists of a hollow rectangular frame and a solid rectangle.
Here we use the rectangular outline width to represent the target number of steps, and the width of the progress bar to indicate the actual number of steps. As the actual number of steps increases, the progress bar will gradually move forward until it reaches the target number of steps. Therefore, when setting the coordinates of the progress bar, it is important to ensure that they match that of the rectangular frame and the solid progress bar should start with a width of 0.
But how does the progress bar width change? Well, we should handle this depending on how much the goal of sports has been completed. Since the completion ratio= actual number of steps / target number of steps, and the current width of the progress bar=completion ratio * the progress bar width. For instance, if the target number of steps is 1000 and the width of the progress bar frame is 160, then the current width of the progress bar would be "(actual number of steps / 1000) * 160". Now we can create a variable completion ratio and update the current width of the progress bar after calculation.
In the process above, if the actual number of steps exceeds the target progress, the progress bar will be out of the rectangle frame, as shown below.
To deal with that, we have to determine if the actual number of steps is greater than the target number 1200, if so, the width of the progress bar stays at 160, otherwise, it follows the previous rule.
The main function is done and the complete program is as follows:
Check the connection of UNIHIKER, and click RUN. Shake the board and see how the number of steps and the progress bar change.
We have done the basic functions of the project in this lesson. Now can you try enriching its interface and function?
（1）Design a background that fits the interface and adjust the content to make it attractive!
（2）Refer to the following figure to design a target setting page, on which users can modify the target step value with plus/minus buttons, and click the OK button to enter the step counting page to start step counting.
An accelerometer, as the name suggests, is a sensor used to measure acceleration. Since acceleration has both magnitude and direction, it is a vector quantity and can be either negative or positive. The direction of acceleration depends on if the object is speeding up or slowing down, and the direction the object is moving.
The UNIHIKER has a built-in accelerator sensor on its back.
The accelerator sensor on the UNIHIKER board is a three-axis accelerometer that can measure acceleration in the X, Y, and Z directions. When the UNIHIKER screen is placed horizontally facing down, the positive direction of the X-axis is towards the edge connectors, the positive direction of the Y-axis is towards the side of the Home button, and the Z-axis is perpendicular to the board, with the positive direction towards the back of the screen.
We usually use the command Read acceleration value x to obtain the readings of the accelerometer. The "strength" option in the drop-down menu represents the square root of the sum of squares of the numerical values of X, Y, and Z-axis accelerations, also √(X² + Y² + Z²).
The specific numerical changes of the accelerator sensor use gravitational acceleration as the reference. Taking the X-axis acceleration as an example, display the numerical value of the X-axis acceleration on the screen of the tablet, and observe the value changes. When you hold the UNIHIKER vertically with the edge connectors downwards, the X-axis is only affected by the vertical downward gravity, and the displayed numerical value on the screen is close to 1. When the edge connector is pointing upwards, the vertical downward gravity will be directed toward the side of the light sensor, and the displayed numerical value on the screen is close to -1.
When you place the UNIHIKER on a table horizontally, the x-axis will not be affected by gravity and therefore the value displayed on the accelerometer screen will be close to 0.
In this project, we mainly used the command of the acceleration sensor on the UNIHIKER. Now let’s get to know it in detail.
Please design a ball movement game where the ball appears randomly in any position, and is controlled by tilting the UNIHIKER to move up, down, left, or right. For example, if the ball appears on the far right, you need to tilt the board to the left to make it move to the left (as shown in the picture below).
If the ball reaches the center point of the screen, you win the game.
Table of Contents for this Tutorial Series：