Overview
I’m one of those people who enjoys working out. However, I never seem to be able to remember how many times I've done an exercise. This is especially true with things like jumping jacks, where you are doing a lot of repetitions. Did I do twenty-three or thirty-three? It’s just counting, but I always seem to lose my place.
So, in this blog post I’m going to use the OLinuXinoOLinuXino to build a device that will automatically count the number of repetitions that I've done. This way I’ll never lose track again! To do this, I’m going to build a trip wire and position it so that when I go through the range of motion for the exercise that I’ll set off the trip wire. I’ll then count how many times the trip wire has been activated and use that to keep track.
The system looks something like this:
Parts
Quantity | Description |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 |
Hardware
First, build the circuit shown below:
This circuit is set up so that when the laser pointer is shining on the light dependent resistor (LDR) that the GPIO pin will read zero. When the light goes away, the GPIO pin will read one. The way that I think about it is that the GPIO pin answers the question: Is there an object between the laser and the sensor? If the answer is yes, then the output is one. If the answer is no, then the output is zero.
To connect the circuit to the OLinuXino, I used the following GPIO pins on the GPIO-2 connector:
Circuit Label | GPIO Pin# | Signal Name |
+3.3V | 3 | 3.3V |
GPIO | 9 | PIN6 |
GND | 4 | GND |
A description of the pins on the GPIO-2 connector can be found on page 24 of the user manual, reproduced here:
Here’s an image of where to connect the circuit to the OLinuXino:
Where the red wire connects to 3.3V, the green wire connects GND, and the black wire connects to GPIO PIN6. I didn't show the circuit, because the circuit diagram above is much clearer to figure out what is going on.
I also have two receivers plugged into the USB ports. One of them is for a wireless keyboard and mouse; the other is for wifi.
Now comes the hardest part, lining up the laser pointer so that it will shine on the light dependent resistor. This can be tricky since the beam of the laser pointer is so small. Also, this needs to be done in such a way that the natural range of motion for the exercise will break the beam when it is performed, but only when the exercise is performed. (Not when you are resting.) Sometimes this can be tricky to find a point that will always be passed through. For jumping jacks, I found that positioning it so that it detected when my arms came up worked well. This had the side effect that it would detect when I raised and lowered my arms. This means that I would get double credit for every rep! That’s not what we want, so with a small software modification, we can count every other detection.
Here’s a video showing the overall system:
Software
Now we need to read the GPIO pint that that laser is shining on (GPIO PIN6) to determine whether or not there is an object blocking the path. If you haven’t used the GPIO pins, or need a refresher, check out this article.
Here’s the code that counts the number of reps that have been completed:
The code starts out by initializing the GPIO library and then setting GPIO PIN6 to be an input pin, since we are going to read whether or not there is an object blocking the path of the laser.
Next we set up some variables to track what is going on. First we specify a variable (mult) that stores how many times the laser has to be broken in order to count a rep. In this case, since my arm is going to break the laser connection on the way up and down, we set this value to two. Next is a counter (raw) that stores the raw number of times an object has been detected. Then comes the variable (reps) that stores the number of reps that have been completed. Finally, we store the previous state of the light sensor in a variable (prev). This way we can tell when an object enters or leaves the lasers path.
Then we go into an infinite loop collecting reps. The first thing that we do is read the current state of the sensor. This state is then compared with the previous state. If there is no change, then we just continue on. However, if the state has changed then we determine whether or not this is an object entering the laser’s path or leaving it. Leaving it would be indicated by the prev variable having a value of one and the val variable having a value of zero. This is what we are looking for so we increment the raw counter. The next if statement does the down sampling of raw repetitions. This is where we enforce the rule that for every two object detections, it counts as one repetition. When this happens, we increment the rep count and print out a message to the user notifying them that they successfully completed a rep.
The final set of statements in the loop just updates the previous value to be the current value and then sleeps for a bit before continuing on to the next loop.
Here’s a video of the system in action, counting the number of jumping jacks that I’m performing:
Summary
We used a trip wire to detect the number of reps that were performed when working out. To do this, we cleverly positioned a laser pointer so that the path would be obstructed when we did an exercise. We then used a light dependent resistor and the OLinuXinoOLinuXino to detect an object passing through the laser’s path. Finally, we used this information to count the number of reps that were completed. No more losing track and having to start over!