Build a Microbit Exercise Counter/timer App!
by keble6 in Circuits > Microcontrollers
75 Views, 1 Favorites, 0 Comments
Build a Microbit Exercise Counter/timer App!
After a knee injury I had some physio exercises to do - and I have to keep doing them! I kept getting muddled with keeping track so I created some simple micro:bit code to help. Maybe it could help you too?
You need to keep count of the sets and reps as well as the timing. For example in one of my exercises I have to raise each leg in turn for 5 seconds, then relax for a short time, then repeat a number of times (the 'reps'). Then rest a bit more and repeat for a number of 'sets'.
I decided that using audible indications of when to start, raise, lower, and end, would make my life easier and be user-friendly. Take a look at a video clip here to see it in action. .
The code uses some built-in sounds that are available using a free complete coding app and simulator called Makecode for micro:bit.
I do include some display cues (the set and rep numbers), mainly to help debugging. Of course you can do more, such as up and down arrows if that's useful.
You can build this project on a physical microbit. Or you can use a simulated microbit on your device! Then it will look and feel like an app.
It's easy to change the numbers so if, like me, you have a series of exercises to do you can create separate files (apps) for each one.
The concept is extendable - for example if you code using Python then a speech synthesis library is available.
Supplies
We're going to use the free app to write simple block-based code. Here's where to get it for your platform:
The app includes a simulator which you can use just as it is.
Additionally, if you want to use a physical microbit, you'll need:
- a micro:bit V2
- The older V1 can be used, but you will need an external speaker
- a battery pack
- 2 times 1.5V AAA cells -
- ideally a battery holder (free with the microbit or as a template)
Some supply options are shown here
Code
- Start the makecode editor for your chosen platform
- Create a New Project and give it a name
- Copy my code from these three block diagrams, or import it from the attached hex file.
- Some notes on the code are below:
- The first block (on start) sets up some parameters for this app, such as the number of Reps NOTE times are specified in milliseconds
- The next block is the Sets loop which runs when button A is pressed. It calls a function doRep.
- The doRep block is another loop which does the sounds and timing.
- Note! makecode uses the command set to initialise a variable. It's an unfortunate name because it may seem to be the same as our Sets variable. But the code is case-sensitive so there is no confusion!
Help in the form of tutorials is available directly from the makecode editor.
Downloads
Test Your Code on the Simulator
You start the app by clicking on the simulator button marked A.
Did it work as you expected? If not please check your code carefully and correct any mistakes.
Edit the Code to Suit Your Needs.
Now that it's working try making any changes you want and test them.
When you are happy you are good to go!
You can save and download your code to a real microbit or make copies for additional exercise routines.
Download to a Physical Microbit (optional)
There are many online tutorials - this one should help.