Output
Back to main page: PRE2015_3_Groep4
To the input: Input
To the code: Code
Quick overview
The script called "OutputFunc_V3.m" controls the output of the system, which consists of making sounds and turning on lights. It has gone through several changes, with different versions having different pros and cons. This script is version 3, hence the suffix "_V3", and is the first and only script with an acceptable runtime, but previous versions would have been preferred if certain tasks didn't take so much time to complete. Nevertheless, version 3 does a very good job.
We've looked at different possibilities for producing sounds. Ideally, we would have connected a speaker to the Arduino, because then it would potentially be able to do all the work, without the aid of a laptop. However, that would require programming the unit in C++, in which most of us are inexperienced, and the Arduino has very limited memory space, so therefor it had been decided to use Matlab and the laptop's speaker instead. The Arduino is used to produce light, though, with added on LEDs. The sound files used for the smart alarm and the standard alarm are "Birds-singing-in-the-morning.mp3" and "Loud-alarm-clock-sound.wav" respectively, both of which have been downloaded from http://www.orangefreesounds.com/. The sound of the smart alarm can be customized by copying a sound file of your own choice to the folder containing the script and changing the filename of the smart alarm, "Alarm1", in "runscript.m". The script is capable of running with sound files of all sizes, but users are advised to use files that aren't too long, because it greatly influences the runtime of the script. The audio of "Birds-singing-in-the-morning.mp3" is about 15 minutes long, so the script is guaranteed to work with sound files of 15 minutes long or less. The standard alarm could be customized in the same manner. However, the script doesn't take into account how long the audio is and just repeats it a number of times.
The output script is initiated when "Complete_script.m" finds a suitable moment to wake up its user. The script is a function with 6 parameters: a connection with the Arduino, the name of the pin that controls the LEDs, the file names of the smart and standard alarm, the moment the user must be awake (the moment the standard alarm is used as a last resort to wake up the user) and finally a vector containing the number of seconds in a second, a minute, an hour and so on, which is used to translate the time and date into seconds.
Light Brightness
Play Sound
The best way to produce sound with an Arduino unit is probably by using the Sparkfun MP3 Player Shield, which is an extra circuit board for the Arduino which can play MP3 files from a microSD card (seen in the picture to the right). The costs, however, are $25, which is too much, so we chose a cheaper option. More info about this product can be found at https://www.sparkfun.com/products/12660.
Since we didn't think it is needed to buy a MP3 player shield we just used the laptop to play sounds. Matlab is doing all the calculations anyway so using the laptop to play sound is more convenient.
Two sound files have been downloaded from the internet to serve as an alarm: the file "Birds-singing-in-the-morning.mp3" is a relaxing audio with, as the name suggests, singing birds and a bit of wind. This file will serve as the Smart Alarm that softly wakes the user up. The file "Loud-alarm-clock-sound.wav" is a standard alarm sound, which will naturally serve as the standard alarm that urges the user to wake up.
When running the script, the user is first asked whether he wants to use the Smart Alarm at all. If he does, the user is then asked to give the time interval (in seconds) in which he wants to wake up. If he doesn't, the script only asks for the time the user wants to wake up. When using the Smart Alarm, the sound must gradually increase over the course of the 'wake up interval'. This is done by creating an array called f filled with linearly increasing numbers between 0 and fmax. The length of f is equal to the number of samples of the Smart Alarm sound file necessary to fill the wake up time interval. This array is then multiplied with y1, the matrix with the sound samples of the Smart Alarm. Thus during the wake up time interval, the sound increases from a maximal amplitude of 0 to a maximal amplitude of fmax. The sound is produced by using the function sound(). Using the function pause(), the script waits until the end of the wake up interval and then stops the sound, after which it starts to play the standard alarm. By using the function pause (without parameters), the script waits until the user presses a button to continue. Afterwards, the user is given the option to stop the script or continue. If he chooses to continue, he can then give feedback on the Smart Alarm (if this was enabled). The feedback consists of choosing between "Too soft", "Soft", "OK", "Loud" and "Too loud", for which the script assigns the values 2, 1, 0, -1 and -2 respectively. This number is added to the value of fmax and stored within the array Fb. The new value for fmax is determined by taking the mean of this array. To make sure that only the most recent feedback is taken into account, and limit the memory required, the maximal length of this array is set to a value called Fbmax, which is for now set to 5. When surpassing this limit, the script deletes the first element of the array Fb. The script also makes sure that the value of fmax stays between fmin and 5, and it gives a notification when it does so. From there on, the script keeps repeating itself until the user decides to stop it.
The actual script is slightly different than described above, but the description clearly shows the thought process behind this.