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:
- 1:3b00679c208a
- Parent:
- 0:2b4c66b3f6aa
- Child:
- 2:7f2974b69b48
--- a/tone.cpp Sun Nov 19 06:08:29 2017 +0000 +++ b/tone.cpp Sun Nov 19 10:03:55 2017 +0000 @@ -3,17 +3,23 @@ /* tone.h Written by Leong Kum Loong. - 19 Nov 2017, REV. 0 + 19 Nov 2017, REV. 1 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. - The mario_example routine "notes" are reference to the work of Gregory Kielian - an Electrical Engineer at Google ATAP Skunkworks via Avalon - from his Mario.ino sketch for Arduino. + + 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); +//Note 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); @@ -26,356 +32,434 @@ 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); +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(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(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(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(650,200); + delay(300); - tone(500,100); - delay(300); - tone(430,100); - delay(150); - tone(500,100); - delay(100); - tone(570,100); - delay(220); + tone(1020,80); + delay(300); + tone(1020,80); + delay(150); + tone(1020,80); + delay(300); - tone(500,100); - 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(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(650,150); + delay(300); + tone(380,100); + delay(150); + tone(430,100); + delay(150); - tone(1020,80); - delay(300); - tone(1020,80); - delay(150); - tone(1020,80); - delay(300); + 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(380,100); - delay(300); - tone(500,100); - delay(300); + tone(550,100); + delay(420); + + tone(500,100); + delay(360); - tone(760,100); - delay(100); - tone(720,100); - delay(150); - tone(680,100); - delay(150); - tone(620,150); - delay(300); + tone(380,100); + delay(300); + tone(500,100); + delay(300); + tone(500,100); + delay(150); + tone(500,100); + delay(300); - tone(650,150); - delay(300); - tone(380,100); - delay(150); - tone(430,100); - delay(150); + 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(500,100); - delay(300); - tone(430,100); - delay(150); - tone(500,100); - delay(100); - tone(570,100); - delay(420); + tone(650,150); + delay(300); + tone(380,100); + delay(150); + tone(430,100); + delay(150); - tone(585,100); - delay(450); + tone(500,100); + delay(300); + tone(430,100); + delay(150); + tone(500,100); + delay(100); + tone(570,100); + delay(220); - tone(550,100); - delay(420); - - tone(500,100); - delay(360); + tone(500,100); + delay(300); - tone(380,100); - delay(300); - tone(500,100); - delay(300); - tone(500,100); - delay(150); - 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(500,100); - 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(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(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(430,100); + delay(150); + tone(500,100); + delay(100); + tone(570,100); + delay(420); - tone(500,100); - delay(300); + tone(585,100); + delay(450); - tone(760,100); - delay(100); - tone(720,100); - delay(150); - tone(680,100); - delay(150); - tone(620,150); - delay(300); + tone(550,100); + delay(420); - tone(650,200); - delay(300); + tone(500,100); + delay(360); - 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(380,100); + delay(300); + tone(500,100); + delay(300); + tone(500,100); + delay(150); + 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(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(650,150); - delay(300); - tone(380,100); - delay(150); - tone(430,100); - delay(150); + 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(500,100); - delay(300); - tone(430,100); - delay(150); - tone(500,100); - delay(100); - tone(570,100); - delay(420); + tone(870,80); + delay(325); + tone(760,80); + delay(600); - 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(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(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. +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); +} - 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); -} +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