Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: tone.cpp
- Revision:
- 4:d76458b00616
- Parent:
- 3:9ddabd24f48c
--- a/tone.cpp Mon Nov 20 03:53:48 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,471 +0,0 @@ -#include "mbed.h" -#include "tone.h" -/* - tone.h - Written by Leong Kum Loong. - 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. - - Example reference from Github - =============================== - The mario_example routine "notes" are reference to the work of Gregory Kielian. - The starwars_example routine "notes" are reference to the work of nicksort. -*/ - -PwmOut speaker(p26); - -void tone(int frequency, int duration_ms){ - float period = (float)1/(float)frequency; - speaker.period(period); - speaker.pulsewidth(period/(float)(2)); - if(duration_ms){ - wait_ms(duration_ms); - speaker = 0; - } -} - -void noTone(){ - speaker = 0; -} - -//Mario theme section. -static void delay(int millis){ - wait_ms(millis * 0.7); //If play speed sounds weird, try modify delay here. -} - -void mario_example(){ - tone(660,100); - delay(150); - tone(660,100); - delay(300); - tone(660,100); - delay(300); - tone(510,100); - delay(100); - tone(660,100); - delay(300); - tone(770,100); - delay(550); - tone(380,100); - delay(575); - - tone(510,100); - delay(450); - tone(380,100); - delay(400); - tone(320,100); - delay(500); - tone(440,100); - delay(300); - tone(480,80); - delay(330); - tone(450,100); - delay(150); - tone(430,100); - delay(300); - tone(380,100); - delay(200); - tone(660,80); - delay(200); - tone(760,50); - delay(150); - tone(860,100); - delay(300); - tone(700,80); - delay(150); - tone(760,50); - delay(350); - tone(660,80); - delay(300); - tone(520,80); - delay(150); - tone(580,80); - delay(150); - tone(480,80); - delay(500); - - tone(510,100); - delay(450); - tone(380,100); - delay(400); - tone(320,100); - delay(500); - tone(440,100); - delay(300); - tone(480,80); - delay(330); - tone(450,100); - delay(150); - tone(430,100); - delay(300); - tone(380,100); - delay(200); - tone(660,80); - delay(200); - tone(760,50); - delay(150); - tone(860,100); - delay(300); - tone(700,80); - delay(150); - tone(760,50); - delay(350); - tone(660,80); - delay(300); - tone(520,80); - delay(150); - tone(580,80); - delay(150); - tone(480,80); - delay(500); - - tone(500,100); - delay(300); - - tone(760,100); - delay(100); - tone(720,100); - delay(150); - tone(680,100); - delay(150); - tone(620,150); - delay(300); - - tone(650,150); - delay(300); - tone(380,100); - delay(150); - tone(430,100); - delay(150); - - tone(500,100); - delay(300); - tone(430,100); - delay(150); - tone(500,100); - delay(100); - tone(570,100); - delay(220); - - tone(500,100); - delay(300); - - tone(760,100); - delay(100); - tone(720,100); - delay(150); - tone(680,100); - delay(150); - tone(620,150); - delay(300); - - tone(650,200); - delay(300); - - tone(1020,80); - delay(300); - tone(1020,80); - delay(150); - tone(1020,80); - delay(300); - - tone(380,100); - delay(300); - tone(500,100); - delay(300); - - tone(760,100); - delay(100); - tone(720,100); - delay(150); - tone(680,100); - delay(150); - tone(620,150); - delay(300); - - tone(650,150); - delay(300); - tone(380,100); - delay(150); - tone(430,100); - delay(150); - - tone(500,100); - delay(300); - tone(430,100); - delay(150); - tone(500,100); - delay(100); - tone(570,100); - delay(420); - - tone(585,100); - delay(450); - - tone(550,100); - delay(420); - - tone(500,100); - delay(360); - - tone(380,100); - delay(300); - tone(500,100); - delay(300); - tone(500,100); - delay(150); - tone(500,100); - delay(300); - - tone(500,100); - delay(300); - - tone(760,100); - delay(100); - tone(720,100); - delay(150); - tone(680,100); - delay(150); - tone(620,150); - delay(300); - - tone(650,150); - delay(300); - tone(380,100); - delay(150); - tone(430,100); - delay(150); - - tone(500,100); - delay(300); - tone(430,100); - delay(150); - tone(500,100); - delay(100); - tone(570,100); - delay(220); - - tone(500,100); - delay(300); - - tone(760,100); - delay(100); - tone(720,100); - delay(150); - tone(680,100); - delay(150); - tone(620,150); - delay(300); - - tone(650,200); - delay(300); - - tone(1020,80); - delay(300); - tone(1020,80); - delay(150); - tone(1020,80); - delay(300); - - tone(380,100); - delay(300); - tone(500,100); - delay(300); - - tone(760,100); - delay(100); - tone(720,100); - delay(150); - tone(680,100); - delay(150); - tone(620,150); - delay(300); - - tone(650,150); - delay(300); - tone(380,100); - delay(150); - tone(430,100); - delay(150); - - tone(500,100); - delay(300); - tone(430,100); - delay(150); - tone(500,100); - delay(100); - tone(570,100); - delay(420); - - tone(585,100); - delay(450); - - tone(550,100); - delay(420); - - tone(500,100); - delay(360); - - tone(380,100); - delay(300); - tone(500,100); - delay(300); - tone(500,100); - delay(150); - tone(500,100); - delay(300); - - tone(500,60); - delay(150); - tone(500,80); - delay(300); - tone(500,60); - delay(350); - tone(500,80); - delay(150); - tone(580,80); - delay(350); - tone(660,80); - delay(150); - tone(500,80); - delay(300); - tone(430,80); - delay(150); - tone(380,80); - delay(600); - - tone(500,60); - delay(150); - tone(500,80); - delay(300); - tone(500,60); - delay(350); - tone(500,80); - delay(150); - tone(580,80); - delay(150); - tone(660,80); - delay(550); - - tone(870,80); - delay(325); - tone(760,80); - delay(600); - - tone(500,60); - delay(150); - tone(500,80); - delay(300); - tone(500,60); - delay(350); - tone(500,80); - delay(150); - tone(580,80); - delay(350); - tone(660,80); - delay(150); - tone(500,80); - delay(300); - tone(430,80); - delay(150); - tone(380,80); - delay(600); - - tone(660,100); - delay(150); - tone(660,100); - delay(300); - tone(660,100); - delay(300); - tone(510,100); - delay(100); - tone(660,100); - delay(300); - tone(770,100); - delay(550); - tone(380,100); - delay(575); -} - - -//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); - tone(a, 500); - tone(f, 350); - tone(cH, 150); - tone(a, 500); - tone(f, 350); - tone(cH, 150); - tone(a, 650); - wait_ms(500); - - tone(eH, 500); - tone(eH, 500); - tone(eH, 500); - tone(fH, 350); - tone(cH, 150); - tone(gS, 500); - tone(f, 350); - tone(cH, 150); - tone(a, 650); - wait_ms(500); -} - -void secondSection(){ - tone(aH, 500); - tone(a, 300); - tone(a, 150); - tone(aH, 500); - tone(gSH, 325); - tone(gH, 175); - tone(fSH, 125); - tone(fH, 125); - tone(fSH, 250); - wait_ms(325); - - tone(aS, 250); - tone(dSH, 500); - tone(dH, 325); - tone(cSH, 175); - tone(cH, 125); - tone(b, 125); - tone(cH, 250); - wait_ms(350); -} - -void starwars_example(){ - firstSection(); - - secondSection(); - - //Variant 1 - tone(f, 250); - tone(gS, 500); - tone(f, 350); - tone(a, 125); - tone(cH, 500); - tone(a, 375); - tone(cH, 125); - tone(eH, 650); - wait_ms(500); - - secondSection(); - - //Variant 2 - tone(f, 250); - tone(gS, 500); - tone(f, 375); - tone(cH, 125); - tone(a, 500); - tone(f, 375); - tone(cH, 125); - tone(a, 650); - wait_ms(650); -} \ No newline at end of file