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.
Dependencies: mbed HC05 QEI MODSERIAL SWSPI mbed-rtos
tone.cpp
00001 #include "tone.h" 00002 00003 DigitalOut speakerOut(buzz); // Set up speaker on digital pin 7 00004 00005 // MELODIES and TIMING // 00006 // melody[] is an array of notes, accompanied by beats[], 00007 // which sets each note's relative length (higher #, longer note) 00008 00009 // Melody 1: Star Wars Imperial March 00010 int melody1[] = { a4, R, a4, R, a4, R, f4, R, c5, R, a4, R, f4, R, c5, R, a4, R, e5, R, e5, R, e5, R, f5, R, c5, R, g5, R, f5, R, c5, R, a4, R}; 00011 int beats1[] = { 50, 20, 50, 20, 50, 20, 40, 5, 20, 5, 60, 10, 40, 5, 20, 5, 60, 80, 50, 20, 50, 20, 50, 20, 40, 5, 20, 5, 60, 10, 40, 5, 20, 5, 60, 40}; 00012 00013 // Melody 2: Star Wars Theme 00014 int melody2[] = { f4, f4, f4, a4s, f5, d5s, d5, c5, a5s, f5, d5s, d5, c5, a5s, f5, d5s, d5, d5s, c5}; 00015 int beats2[] = { 21, 21, 21, 128, 128, 21, 21, 21, 128, 64, 21, 21, 21, 128, 64, 21, 21, 21, 128 }; 00016 00017 int MAX_COUNT = sizeof(melody1) / 2; // Melody length, for looping. 00018 long tempo = 10000; // Set overall tempo 00019 int pause = 1000; // Set length of pause between notes 00020 int rest_count = 1; // Loop variable to increase Rest length (BLETCHEROUS HACK; See NOTES) 00021 // Initialize core variables 00022 int toneM = 0; 00023 int beat = 0; 00024 long duration = 0; 00025 // PLAY TONE // 00026 // Pulse the speaker to play a tone for a particular duration 00027 void imperial_march() 00028 { 00029 for (int i=0; i<36; i++) { 00030 toneM = melody1[i]; 00031 beat = beats1[i]; 00032 duration = beat * tempo; // Set up timing 00033 long elapsed_time = 0; 00034 if (toneM > 0) { // if this isn't a Rest beat, while the tone has 00035 // played less long than 'duration', pulse speaker HIGH and LOW 00036 while (elapsed_time < duration) { 00037 speakerOut=1; 00038 wait_us(toneM / 2); 00039 speakerOut=0; 00040 wait_us(toneM / 2); 00041 elapsed_time += (toneM); 00042 } 00043 } else { // Rest beat; loop times delay 00044 for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count 00045 wait_us(duration); 00046 } 00047 } 00048 } 00049 } 00050
Generated on Wed Jul 13 2022 22:51:50 by
1.7.2