Initial Commit
Dependencies: mbed HC05 QEI MODSERIAL SWSPI mbed-rtos
Drivers/Tone/tone.cpp@1:1da89c13dfa1, 2014-10-11 (annotated)
- Committer:
- Throwbot
- Date:
- Sat Oct 11 03:53:27 2014 +0000
- Revision:
- 1:1da89c13dfa1
- Child:
- 4:81b0de07841f
Ultrasonic seperate class
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Throwbot | 1:1da89c13dfa1 | 1 | #include "tone.h" |
Throwbot | 1:1da89c13dfa1 | 2 | |
Throwbot | 1:1da89c13dfa1 | 3 | DigitalOut speakerOut(buzz); // Set up speaker on digital pin 7 |
Throwbot | 1:1da89c13dfa1 | 4 | |
Throwbot | 1:1da89c13dfa1 | 5 | // MELODIES and TIMING // |
Throwbot | 1:1da89c13dfa1 | 6 | // melody[] is an array of notes, accompanied by beats[], |
Throwbot | 1:1da89c13dfa1 | 7 | // which sets each note's relative length (higher #, longer note) |
Throwbot | 1:1da89c13dfa1 | 8 | |
Throwbot | 1:1da89c13dfa1 | 9 | // Melody 1: Star Wars Imperial March |
Throwbot | 1:1da89c13dfa1 | 10 | 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}; |
Throwbot | 1:1da89c13dfa1 | 11 | 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}; |
Throwbot | 1:1da89c13dfa1 | 12 | |
Throwbot | 1:1da89c13dfa1 | 13 | // Melody 2: Star Wars Theme |
Throwbot | 1:1da89c13dfa1 | 14 | int melody2[] = { f4, f4, f4, a4s, f5, d5s, d5, c5, a5s, f5, d5s, d5, c5, a5s, f5, d5s, d5, d5s, c5}; |
Throwbot | 1:1da89c13dfa1 | 15 | int beats2[] = { 21, 21, 21, 128, 128, 21, 21, 21, 128, 64, 21, 21, 21, 128, 64, 21, 21, 21, 128 }; |
Throwbot | 1:1da89c13dfa1 | 16 | |
Throwbot | 1:1da89c13dfa1 | 17 | int MAX_COUNT = sizeof(melody1) / 2; // Melody length, for looping. |
Throwbot | 1:1da89c13dfa1 | 18 | long tempo = 10000; // Set overall tempo |
Throwbot | 1:1da89c13dfa1 | 19 | int pause = 1000; // Set length of pause between notes |
Throwbot | 1:1da89c13dfa1 | 20 | int rest_count = 1; // Loop variable to increase Rest length (BLETCHEROUS HACK; See NOTES) |
Throwbot | 1:1da89c13dfa1 | 21 | // Initialize core variables |
Throwbot | 1:1da89c13dfa1 | 22 | int toneM = 0; |
Throwbot | 1:1da89c13dfa1 | 23 | int beat = 0; |
Throwbot | 1:1da89c13dfa1 | 24 | long duration = 0; |
Throwbot | 1:1da89c13dfa1 | 25 | // PLAY TONE // |
Throwbot | 1:1da89c13dfa1 | 26 | // Pulse the speaker to play a tone for a particular duration |
Throwbot | 1:1da89c13dfa1 | 27 | void imperial_march() |
Throwbot | 1:1da89c13dfa1 | 28 | { |
Throwbot | 1:1da89c13dfa1 | 29 | for (int i=0; i<MAX_COUNT; i++) { |
Throwbot | 1:1da89c13dfa1 | 30 | toneM = melody1[i]; |
Throwbot | 1:1da89c13dfa1 | 31 | beat = beats1[i]; |
Throwbot | 1:1da89c13dfa1 | 32 | duration = beat * tempo; // Set up timing |
Throwbot | 1:1da89c13dfa1 | 33 | long elapsed_time = 0; |
Throwbot | 1:1da89c13dfa1 | 34 | if (toneM > 0) { // if this isn't a Rest beat, while the tone has |
Throwbot | 1:1da89c13dfa1 | 35 | // played less long than 'duration', pulse speaker HIGH and LOW |
Throwbot | 1:1da89c13dfa1 | 36 | while (elapsed_time < duration) { |
Throwbot | 1:1da89c13dfa1 | 37 | speakerOut=1; |
Throwbot | 1:1da89c13dfa1 | 38 | wait_us(toneM / 2); |
Throwbot | 1:1da89c13dfa1 | 39 | speakerOut=0; |
Throwbot | 1:1da89c13dfa1 | 40 | wait_us(toneM / 2); |
Throwbot | 1:1da89c13dfa1 | 41 | elapsed_time += (toneM); |
Throwbot | 1:1da89c13dfa1 | 42 | } |
Throwbot | 1:1da89c13dfa1 | 43 | } else { // Rest beat; loop times delay |
Throwbot | 1:1da89c13dfa1 | 44 | for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count |
Throwbot | 1:1da89c13dfa1 | 45 | wait_us(duration); |
Throwbot | 1:1da89c13dfa1 | 46 | } |
Throwbot | 1:1da89c13dfa1 | 47 | } |
Throwbot | 1:1da89c13dfa1 | 48 | } |
Throwbot | 1:1da89c13dfa1 | 49 | } |
Throwbot | 1:1da89c13dfa1 | 50 |