2. Practical functions: There is an awesome function on the small bit - accelerometer. It can achieve a lot of fun functions, now we use this function to make a pedometer.
3. Programming ideas.
Wrong idea: there is a default vibration monitoring block in the component, the direct programming idea is to add 1 to the step variable when vibrating. but the actual use you will find that the measurement value is very inaccurate, there must be a large vibration to record, and the measurement interval is also too long, walking faster will miss the meter, no practical value.
Improvement ideas: In order to accurately count steps we need to reasonably improve the sensitivity to ensure that the vibration of the walk is recorded while also ignoring smaller vibrations. Micro:bit can accurately obtain its own gravitational acceleration value, we calculate the difference between the two acceleration values according to a certain time interval, if the absolute value of the difference is greater than a suitable value is considered to have taken a step, step variable plus 1.
Press the A key to display the step value.
After programming, the time interval and acceleration difference value need to be modified repeatedly by actual measurement, and the monitoring effect of the program can be improved by debugging.
The time interval can be pre-estimated by taking 100 steps and measuring the average time, my own average time is about half a second.
Acceleration difference value is finally determined between 100 and 200 is more appropriate, too big easy to miss steps, too small easy to count more.
4. programming implementation.
(1) Set the variables: step number, X1, X2, used to record the acceleration value twice.
(2) Initialization: the number of steps is zero, X2 records the initial acceleration value once.
(3) infinite loop: record the acceleration value X1, calculate the absolute value of the difference between X1 and X2, and judge that it is greater than 150 that calculates one step.
(4) Set the value of X2 to X1 as the basis for a new calculation.
(5) Pause for 550ms and wait for the next cycle.
(6) Press A to display the current step number.
Transfer the hex file into Micro:bit, connect the battery box, and actually measure several times to see if the value setting is reasonable and optimize it for your situation.
PI-SUPPLY is a professional component supplier, providing everything you need for your component project. We also stock a huge selection of Raspberry pi, Arduino, micro:bit, rock pi and nvidia manufacturer accessories from your favorite brands.