![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
miniblip play notes with interrupts with variable volume
Fork of blip_playnotes by
Revision 3:2af917fe09c3, committed 2015-12-04
- Comitter:
- JCristobal
- Date:
- Fri Dec 04 10:11:15 2015 +0000
- Parent:
- 2:85d8ce475baa
- Commit message:
- miniblip play notes with interrupts with variable volume
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 85d8ce475baa -r 2af917fe09c3 main.cpp --- a/main.cpp Thu Nov 26 09:28:03 2015 +0000 +++ b/main.cpp Fri Dec 04 10:11:15 2015 +0000 @@ -4,24 +4,72 @@ #include "mbed.h" #include "SongPlayer.h" -//Set up notes and durations for sample song to play -// A 0.0 duration note at end terminates song play -float note[18]= {1568.0,1396.9,1244.5,1244.5,1396.9,1568.0,1568.0,1568.0,1396.9, - 1244.5,1396.9,1568.0,1396.9,1244.5,1174.7,1244.5,1244.5, 0.0 - }; -float duration[18]= {0.48,0.24,0.72,0.48,0.24,0.48,0.24,0.24,0.24, - 0.24,0.24,0.24,0.24,0.48,0.24,0.48,0.48, 0.0 - }; +// Potentiometer to handle the volume +#define ANALOG_POTENTIOMETER P0_22 +AnalogIn ain(ANALOG_POTENTIOMETER); + +#define MUTE 0.0 +#define END 1.0 + +//-- Notes and frequencies (Hz) +#define DO_4 261.626 +#define MI_4 329.628 +#define SOL_4 391.995 +#define SI_4 493.883 +#define DO_5 523.251 + +//-- Tempo +#define TEMPO 200 + +//-- Default volume +float VOL = 0.5; + +//-- Notes for the imperial march. MUTE means silence. END for marking the end +float notes[] = {MI_4, MUTE, MI_4, MUTE, MI_4, MUTE, DO_4, SOL_4, MI_4, MUTE, DO_4, SOL_4, MI_4, MUTE, + SI_4, MUTE, SI_4, MUTE, SI_4, MUTE, DO_5, SOL_4, MI_4, MUTE, DO_4, SOL_4, MI_4, END}; + +//-- Notes duration +float tempo[] = {2 , 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 3, + 2 , 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 0}; + +//-- Declare speaker object +PwmOut speaker(P0_8); + +void play() +{ + int i=0; + //-- Reproduce all the notes in the list + while(notes[i] != END) { + + //-- If silence, turn of the PWM + if (notes[i] == MUTE) { + speaker = 0.0; + } + else { + speaker.period(1.0/notes[i]); //-- Note + float potenciometro = ain.read() * 0.7f; // Handle the volume with the potentiometer + speaker = potenciometro; //-- Set the volume + } + + //-- Note duration + wait_ms(TEMPO * tempo[i]); + + //-- Move to the next note + i++; + } + + //-- Turn off the speacker + speaker = MUTE; +} + int main() { - // Buzzer pin - SongPlayer mySpeaker(P0_8); - // Start song and return once playing starts - mySpeaker.PlaySong(note,duration); - // loops forever while song continues to play to end using interrupts - while(1) { - wait(.1); - } + + while(1){ + // Play the song!! + play(); + wait_ms(500); + } ; }