mbed Starter Kit / mbed Starter Kit Demo Programs
Committer:
ShawnHymel
Date:
Mon Jul 28 20:29:28 2014 +0000
Revision:
0:6a73d3dc037e
Added main.cpp files from each of the demo programs. Libraries are not included.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ShawnHymel 0:6a73d3dc037e 1 // Plays a familiar melody using PWM to the headphones. To find the frequencies
ShawnHymel 0:6a73d3dc037e 2 // of notes, see http://en.wikipedia.org/wiki/Piano_key_frequencies
ShawnHymel 0:6a73d3dc037e 3 // Based on the "speaker_demo_PWM" program by Jim Hamblen
ShawnHymel 0:6a73d3dc037e 4
ShawnHymel 0:6a73d3dc037e 5 #include "mbed.h"
ShawnHymel 0:6a73d3dc037e 6
ShawnHymel 0:6a73d3dc037e 7 #define VOLUME 0.01
ShawnHymel 0:6a73d3dc037e 8 #define BPM 100.0
ShawnHymel 0:6a73d3dc037e 9
ShawnHymel 0:6a73d3dc037e 10 PwmOut pwm_pin(p21);
ShawnHymel 0:6a73d3dc037e 11
ShawnHymel 0:6a73d3dc037e 12 // Plays a sound with the defined frequency, duration, and volume
ShawnHymel 0:6a73d3dc037e 13 void playNote(float frequency, float duration, float volume) {
ShawnHymel 0:6a73d3dc037e 14 pwm_pin.period(1.0/frequency);
ShawnHymel 0:6a73d3dc037e 15 pwm_pin = volume/2.0;
ShawnHymel 0:6a73d3dc037e 16 wait(duration);
ShawnHymel 0:6a73d3dc037e 17 pwm_pin = 0.0;
ShawnHymel 0:6a73d3dc037e 18 }
ShawnHymel 0:6a73d3dc037e 19
ShawnHymel 0:6a73d3dc037e 20 int main()
ShawnHymel 0:6a73d3dc037e 21 {
ShawnHymel 0:6a73d3dc037e 22 float beat_duration;
ShawnHymel 0:6a73d3dc037e 23
ShawnHymel 0:6a73d3dc037e 24 // Calculate duration of a quarter note from bpm
ShawnHymel 0:6a73d3dc037e 25 beat_duration = 60.0 / BPM;
ShawnHymel 0:6a73d3dc037e 26
ShawnHymel 0:6a73d3dc037e 27 // Loop forever
ShawnHymel 0:6a73d3dc037e 28 while(1) {
ShawnHymel 0:6a73d3dc037e 29
ShawnHymel 0:6a73d3dc037e 30 // First measure
ShawnHymel 0:6a73d3dc037e 31 playNote(391.995, (beat_duration - 0.1), VOLUME);
ShawnHymel 0:6a73d3dc037e 32 wait(0.1);
ShawnHymel 0:6a73d3dc037e 33 playNote(391.995, (beat_duration - 0.1), VOLUME);
ShawnHymel 0:6a73d3dc037e 34 wait(0.1);
ShawnHymel 0:6a73d3dc037e 35 playNote(391.995, (beat_duration - 0.1), VOLUME);
ShawnHymel 0:6a73d3dc037e 36 wait(0.1);
ShawnHymel 0:6a73d3dc037e 37 playNote(311.127, (0.75 * beat_duration), VOLUME);
ShawnHymel 0:6a73d3dc037e 38 playNote(466.164, (0.25 * beat_duration), VOLUME);
ShawnHymel 0:6a73d3dc037e 39
ShawnHymel 0:6a73d3dc037e 40 // Second measure
ShawnHymel 0:6a73d3dc037e 41 playNote(391.995, (beat_duration - 0.1), VOLUME);
ShawnHymel 0:6a73d3dc037e 42 wait(0.1);
ShawnHymel 0:6a73d3dc037e 43 playNote(311.127, (0.75 * beat_duration), VOLUME);
ShawnHymel 0:6a73d3dc037e 44 playNote(466.164, (0.25 * beat_duration), VOLUME);
ShawnHymel 0:6a73d3dc037e 45 playNote(391.995, ((2 * beat_duration) - 0.1), VOLUME);
ShawnHymel 0:6a73d3dc037e 46 wait(0.1);
ShawnHymel 0:6a73d3dc037e 47
ShawnHymel 0:6a73d3dc037e 48 // Third measure
ShawnHymel 0:6a73d3dc037e 49 playNote(587.330, (beat_duration - 0.1), VOLUME);
ShawnHymel 0:6a73d3dc037e 50 wait(0.1);
ShawnHymel 0:6a73d3dc037e 51 playNote(587.330, (beat_duration - 0.1), VOLUME);
ShawnHymel 0:6a73d3dc037e 52 wait(0.1);
ShawnHymel 0:6a73d3dc037e 53 playNote(587.330, (beat_duration - 0.1), VOLUME);
ShawnHymel 0:6a73d3dc037e 54 wait(0.1);
ShawnHymel 0:6a73d3dc037e 55 playNote(622.254, (0.75 * beat_duration), VOLUME);
ShawnHymel 0:6a73d3dc037e 56 playNote(466.164, (0.25 * beat_duration), VOLUME);
ShawnHymel 0:6a73d3dc037e 57
ShawnHymel 0:6a73d3dc037e 58 // Fourth measure
ShawnHymel 0:6a73d3dc037e 59 playNote(369.994, (beat_duration - 0.1), VOLUME);
ShawnHymel 0:6a73d3dc037e 60 wait(0.1);
ShawnHymel 0:6a73d3dc037e 61 playNote(311.127, (0.75 * beat_duration), VOLUME);
ShawnHymel 0:6a73d3dc037e 62 playNote(466.164, (0.25 * beat_duration), VOLUME);
ShawnHymel 0:6a73d3dc037e 63 playNote(391.995, ((2 * beat_duration) - 0.1), VOLUME);
ShawnHymel 0:6a73d3dc037e 64 wait(0.1);
ShawnHymel 0:6a73d3dc037e 65 }
ShawnHymel 0:6a73d3dc037e 66 }
ShawnHymel 0:6a73d3dc037e 67