Output: Difference between revisions

From Control Systems Technology Group
Jump to navigation Jump to search
No edit summary
No edit summary
Line 14: Line 14:
To create sound, a simple speaker or a piezo buzzer will do, like this one [http://www.lightinthebox.com/nl/compatibel-arduino-passieve-luidspreker-zoemer-module_p903345.html?currency=EUR&litb_from=paid_adwords_shopping&utm_source=google_shopping&utm_medium=cpc&adword_mt=&adword_ct=87561882434&adword_kw=&adword_pos=1o1&adword_pl=&adword_net=g&adword_tar=&adw_src_id=1810908567_301588514_21687237914_aud-141409042634:pla-117284547555&gclid=CL3Op-jcvssCFcZAGwodaYwGVA here] (seen in the picture to the right). The speaker is simply connected to an output pin of the Arduino and vibrates in accordance with the voltage on that pin, thus creating sound.
To create sound, a simple speaker or a piezo buzzer will do, like this one [http://www.lightinthebox.com/nl/compatibel-arduino-passieve-luidspreker-zoemer-module_p903345.html?currency=EUR&litb_from=paid_adwords_shopping&utm_source=google_shopping&utm_medium=cpc&adword_mt=&adword_ct=87561882434&adword_kw=&adword_pos=1o1&adword_pl=&adword_net=g&adword_tar=&adw_src_id=1810908567_301588514_21687237914_aud-141409042634:pla-117284547555&gclid=CL3Op-jcvssCFcZAGwodaYwGVA here] (seen in the picture to the right). The speaker is simply connected to an output pin of the Arduino and vibrates in accordance with the voltage on that pin, thus creating sound.


To simulate the production of sound, a quick script has been written in Matlab (because Matlab is easier to use). This script contains two alarm sounds which are both modeled as sine functions but with different frequencies. The first alarm sound, Alarm1, begins playing at time tStart, which is the right time to wake up the user as calculated by the main script. This alarm would ideally be a natural and more relaxing sound, like chirping birds, but that's probably nearly impossible to do with the tone() function, so we'll have to find something else. The intensity of this sound rises gradually and reaches its maximum at tEnd, the moment the user MUST wake up. Or it stops at tWake, which is the moment the user wakes up. If he hasn't woken op yet, Alarm1 stops playing and is replaced by Alarm2, which will be a standard alarm noise. The intensity is set on its maximum to make sure the user wakes up. When he does, at tWake, the sound stops. The script is written below and the result can be seen in the pictures to the right. The blue graph represents Alarm1 and the red one Alarm2.
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. A script has been written in Matlab to test the sound output system before translating it to the Arduino. The function "audioread()" translates the sound files into samples and a sample frequency. Both sound files contain two sets of samples or channels. It seems that each channel is meant for one ear of the headset, as they are only slightly different to each other. For the Arduino we'll have to use one of these channels, or preferably combine them. The samples can be used as a voltage output for the pin on the Arduino that's connected with the speaker. They have a range of [-1, 1], so by multiplying the samples with a factor fmax, the range can be set to [-fmax, fmax]. The value of fmax is by default 3. The Arduino has a maximal output voltage of 5 V, so the maximal value for fmax is 5. The sound needs to be audible, so the minimal value of fmax, fmin, is by default 1. Testing with the Arduino and the speaker must determine whether the sound is still audible when fmax equals 1 or whether the value of fmin could be set lower.
 
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.


=== Script ===
=== Script ===

Revision as of 01:02, 21 March 2016

Back to main page: PRE2015_3_Groep4

To the input: Input

To the code: Code

Light Brightness

Heating

Play Sound

The SparkFun MP3 Player Shield

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.

Buzzer module for Arduino

To create sound, a simple speaker or a piezo buzzer will do, like this one here (seen in the picture to the right). The speaker is simply connected to an output pin of the Arduino and vibrates in accordance with the voltage on that pin, thus creating sound.

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. A script has been written in Matlab to test the sound output system before translating it to the Arduino. The function "audioread()" translates the sound files into samples and a sample frequency. Both sound files contain two sets of samples or channels. It seems that each channel is meant for one ear of the headset, as they are only slightly different to each other. For the Arduino we'll have to use one of these channels, or preferably combine them. The samples can be used as a voltage output for the pin on the Arduino that's connected with the speaker. They have a range of [-1, 1], so by multiplying the samples with a factor fmax, the range can be set to [-fmax, fmax]. The value of fmax is by default 3. The Arduino has a maximal output voltage of 5 V, so the maximal value for fmax is 5. The sound needs to be audible, so the minimal value of fmax, fmin, is by default 1. Testing with the Arduino and the speaker must determine whether the sound is still audible when fmax equals 1 or whether the value of fmin could be set lower.

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.

Script

clear variables;close all;clc;

%% Alarm sounds
Alarm1 = 'Birds-singing-in-the-morning.mp3';% The 'Smart Alarm'
Alarm2 = 'Loud-alarm-clock-sound.wav';      % The 'Standard Alarm'
[y1,Fs1] = audioread(Alarm1);               % Alarm 1 translated into sound samples y1 and sample frequency Fs1
[y2,Fs2] = audioread(Alarm2);               % Alarm 2 translated into sound samples y2 and sample frequency Fs2

%% Feedback matrix
Fb = [];                                    % n*2 matrix which contains the user's feedback
Fbmax = 5;                                  % Maximal number of rows of the feedback matrix

%% Maximal sound amplitude boundaries
fmin = 1;                                   % This variable is to make sure that the alarm always makes an audible sound
fmax = 3;                                   % Maximal sound amplitude in volts (for the speaker)

while 1
    %% Use Smart Alarm? (Alarm 1)
    Smart = input('Use Smart Alarm?\n 1 = Yes\n 0 = No\n');
    
    %% Wake up interval/time
    if Smart
        tStart = input('Start of the time interval the user wants to wake up: ');
        tEnd = input('End of the time interval the user wants to wake up: ');
    else
        tWake = input('Time the user wants to wake up: ');
    end

    %% Alarm 1 properties
    if Smart
        y1max = Fs1*(tEnd-tStart);          % Number of samples to be used during the 'wake up interval'
        df = fmax/y1max;                    % Step size of the 'multiplication factor array'
        f = df:df:fmax;                     % Multiplication factor array: makes sure the Smart Alarm gradually increases in volume
    end

    %% Play alarm 1
    if Smart
        pause(tStart);                      % Wait until it's time to turn on the Smart Alarm
        disp('tStart');
        sound([f' f'].*y1(1:y1max,:),Fs1);  % Play the Smart Alarm which gradually increases in volume due to f
        pause(tEnd-tStart);                 % Wait until the user must be woken up
        disp('tEnd');
        clear sound;                        % Stop the Smart Alarm if it hasn't stopped yet
    else
        pause(tWake);                       % Wait until it's time to wake up the user
        disp('tWake');
    end

    %% Play alarm 2
    sound(y2,Fs2);                          % Play the standard alarm
    pause;                                  % Wait until the user is awake and presses a button
    clear sound;                            % Stop the standard alarm
    
    %% Stop script?
    Stop = input('Stop script?\n 0 = No\n 1 = Yes\n');
    if Stop
        break;                              % Stops the script
    end
    
    if Smart
        %% Feedback
        Fb(end+1,1) = fmax;                 % Give feedback about this volume setting
        Fb(end,2) = 3 - input('Rate your sound experience:\n 1 = Too soft\n 2 = Soft\n 3 = OK\n 4 = Loud\n 5 = Too loud\n');
        if size(Fb,1) > Fbmax;              % Feedback matrix can't be larger than Fbmax rows
            Fb = Fb(2:end,:);               % Delete first row
        end

        %% Maximal sound amplitude
        fmax = mean(Fb(:,1)+Fb(:,2));       % New maximal volume is determined by looking at the total feedback
        if fmax < fmin                      % Maximal volume needs to have a certain level so that the sound is still audible
            fmax = fmin;
            disp('Notice: minimal sound level reached');
        end
        if fmax > 5                         % Maximal voltage for the speaker is 5 V
            fmax = 5;
            disp('Notice: maximal sound level reached');
        end
    end
end

Feedback Statistics

Graphic Simulation