Github Link
Demo on Deema’s page
Concept
For this week’s assignment, we had to create a musical instrument that incorporates both digital and analog input. The concept we came up with was inspired by the floor piano tiles that play notes when you step on them. So, we built a similar concept where we created tiles that produce musical notes when stepped/pressed on AND we incorporated a potentiometer that changes the pitch of the note depending on how you twist and turn it.
We created three tiles in total. We built it so that no specific tile is linked to a specific note. Just that when you press any tile, a note plays; and if you step again on any other tile, the next note in the sequence plays.
Schematic
The Process
First, for the piano tiles, we decided to follow the same logic we used in the invisible switch assignment: foils attached to wires that generate power when they make contact, once that was complete, we moved on to incorporating the analog component. We placed the potentiometer and connected it to an analog pin, a 5V pin, and a GND pin.
For the code:
In order for the arduino to not translate every single LOW reading from a single step into multiple notes, we included a lastTileState component, where the buzzer will only translate the LOW reading to a note if the previous state of the tile was HIGH. This would prevent the tiles from playing multiple notes in one single press.
We had to figure out a way to change the pitch of the notes without completely changing what the note was. Since there is a trend among the notes where if you want to move from a note from higher pitch to a lower or vice versa, the note’s pitch value/frequency would need to either be divided by 2 or multiplied by 2.
For example: standard Do sound = C4; a lower pitch would be C3. The difference between the pitch of the two is that C4 is double C3
For that we mapped the potentiometer’s input values (0, 1023) to (50, 200) and divided by 100, this allows us to shift between notes and their pitches smoothly using the potentiometer.
We multiplied this factor to the original note that would have been played and set the tone to include the multiplied value as the frequency in the tone statement.
Originally I had the pitch and factor as just mapping the potentiometer values to the values of all the notes (31, 4978) so the note that is played would correspond to the values of the potentiometer. But then I realized this would change the notes altogether instead of just changing the pitches of the specific notes I chose, that’s when I used chatgpt and it suggested creating a factor component which is where I got the idea from.
I also initially tried keeping the analog and potentiometer section separate from the digital section of the code, adding a separate tone statement for the pitch and another one for the regular notes, but it caused an error, so I had to resort to figuring out a way to incorporate the pitch and analog section in the same section as the digital input.
I also tried incorporating a step counter within the if statement, but it practically added nothing and was not communicating anything to the code so I just removed it entirely.
Code Snippet
I’m personally proud of this if statement snippet because it helped me solve the problem of when to detect if the tiles were JUST pressed instead of if it’s pressed in general, which helped me avoid many problems. It also helped me to get rid of many variables I tried to include to keep track of the steps and presses.
if ( (tileOneState==LOW && lastTileOneState==HIGH) || (tileTwoState==LOW && lastTileTwoState==HIGH) || (tileThreeState==LOW && lastTileThreeState==HIGH) )
