Concept
After playing with different songs from the GitHub folder, we have decided to include several of them at the same time into our interpretation of a musical instrument. One way to do so would be to switch between three songs back and forth, creating a version of a radio with changing stations. Three classical compositions were chosen: Ode to Joy, Fur Elise, and Badinerie. However, this idea could be realised simply with two digital switches, so we decided to push it further by adding photoresistors that would allow us to work with a spectrum of values.
In what ways can a melody be altered while playing? Its volume or playback speed can be changed – as such, we decided to take values from one photoresistor to set up how loud the song was playing, and the other to set up the speed. A combination of all these electrical components helped us to create an advanced version of a radio, resembling a music player that allows to modify certain qualities of the sound.
Highlight of the code
:^)
Demonstration
Reflection
Although we did not expect this idea to be so complex, we have faced serious difficulties in implementing the functions of switches – the audio kept repeating itself without any response to the change of digital or analog values. After troubleshooting the whole code using Serial.print(), we decided to ask the professor for help in order to finally tackle the issue.
—
Update
After a week of troubleshooting, changing the Arduino code and circuit, we still couldn’t figure out a way for project to work as we intended. Therefore, we introduced new idea of a player that will reflect the mood of the room based on its lightning. So we integrated photoresistor and a button. Based on how lighted the setting was, if moderate to highly lighted, the piezo buzzer would play major melody (happy), and if lighting was low, minor melody (sad) was played. The button was used to stop the play of the melody completely. When the button is pressed, melody is silenced.
Here is a link to the Arduino code.
We think the most complex part with working on this new idea, just like it was on the previous one was working with buttons, since it had to stop the play of the melody. We could finally resolve this by introducing boolean variables and setting them throughout the code.
void loop() { int lightValue = analogRead(lightPin); buttonState = digitalRead(buttonPin); // checking if button is pressed if (buttonState == LOW && lastbuttonstate == HIGH) { isPlaying = false; noTone(buzzerPin); // stop playing when button is pressed } lastbuttonstate = buttonState; if (!isPlaying) { if (lightValue > 600) { playMelody(melody1, duration1, melody1Length); //playing happy melody when sensor value is higher than 600 } else { playMelody(melody2, duration2, melody2Length); //playing sad melody when sensor value is lower than 600 } } delay(100); } void playMelody(int melody[], int duration[], int length) { isPlaying = true; // setting playing state of the melody for (int i = 0; i < length; i++) { if (digitalRead(buttonPin) == LOW) { noTone(buzzerPin); isPlaying = false; return; } tone(buzzerPin, melody[i], duration[i]); delay(duration[i] * 1.3); //adding delay between notes } isPlaying = false; }
Here is the video of how it works: