After having worked with a piezo speaker, we wanted to hear some real music and not just synthesized one. Our idea was to use a Music Shield v2.0 which is a device that can be plugged on Arduino and that plays real music. In order to achieve that, you have to insert a micro SD card with music files on it and to connect the shield to a speaker. It is very easy to use and quite cheap to produce. More details about it on: http://www.seeedstudio.com/wiki/Music_Shield_V2.0.
We first learnt how to use the Shield on its own. We added a library to the libraries of the Arduino software. We found it on github: https://github.com/Seeed-Studio/Music_Shield.
We simply used the code given as an example in the library of the Music Shield:
File -> Examples -> MusicPlayer -> createList
Then we adapted the code to be closer to the prototype and created a circuit with the light-to-frequency converter (LTF) which is used to detect Arsenic concentration. (Link to the code)
The first problem we encountered was that it only counted light intensity once; then the music stopped and it also stopped counting. It seemed to us that there was no link between the music played and the counter. Then we learnt that the problem was linked to the TIMER: the Music Shield we were using used the TIMER1 of the arduino that was already used to count the output of the LTF.
After a discussion with Robin Scheibler, he proposed us three different options to solve this problem:
1) Use an interrupt to measure the output of the LTF. (Link to the code)
2) Modify the music shield library to use TIMER2 instead of TIMER1.
3) Use two arduinos: one for the music and the other to measure light. (Link to the code)
We tried the first option by adapting our previous code with the code of interruptCounter. The advantage of Interrupt is that: «Interrupts are useful for making things happen automatically in microcontroller programs, and can help solve timing problems. » (Arduino.cc)
But this solution did not solve our problem: the songs were often not found on the SD cart and it seemed that there was still no link between the music being played and the counting as if both were acting completely independently. And the program was finally always interrupted in a random way.
We decided that this solution was not the right one, thus we decided to work on the second one. We tried to modify the library of the MusicPlayer to put the arduino shield on TIMER2 (instead of TIMER1). This option revealed to be too complicated for us since we did not understand all the methods related to the timer and how exactly we had to change them. The documentation found on the web about this was also quite limited.
Therefore the only solution left was the third one: to use two different arduinos. This way there would be no more problem with the timers because each arduino has a TIMER1. The communication between the two arduino consist of one being a Master, which gives orders to the other, the Slave. We decided that the Master had to be the one with the LTF, and the Slave the Music Shield.
The first problem was the fact that we only used a unidirectional transmission. The master was telling the slave to play music, but just went on counting without checking if the song had been played entirely. The result was that it switched between the musics quite rapidly without being completely played.
We came up with two solutions: the first one (the easiest one) was to put a delay so that the music had time to be played. This solution worked well but didn’t allow the modulation of the code: the delays would have to be adapted to each song we want to play since they don’t have the same duration. The second solution, which seemed more accurate, was to create a bidirectional communication between the two arduinos:
- Transmit to Slave when to play a specific music
- Request from Slave when the song is over
- Receive from Master when to play a specific music.
- Send to Master when the song is over.
To determine when the song was over, we had to add a method in the class of the MusicPlayer library. This method returns a byte if the song has been totally played and returns 0 byte if it has not.
After many trials and fails we finally managed to produce a reliable connection between the counter and the shield. They are both considering the state of the other before going further. The Counter is waiting for the Music shield to play the entire song, and the Music Shield is waiting for the Counter before playing a new song and to choose which song to play.
The next step will be to adapt this code to the prototype. Now that there is a clear working interaction between the two arduinos, it will become easier to continue.