Leong Kum Loong (NYP) / tone_h

Dependents:   Tone_Example

Revision:
3:9ddabd24f48c
Parent:
2:7f2974b69b48
diff -r 7f2974b69b48 -r 9ddabd24f48c tone.cpp
--- a/tone.cpp	Sun Nov 19 10:17:34 2017 +0000
+++ b/tone.cpp	Mon Nov 20 03:53:48 2017 +0000
@@ -3,7 +3,7 @@
 /*
     tone.h
     Written by Leong Kum Loong.
-    19 Nov 2017, REV. 1
+    20 Nov 2017, REV. 2
     
     All routines and functions in this library are written by me solely with the purpose
     of implementing an identical "tone" library that was introduced by Arduino IDE.
@@ -16,15 +16,17 @@
 
 PwmOut speaker(p26);
 
-//Notes for starwars_example.
-int c = 261, d = 294, e = 329, f = 349, g = 391, gS = 415, a = 440, aS = 455, b = 466, cH = 523;
-int cSH = 554, dH = 587, dSH = 622, eH = 659, fH = 698, fSH = 740, gH = 784, gSH = 830, aH = 880;
-
 void tone(int frequency, int duration_ms){
     float period = (float)1/(float)frequency;
     speaker.period(period);
     speaker.pulsewidth(period/(float)(2));
-    wait_ms(duration_ms);
+    if(duration_ms){
+        wait_ms(duration_ms);
+        speaker = 0;
+    }
+}
+
+void noTone(){
     speaker = 0;
 }
 
@@ -389,6 +391,9 @@
  
   
 //Star wars theme section.
+int c = 261, d = 294, e = 329, f = 349, g = 391, gS = 415, a = 440, aS = 455, b = 466, cH = 523;
+int cSH = 554, dH = 587, dSH = 622, eH = 659, fH = 698, fSH = 740, gH = 784, gSH = 830, aH = 880;
+
 void firstSection(){
     tone(a, 500);
     tone(a, 500);