Wednesday, 2 August 2017

Introducing your Pi-rsonal Trainer

Since starting a new job earlier in the year I've had to make a lot of  adjustments to my fitness schedule. One thing I really miss are the exercise classes I used to attend. I particularly miss the High Intensity Interval Training sessions which were a great way get a quick cardiovascular workout.

I'd been rolling my own version at home in the back garden for a few weeks and found that although these sessions worked reasonably well,  I missed the class instructor who used to call out the next exercises and provide motivational commentary. Keeping track of what exercise I've just done gets increasingly difficult the more tired I get.  I also found it annoying to have to check my watch for when to pause between exercises and re-setting a timer every 20 seconds is annoying.

Obviously it would be hard to simulate the enthusiasm of a trainer but the exercise choice and timings seemed like a straightforward thing to automate with a Raspberry Pi.


I wanted a visual and audible alerting mechanism and something that would be small enough to be readily portable and run from a standard power bank.

Here is the finished prototype: my Pi-rsonal Trainer.


BoM

Pi Zero running Raspbian Jessie-lite and with the ScrollPhat HD Python library installed.
Pimoroni ScrollPhat HD
Pimoroni Pico HAT Hacker
Buzzer
Two push buttons

Construction



1) Solder a standard male header onto the Pi Zero.
2) Solder on the Pico HAT Hacker, being careful not to let the solder wick too far up the pins.
3) Solder a small buzzer directly on to the Pico HAT Hacker between the holes for Ground and GPIO 18
4) Solder two buttons onto the Pico HAT Hacker, one between Ground and GPIO 9 and the other between a different Ground pin and GPIO 19.
5) Solder a female header on to the ScrollPhat HD and then mount this onto the Pi.


Operation

For my HIIT sessions I like to have 4 repetitions of 10 x 20s periods of exercise interspersed with 10 second rests.

Some simple Python code runs at startup-up. When the bigger button (on GPIO19) is pressed, the sequence begins with an on/off flash of all the LEDs on the ScrollPhat . An exercise is selected at random from a list and this choice is scrolled across the matrix for 5 seconds, followed by a 5 second countdown accompanied by beeps. Then there's another flash and the 20 second exercise period begins. The exercise being undertaken is constantly scrolled across the LED matrix until the last 5 seconds when there's another beeping countdown. Then there's a 10 second rest period during with the next exercise is scrolled. And repeat 10 times.

Then a 30 second break.

Then repeat all that 4 times!


The LEDs of the ScrollPhat HD are nice and bright, and visible in full sunshine. The beeps are annoyingly shrill so that I can hear them even though I'll normally be listening to music on my earbuds. This means I don't have to keep watching the LED display to now when the various intervals are finished.

The code is easily customisable if you want different exercises or want to alter the timings. You could just as easily program a Yoga session rather than a HIIT workout!

Thursday, 9 February 2017

MakeTronix Alarm and GPIOZero on Raspberry Pi

I funded the MakeTronix Alarm on IndieGoGo because it contains many of the components I usd with Raspberry Pi at CoderDojo, but in one compact package. Don't get me wrong, I'm all in favour of having learners build the circuit using a breadboard, but sometimes there just isn't the time to do that and code it.

The board arrived very quickly and it is a nice, neat package. I was pleased that they kept the board to 26 pins as this will enable it to work with my trusty set of original mode Bs.

The online documentation is easy to find but I have to say I found it a little disappointing. It seems weird to not show a picture of the board mounted on a Pi in the “connecting the board” section. The written description is entirely accurate but a simple diagram would be much better, especially for younger users. I was also surprised to find that the code examples downloaded from the MakeTronix github don't use gpiozero. There's nothing wrong with using the Rpi.GPIO library – it works perfectly – but I believe  gpiozero is much better for those who are new to Python and this board is being marketed as a “fantastic educational circuit board for learning programming”. They've tried to abstract out the key functions into a library file that the other examples import, but this feels a bit unwieldy, again especially for novice Pythonistas.

I also couldn't get the full alarm code to work. It turned out this was nothing to do with the code: the PIR that shipped with my kit didn't work correctly and would not trigger on motion at any sensitivity. Luckily I have plenty of these PIRs and substituting in a replacement got everything working.

Nevertheless I decided to write a gpiozero version. It uses a couple of lambda functions which you might argue are too opaque for beginners (and I'd probably agree) but they do remove the need to give each button its own when_pressed function individually.

Here's why I love gpiozero – all sorts of hardware control is possible with simple, easy to understand syntax. My MTAlarm code does the following:

1. Activates the PIR and waits for motion to be detected. The LED glows/pulses to indicate that the alarm is active.

2. When motion is detected, the LED starts flashing.


3. If within 10 seconds  the correct code is entered (each key beeps when pressed and DEL acts as backspace) the alarm is disabled.






If not, the buzzer starts to beeeeeep constantly.