From selecting a song to watching our motor and needle move to the playing song, our software and firmware control it all. We made use of Arduino software and Python's Pygame library to help our SoundCrystal perform. Learn more about our system below and check out our source code in our Github! Files in GitHub are organized by either Arduino or Python, and the final code on our primary Arduino goes by LCD_and_Buttons_and_Servo.ino. Our final Python code goes by musicplayer_with_buttons.py.
Our Arduino takes input from 5 buttons, and occasional input from the Python Music Player in order to give output to the LCD screen, DC motor, servo, and music player:
Activation Button: This button activates our system, not allowing a user to use the music buttons until the marble run has activated the system.
Back/Forward Buttons: These two buttons help a user scroll through the song list, displayed on the LCD screen.
Song Select Button: This button tells the Arduino to send a message through Serial notifying the music player that the user has selected a particular song and to load that song in the music player.
Play/Pause Button: This button tells the Arduino to send a message through Serial to either play the loaded song, or to pause the song if one is already playing. Depending on whether the signal has been given to play or pause, the needle tilt and record spinner will receive an output signal from the Arduino to move accordingly.
When a song ends, the Python Music Player will give a signal to the Arduino that also resets the DC motor and needle tilt servo.
music player
For our music player, we used the Python library, PyGame, to control the playback of music. There are three major components of the code: MP3 file detection, arduino communication, and button responses.
MP3 File Detection: This part of the script finds all the MP3 files in a folder given the path. It can be modified to find .wav files instead. When the files are found, it is compiled into a list and this song list is used in the main code.
Arduino Communication: With two simple lines of code, we can communicate with the primary Arduino. By being able to communicate with the Arduino, we are able to read the Arduino's serial output and write things to the Arduino's serial.
Button Responses: By using Arduino communication, we are able to see what button has been pressed on our SoundCrystal to respond accordingly. Every time a button is pressed, the serial port outputs a number and python runs an IF statement for that button. Button 1 and 4 (back/forward button) scroll through the song list. By clicking these buttons you change the index of the song list, therefore changing the MP3 file on tap. This file corresponds to the song choice shown on the SoundCrystal's LCD screen. If Button 2 (select button) is clicked, that MP3 file is loaded. Button 3 (play/pause button), allows you to play and pause the song that has been selected.
When a song has ended, Python writes to serial to ensure that the motor and needle tilt servo are reset.