plays a fucking annoying sound for 10 seconds and loops.

Dependencies:   mbed

Fork of song_demo_PWM by jim hamblen

Files at this revision

API Documentation at this revision

Comitter:
mganseij
Date:
Tue Oct 27 13:30:31 2015 +0000
Parent:
2:2cca82c058fc
Child:
4:64c31855f0be
Commit message:
test shit

Changed in this revision

SongPlayer.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
tones.h Show annotated file Show diff for this revision Revisions of this file
--- a/SongPlayer.h	Tue Oct 27 10:06:16 2015 +0000
+++ b/SongPlayer.h	Tue Oct 27 13:30:31 2015 +0000
@@ -1,4 +1,5 @@
 #include "mbed.h"
+bool keepplaying = true;
 // new class to play a note on Speaker based on PwmOut class
 class SongPlayer
 {
@@ -30,12 +31,14 @@
 //Interrupt Routine to play next note
 void SongPlayer::nextnote()
 {
-    _pin = 0.0;
-    notecount++; //setup next note in song
-    if (durationptr[notecount]!=0.0) {
-        _pin.period(1.0/frequencyptr[notecount]);
-        noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]);
-        _pin = vol/2.0;
-    } else
-        _pin = 0.0; //turn off on last note
+    if(keepplaying==true){
+        _pin = 0.0;
+        notecount++; //setup next note in song
+        if (durationptr[notecount]!=0.0) {
+            _pin.period(1.0/frequencyptr[notecount]);
+            noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]);
+            _pin = vol/2.0;
+    }   else
+            _pin = 0.0; //turn off on last note
+    }
 }
\ No newline at end of file
--- a/main.cpp	Tue Oct 27 10:06:16 2015 +0000
+++ b/main.cpp	Tue Oct 27 13:30:31 2015 +0000
@@ -1,32 +1,75 @@
 #include "mbed.h"
+#include "tones.h"
 #include "SongPlayer.h"
-#include "tones.h"
+
+// I apologize in advance for this fucking mess of a code
 
-// Song test program - plays a song using PWM and timer interrupts
-// for documentation see http://mbed.org/users/4180_1/notebook/using-a-speaker-for-audio-output/
-// can be used to play a song, if you have the notes and durations
-// for musical note frequencies see http://en.wikipedia.org/wiki/Piano_key_frequencies
+DigitalIn input(D15); 
+PwmOut speaker(A5);
+SongPlayer mySpeaker(A5);
+Timer songTimer;
+
+bool playbuzz;
 
-//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
-//                    };
+void evalbuzz()
+{
+    if(input == 1)
+    {
+        playbuzz = true;
+    }
+    else
+    {
+        playbuzz = false;
+    }
+}
 
-//DigitalOut led1(LED1);
+void buzz()
+{
+    for (int i=0; i<26; i=i+2)
+    {
+        evalbuzz();
+        if(playbuzz == true)
+        {
+            speaker.period(1.0/3800.0);
+            speaker = float(i)/50.0;
+            wait(.2);
+            speaker.period(1.0/4200.0);
+            wait(.2);
+        }
+        else
+        {
+            songTimer.reset();
+            mySpeaker.PlaySong(note,duration);
+            break;
+        }
+    }
+    
+}
+
 int main()
 {
-// setup instance of new SongPlayer class, mySpeaker using pin 26
-// the pin must be a PWM output pin
-    SongPlayer mySpeaker(A5);
-// Start song and return once playing starts
+    songTimer.start();
     mySpeaker.PlaySong(note,duration);
-    // loops forever while song continues to play to end using interrupts
-    while(1) {
-        //led1 = !led1;
+    while(1)
+    {
+        if(input == 0)
+        {
+            keepplaying = true;
+        }
+        else
+        {
+            keepplaying = false;
+        }
+        if(input == 1)
+        {
+            playbuzz = true;
+            buzz();
+        }
+        if(songTimer.read() > 9.2)
+        {
+            songTimer.reset();
+            mySpeaker.PlaySong(note,duration);
+        }
         wait(.1);
     }
 }
--- a/tones.h	Tue Oct 27 10:06:16 2015 +0000
+++ b/tones.h	Tue Oct 27 13:30:31 2015 +0000
@@ -1,3 +1,4 @@
+#define rest 0.0
 #define NOTE_B0  31.0
 #define NOTE_C1  33.0
 #define NOTE_CS1 35.0
@@ -87,9 +88,10 @@
 #define NOTE_CS8 4435.0
 #define NOTE_D8  4699.0
 #define NOTE_DS8 4978.0
+#define notet 0.1
  
 //Mario main theme melody
-float note[] = {
+/*float note[] = {
   NOTE_E7, NOTE_E7, 0.0, NOTE_E7,
   0.0, NOTE_C7, NOTE_E7, 0.0,
   NOTE_G7, 0.0, 0.0,  0.0,
@@ -144,4 +146,20 @@
   0.12,0.12,0.12,0.12,
   0.12,0.12,0.12,0.12,
   0.12,0.12,0.12,0.12,
+};
+
+*/
+
+float note[] =
+{
+    NOTE_E7, rest, NOTE_E7, rest, NOTE_E7, rest, NOTE_C7, rest, NOTE_E7, rest, NOTE_G7, rest,
+    NOTE_C7, rest, NOTE_G6, rest, NOTE_E6, rest, NOTE_A7, rest, NOTE_B7, rest, NOTE_A7, rest, NOTE_A7,
+    NOTE_G6, rest, NOTE_E7, rest, NOTE_G7, rest, NOTE_A7, rest, NOTE_F7, rest, NOTE_G7, rest, NOTE_E7, rest, NOTE_C7, rest, NOTE_D7, rest, NOTE_B6
+};
+
+float duration[] = 
+{
+    notet, notet, notet, notet, notet, notet, notet, notet, notet, notet, notet, notet,
+    notet, notet, notet, notet, notet, notet, notet, notet, notet, notet, notet, notet, notet,
+    notet, notet, notet, notet, notet, notet, notet, notet, notet, notet, notet, notet, notet, notet, notet, notet, notet, notet, notet
 };
\ No newline at end of file