In the previous post, we decided to start making a floor piano using a FLORA and some other bits. Here we're going to improve on the code and make it more permanent!
Step 3: improving the code
Before we go any further into making the sensors a permanent installation, we should fix the bugs I mentioned in step 2. We're going to do this using an averaging algorithm which applies a low pass filter.
Basically, this means that the baseline value of "pressed" or "not pressed" alters as time continues - whilst we could just use a regular average for this, the average could be affected by the second problem I mentioned regarding sensor value time delay, so the low pass filter will mean that changes to the sensor value don't change the average by a sufficient amount to be altered by this resistance problem.
The idea is pretty simple:
- Read in a value
- check it against the current average (with a small margin of error because 1 or 2 values either side could still be counted as "unpressed")
- if it's below it, play the note
- at the end of the loop, recalculate the average: this uses a "k" value, meaning that we take a certain amount of the old average into consideration, and a certain amount of the new sensor value into consideration when recalculating
This particular method is a form of windowing average, but one that works very well on micro controllers because we don't have to store all of the sensor values to figure out the current windowed average.
The code is below, or you can clone it from my Codebender sketch:
Step 4: Making the keys more permanent
Next up, we're going to lay the keys we made earlier onto a mat and make it permanent so that we don't have to carry around 4 sensors and use alligator clips for everything.
- Begin by cutting the right size material for the mat, including sufficient space to be able to position the FLORA and sew from the keys to the FLORA
- Spray adhesive over each area of the mat you're going to position the keys and stick the key sensors onto the adhesive, taking care to make sure the conductive threads coming from each key do not get stuck to the mat.
- Sew each pair of conductive threads into the mat, connecting one of each thread pairs to one of the Analog pins on the flora (D6-D10) and the other of the thread pairs to GND on the flora. I personally used Sewable snaps so that I could remove the FLORA for other projects, which you can read about on the Adafruit blog.
Your final mat should look something like this:
I haven't shown the buzzer here, or in the code, but that should plug in to a ground and the pin marked "SCL" on your flora - you can either solder this in or alligator clip it if you intend to reuse the FLORA for something else.
Step 5: the final code
Lastly, we need to modify the code written in step 4 so that it applies to each of the 4 keys - this is as simple as setting up a few new variables, as you can see below:
I will note that this still isn't completely bug free, and it will be a bit jittery - part of this is fiddling with margin of error and k values. If anyone lands on the perfect solution to make it not jitter so much, do let me know in the comments section!
PS: You can see the video of this project here: