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.
Fork of Melody by
Revision 0:f1e3ef53cc3c, committed 2016-03-01
- Comitter:
- eencae
- Date:
- Tue Mar 01 15:58:55 2016 +0000
- Child:
- 1:7c149b76c057
- Commit message:
- Changed default melody.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Tue Mar 01 15:58:55 2016 +0000
@@ -0,0 +1,201 @@
+/* Melody
+
+Play a simple melody on the DAC using a look-up table for the sine waveform
+Includes a primitive graphic equalizer on the LEDs
+
+Craig A. Evans
+
+March 2014
+
+*/
+
+#include "mbed.h"
+#include "tone.h" // note definitions
+
+#define PI 3.14159265359
+
+Timer noteTimer; // timer for note duration
+AnalogOut aout(p18); // DAC on pin 18
+BusOut leds(LED4,LED3,LED2,LED1); // LEDs for display
+
+void initArray(); // function to initialise sample array
+void tone(float frequency,float duration); // play a tone of set frequency for given duration
+void graphicEqualizer(float frequency); // displays pattern on LEDs depending on the frequency
+
+int n = 32; // number of samples
+float y[32]; // array to store samples
+float BPM = 73.0; // beats per minute
+
+// comment one of the blocks out below - need noteArray[] and noteDuration[]
+
+/*
+// secret film melody...
+float noteArray[] = {
+ NOTE_E4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_E4,
+ NOTE_E4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,
+ NOTE_E4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_E4,
+ NOTE_E4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,
+ NOTE_DS5,NOTE_D5,NOTE_B4,NOTE_A4,NOTE_B4,NOTE_E4,NOTE_G4,NOTE_DS5,
+ NOTE_D5,NOTE_G4,NOTE_B4,NOTE_B4,NOTE_FS5,NOTE_F5,NOTE_B4,NOTE_D5,
+ NOTE_AS5,NOTE_A5,NOTE_F5,NOTE_A5,NOTE_DS6,NOTE_D6
+};
+
+// durations of the notes
+// 1/8th, 1/16th, 1/4th etc.
+float noteDuration[] = {
+ 8,16,16,8,4,8,8,8,
+ 8,16,16,8,4,8,8,8,
+ 8,16,16,8,4,8,8,8,
+ 8,16,16,8,4,8,8,8,
+ 8,2,8,8,1,8,4,8,
+ 4,8,8,8,8,4,8,4,
+ 8,4,8,4,8,3
+};
+*/
+
+
+// secret computer game melody...
+float noteArray[] = {
+ NOTE_E7, NOTE_E7, 0, NOTE_E7,
+ 0, NOTE_C7, NOTE_E7, 0,
+ NOTE_G7, 0, 0, 0,
+ NOTE_G6, 0, 0, 0,
+
+ NOTE_C7, 0, 0, NOTE_G6,
+ 0, 0, NOTE_E6, 0,
+ 0, NOTE_A6, 0, NOTE_B6,
+ 0, NOTE_AS6, NOTE_A6, 0,
+
+ NOTE_G6, NOTE_E7, NOTE_G7,
+ NOTE_A7, 0, NOTE_F7, NOTE_G7,
+ 0, NOTE_E7, 0,NOTE_C7,
+ NOTE_D7, NOTE_B6, 0, 0,
+
+ NOTE_C7, 0, 0, NOTE_G6,
+ 0, 0, NOTE_E6, 0,
+ 0, NOTE_A6, 0, NOTE_B6,
+ 0, NOTE_AS6, NOTE_A6, 0,
+
+ NOTE_G6, NOTE_E7, NOTE_G7,
+ NOTE_A7, 0, NOTE_F7, NOTE_G7,
+ 0, NOTE_E7, 0,NOTE_C7,
+ NOTE_D7, NOTE_B6, 0, 0
+};
+
+// durations of the notes
+// 1/8th, 1/16th, 1/4th etc.
+float noteDuration[] = {
+ 12, 12, 12, 12,
+ 12, 12, 12, 12,
+ 12, 12, 12, 12,
+ 12, 12, 12, 12,
+
+ 12, 12, 12, 12,
+ 12, 12, 12, 12,
+ 12, 12, 12, 12,
+ 12, 12, 12, 12,
+
+ 9, 9, 9,
+ 12, 12, 12, 12,
+ 12, 12, 12, 12,
+ 12, 12, 12, 12,
+
+ 12, 12, 12, 12,
+ 12, 12, 12, 12,
+ 12, 12, 12, 12,
+ 12, 12, 12, 12,
+
+ 9, 9, 9,
+ 12, 12, 12, 12,
+ 12, 12, 12, 12,
+ 12, 12, 12, 12,
+
+};
+
+
+int main()
+{
+ initArray(); // fill array with sine samples
+
+ // calculate number of notes in array
+ //sizeof() returns number of BYTES in array,
+ // so divide by size of a float in BYTES to get number of elements
+ int notes = sizeof(noteArray)/sizeof(float);
+
+ while (1) {
+
+ // loop through notes
+ for(int i=0; i < notes; i++) {
+
+ // play note
+ tone(noteArray[i],60.0/(BPM*noteDuration[i]));
+ // leave a short pause between notes
+ wait(60.0/(BPM*noteDuration[i]));
+ }
+
+ wait(1.0); // wait a second before repeating
+
+ }
+
+}
+
+void initArray()
+{
+ // create LUT - loop through array and calculate sine wave samples
+ for (int i = 0; i < n ; i++) {
+ y[i] = 0.5 + 0.5*sin(i*2*PI/n);
+ }
+
+}
+
+void tone(float frequency,float duration)
+{
+ graphicEqualizer(frequency); // equalizer on LEDs
+
+ if (frequency > 0) { // a frequency of 0 indicates no note played so only play a note if frequency is not 0
+
+ float dt = 1.0/(frequency*n) - (1.34e-6 + 1e-6); // calculate time step - take into account DAC time and wait() offset
+
+ noteTimer.start(); // start timer
+
+ while(noteTimer.read() < duration) { // keep looping while timer less than duration
+
+ for (int i = 0; i < n ; i++) { // loop through samples and output analog waveform
+ aout = y[i];
+ wait(dt); // leave appropriate delay for frequency
+ }
+ }
+
+ noteTimer.stop(); // stop the timer
+ noteTimer.reset(); // reset to 0
+
+ } else { // if no note played, have a simple delay
+ wait(duration);
+ }
+
+ leds = 0; // turn off LEDs
+}
+
+// primitive graphic equalizer on the LEDs
+void graphicEqualizer(float frequency)
+{
+ // these numbers are fairly random and picked to give a
+ // decent effect when the melodies plays.
+ // Will have to change for different melodies
+ if (frequency < 123) {
+ leds = 0; // all off
+ }
+ else if (frequency < 440) {
+ leds = 8; // LED1
+ }
+ else if (frequency < 523) {
+ leds = 12; // LED1,LED2
+ }
+ else if (frequency < 1047) {
+ leds = 14; // LED1,LED2,LED3
+ }
+ else {
+ leds = 15; // LED1,LED2,LED3,LED4
+ }
+
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Mar 01 15:58:55 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/8e73be2a2ac1 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tone.h Tue Mar 01 15:58:55 2016 +0000 @@ -0,0 +1,90 @@ +// Note definitions from Arduino.cc +#define NOTE_B0 31 +#define NOTE_C1 33 +#define NOTE_CS1 35 +#define NOTE_D1 37 +#define NOTE_DS1 39 +#define NOTE_E1 41 +#define NOTE_F1 44 +#define NOTE_FS1 46 +#define NOTE_G1 49 +#define NOTE_GS1 52 +#define NOTE_A1 55 +#define NOTE_AS1 58 +#define NOTE_B1 62 +#define NOTE_C2 65 +#define NOTE_CS2 69 +#define NOTE_D2 73 +#define NOTE_DS2 78 +#define NOTE_E2 82 +#define NOTE_F2 87 +#define NOTE_FS2 93 +#define NOTE_G2 98 +#define NOTE_GS2 104 +#define NOTE_A2 110 +#define NOTE_AS2 117 +#define NOTE_B2 123 +#define NOTE_C3 131 +#define NOTE_CS3 139 +#define NOTE_D3 147 +#define NOTE_DS3 156 +#define NOTE_E3 165 +#define NOTE_F3 175 +#define NOTE_FS3 185 +#define NOTE_G3 196 +#define NOTE_GS3 208 +#define NOTE_A3 220 +#define NOTE_AS3 233 +#define NOTE_B3 247 +#define NOTE_C4 262 +#define NOTE_CS4 277 +#define NOTE_D4 294 +#define NOTE_DS4 311 +#define NOTE_E4 330 +#define NOTE_F4 349 +#define NOTE_FS4 370 +#define NOTE_G4 392 +#define NOTE_GS4 415 +#define NOTE_A4 440 +#define NOTE_AS4 466 +#define NOTE_B4 494 +#define NOTE_C5 523 +#define NOTE_CS5 554 +#define NOTE_D5 587 +#define NOTE_DS5 622 +#define NOTE_E5 659 +#define NOTE_F5 698 +#define NOTE_FS5 740 +#define NOTE_G5 784 +#define NOTE_GS5 831 +#define NOTE_A5 880 +#define NOTE_AS5 932 +#define NOTE_B5 988 +#define NOTE_C6 1047 +#define NOTE_CS6 1109 +#define NOTE_D6 1175 +#define NOTE_DS6 1245 +#define NOTE_E6 1319 +#define NOTE_F6 1397 +#define NOTE_FS6 1480 +#define NOTE_G6 1568 +#define NOTE_GS6 1661 +#define NOTE_A6 1760 +#define NOTE_AS6 1865 +#define NOTE_B6 1976 +#define NOTE_C7 2093 +#define NOTE_CS7 2217 +#define NOTE_D7 2349 +#define NOTE_DS7 2489 +#define NOTE_E7 2637 +#define NOTE_F7 2794 +#define NOTE_FS7 2960 +#define NOTE_G7 3136 +#define NOTE_GS7 3322 +#define NOTE_A7 3520 +#define NOTE_AS7 3729 +#define NOTE_B7 3951 +#define NOTE_C8 4186 +#define NOTE_CS8 4435 +#define NOTE_D8 4699 +#define NOTE_DS8 4978 \ No newline at end of file
