Push the button, it screams. Turn the knob, and it screams even louder. For this week, we decided to create a sound/musical instrument that is controlled by a button and potentiometer.
Concept
We decided to call our machine, “Mr.Problematico” because of the issues we encountered while building this machine. The premise is simple: We have a potentiometer that controls the pitch and a button that plays/stops the music.
How it works
In the code, we mapped the potentiometer to 20 different notes. Then, we created an if-else statement, which plays a music function playMusic, whenever the button is pressed and does not play the music whenever it is not pressed.
In the music function, we played the note based on the value received by the potentiometer (value) and turned on the light as well.
// Map the potentiomenter values according to the list length. int note = map(pontValue, 0, 1023, 1, 21); // If button is pressed, then play a sound according to the potentiometer. if (buttonState == HIGH) { playMusic(note); //Serial.print("HIGH \n"); } else if (buttonState == LOW) { //Serial.print("LOW \n"); } } //Play the tune, wait a specific time and light the LED according to the //arrays of melodies and duration. In other words, the LED and the sounds generated are synchronized. void playMusic(int value) { int noteDuration = 1000 / noteDurations[value]; digitalWrite(13, HIGH); tone(8, melody[value], noteDuration); digitalWrite(13, LOW); delay(noteDuration); }
Closing Remarks
During our building time, we spent a lot of hours trying to figure out why our machine decided to stop very long when we executed the music function. After some consultation, the delay function delays the whole machine rather than the code line. We tried to play and go around trying to fix this weird delay. We figured out that by missing a resistor to the button that messed up the whole circuit, before eventually creating the machine above. It was not completely the code’s fault but rather our miss information.
We’re quite proud of this project. Looking forward to the final project!