Concept
Since both of us are video game fans and Undertale fans in particular, we decided to make a homage to Megalovania, a song from the game that has achieved fame as a meme.
Our project, then, was to make a keyboard whose notes only enable a playing of the first part of Megalovania (limited by the fact that we only had access to 8 buttons and 2 breadboards put together). We planned on using the Servo as a percussion component, controlled by a potentiometer.
Schematic
Program/Code
We have very long lines of comments and code, so the preformatted option ended up messing with the formatting. Instead, you can access the code on a GitHub Gist by clicking here.
Behaviour
Every button is (supposed) to play a different note, and since it is an instrument, the Servo serves the role of a metronome or beat-keeper of sorts whose speed can be modified with the help of a potentiometer.
Even though the keyboard is weird in that its notes are B2, A3, C3, D3, F3, G3, GS3, and D4, its whole point is to be able to play the first three bars of the score of Megalovania and nothing else (of course, there may be another song that you can reliably play using these specific notes, but our purpose was to make it for that one song).
Problems
On a first test with the buttons, the keyboard worked as it should. As we added the Servo and a second speaker, the notes stopped working, and no matter what button we pressed, the speaker would always produce the same sound. The one exception to the rule was the last button, that sounded like static. We tried changing variables, using digitalRead instead of the integer we had set up, started defining notes using their frequency values instead of their NOTE_ identity.
Then we tried using two speakers, splitting 4 notes for each speaker. The result was the same, except now we had 2 static buttons instead of 1. As we kept messing around with the code, eventually we had 3 static buttons instead of 1. Interestingly enough, they were always the last buttons. So first it was the one on pin 9, then pin 8, and then pin 7, while pins 2 through 6 kept producing the same note.
Then we tried using a new file. Two buttons controlling two separate speakers and playing one note each worked just fine. As more were added, however, the same thing happened.
At first we thought it was the Servo messing the file up, but as we got it up and running we found that everything remained the same, even though it started out working oddly once we added Serial.print to see what it was doing. It seemed like the velocity of the Servo was directly controlled by the value used in Serial.begin(). Once we removed all Serial commands, everything was good with the Servo, but the notes were still broken.
Sparkfun’s online tutorial for it used a weird arrangement where every button was connected in such a way that there was no need for 5V. The pin cable connected to the switch, then the switch connected to ground; no resistance or electricity. Their code, likewise, had an if statement relying on buttonState == LOW instead of the seemingly logical buttonState == HIGH. We tried this, but it didn’t work. A tutorial on YouTube used while statements to get it to work, but we also needed the Servo to keep running in a manipulable state, so we had to stick to if statements.
It is not a problem with the speaker, as we had thought: we set a code for it to just play notes at the setup without us pressing any buttons and it worked just fine. The problem arises at the time of having it play multiple sounds from multiple switches, even though there was a single instance where it worked just fine.
Your instrument is a creative twist on the common keyboard instrument. I’m glad that you posted the code on Github, it’s much better (and geekier). I wish we had time to rebuild your circuit and see if we could figure out what was going on. I’m pretty sure it has to do with the if() …else structure as we discussed. For instance:
if (digitalRead(pin_B2) == HIGH) // plays B2 if button is
but then
else if (button_C3 == 0) { // turns off B2 regardless of button B2, because button C3 is not pressed!
But you said you tried my suggestion and that didn’t help.
Your documentation does an excellent job of discussing problems. Nice work.
You should have provided a link to the SparkFun tutorial you mention. I suspect they were making use of something called an “internal pullup resistor”. Remind me in class and I’ll mention this – you’re not the first to run into this.
Regarding signifiers and intuitive interaction, how was the user supposed to understand the intention of this instrument? I.e. if you did not explain that concept, a user might assume that it was out of tune.