The purpose is to teach users to play a song by following the LEDs and play the corresponding pushbottons.
main.cpp@0:9fc296756703, 2013-10-17 (annotated)
- Committer:
- hpham33
- Date:
- Thu Oct 17 14:20:18 2013 +0000
- Revision:
- 0:9fc296756703
The purpose is to teach users to play a song by following the LEDs and play the corresponding pushbottons.
;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hpham33 | 0:9fc296756703 | 1 | #include "mbed.h" |
hpham33 | 0:9fc296756703 | 2 | |
hpham33 | 0:9fc296756703 | 3 | DigitalIn pb1(p8); |
hpham33 | 0:9fc296756703 | 4 | DigitalIn pb2(p9); |
hpham33 | 0:9fc296756703 | 5 | DigitalIn pb3(p10); |
hpham33 | 0:9fc296756703 | 6 | DigitalIn pb4(p12); |
hpham33 | 0:9fc296756703 | 7 | DigitalIn pb5(p13); |
hpham33 | 0:9fc296756703 | 8 | DigitalIn pb6(p17); |
hpham33 | 0:9fc296756703 | 9 | DigitalIn pb7(p20); |
hpham33 | 0:9fc296756703 | 10 | //PwmOut speaker(p21); |
hpham33 | 0:9fc296756703 | 11 | DigitalOut led1(p30); |
hpham33 | 0:9fc296756703 | 12 | DigitalOut led2(p29); |
hpham33 | 0:9fc296756703 | 13 | DigitalOut led3(p28); |
hpham33 | 0:9fc296756703 | 14 | DigitalOut led4(p26); |
hpham33 | 0:9fc296756703 | 15 | DigitalOut led5(p25); |
hpham33 | 0:9fc296756703 | 16 | DigitalOut led6(p5); |
hpham33 | 0:9fc296756703 | 17 | DigitalOut led7(p6); |
hpham33 | 0:9fc296756703 | 18 | class Speaker |
hpham33 | 0:9fc296756703 | 19 | { |
hpham33 | 0:9fc296756703 | 20 | public: |
hpham33 | 0:9fc296756703 | 21 | Speaker(PinName pin) : _pin(pin) { |
hpham33 | 0:9fc296756703 | 22 | // _pin(pin) means pass pin to the Speaker Constructor |
hpham33 | 0:9fc296756703 | 23 | } |
hpham33 | 0:9fc296756703 | 24 | // class method to play a note based on PwmOut class |
hpham33 | 0:9fc296756703 | 25 | void PlayNote(float frequency, float duration, float volume) { |
hpham33 | 0:9fc296756703 | 26 | _pin.period(1.0/frequency); |
hpham33 | 0:9fc296756703 | 27 | _pin = volume/2.0; |
hpham33 | 0:9fc296756703 | 28 | wait(duration); |
hpham33 | 0:9fc296756703 | 29 | _pin = 0.0; |
hpham33 | 0:9fc296756703 | 30 | } |
hpham33 | 0:9fc296756703 | 31 | |
hpham33 | 0:9fc296756703 | 32 | private: |
hpham33 | 0:9fc296756703 | 33 | // sets up specified pin for PWM using PwmOut class |
hpham33 | 0:9fc296756703 | 34 | PwmOut _pin; |
hpham33 | 0:9fc296756703 | 35 | }; |
hpham33 | 0:9fc296756703 | 36 | // no external PullUp resistor needed |
hpham33 | 0:9fc296756703 | 37 | // Pushbutton from P8 to GND. |
hpham33 | 0:9fc296756703 | 38 | int main() { |
hpham33 | 0:9fc296756703 | 39 | Speaker mySpeaker(p21); |
hpham33 | 0:9fc296756703 | 40 | pb1.mode(PullUp); |
hpham33 | 0:9fc296756703 | 41 | pb2.mode(PullUp); |
hpham33 | 0:9fc296756703 | 42 | pb3.mode(PullUp); |
hpham33 | 0:9fc296756703 | 43 | pb4.mode(PullUp); |
hpham33 | 0:9fc296756703 | 44 | pb5.mode(PullUp); |
hpham33 | 0:9fc296756703 | 45 | pb6.mode(PullUp); |
hpham33 | 0:9fc296756703 | 46 | pb7.mode(PullUp); |
hpham33 | 0:9fc296756703 | 47 | while(1){ |
hpham33 | 0:9fc296756703 | 48 | led1=1; |
hpham33 | 0:9fc296756703 | 49 | while(led1==1){ |
hpham33 | 0:9fc296756703 | 50 | if(pb1!=1) { |
hpham33 | 0:9fc296756703 | 51 | mySpeaker.PlayNote(261.6, 0.5, 0.01); |
hpham33 | 0:9fc296756703 | 52 | led1=0; |
hpham33 | 0:9fc296756703 | 53 | wait(0.5); |
hpham33 | 0:9fc296756703 | 54 | } |
hpham33 | 0:9fc296756703 | 55 | } |
hpham33 | 0:9fc296756703 | 56 | led2 = 1; |
hpham33 | 0:9fc296756703 | 57 | while(led2==1){ |
hpham33 | 0:9fc296756703 | 58 | if(pb2 !=1){ |
hpham33 | 0:9fc296756703 | 59 | mySpeaker.PlayNote(293.7, 0.5, 0.01); |
hpham33 | 0:9fc296756703 | 60 | led2 = 0; |
hpham33 | 0:9fc296756703 | 61 | wait(0.5); |
hpham33 | 0:9fc296756703 | 62 | } |
hpham33 | 0:9fc296756703 | 63 | } |
hpham33 | 0:9fc296756703 | 64 | |
hpham33 | 0:9fc296756703 | 65 | led3=1; |
hpham33 | 0:9fc296756703 | 66 | while(led3==1){ |
hpham33 | 0:9fc296756703 | 67 | if(pb3 !=1){ |
hpham33 | 0:9fc296756703 | 68 | mySpeaker.PlayNote(329.6, 0.5, 0.01); |
hpham33 | 0:9fc296756703 | 69 | led3 = 0; |
hpham33 | 0:9fc296756703 | 70 | wait(0.5); |
hpham33 | 0:9fc296756703 | 71 | } |
hpham33 | 0:9fc296756703 | 72 | } |
hpham33 | 0:9fc296756703 | 73 | led4=1; |
hpham33 | 0:9fc296756703 | 74 | while(led4==1){ |
hpham33 | 0:9fc296756703 | 75 | if(pb4 !=1){ |
hpham33 | 0:9fc296756703 | 76 | mySpeaker.PlayNote(349.2, 0.5, 0.01); |
hpham33 | 0:9fc296756703 | 77 | led4 = 0; |
hpham33 | 0:9fc296756703 | 78 | wait(0.5); |
hpham33 | 0:9fc296756703 | 79 | } |
hpham33 | 0:9fc296756703 | 80 | } |
hpham33 | 0:9fc296756703 | 81 | led5=1; |
hpham33 | 0:9fc296756703 | 82 | while(led5==1){ |
hpham33 | 0:9fc296756703 | 83 | if(pb5 !=1){ |
hpham33 | 0:9fc296756703 | 84 | mySpeaker.PlayNote(391.9, 0.5, 0.01); |
hpham33 | 0:9fc296756703 | 85 | led5 = 0; |
hpham33 | 0:9fc296756703 | 86 | wait(0.5); |
hpham33 | 0:9fc296756703 | 87 | } |
hpham33 | 0:9fc296756703 | 88 | } |
hpham33 | 0:9fc296756703 | 89 | led6=1; |
hpham33 | 0:9fc296756703 | 90 | while(led6==1){ |
hpham33 | 0:9fc296756703 | 91 | if(pb6 !=1){ |
hpham33 | 0:9fc296756703 | 92 | mySpeaker.PlayNote(440.0, 0.5, 0.01); |
hpham33 | 0:9fc296756703 | 93 | led6 = 0; |
hpham33 | 0:9fc296756703 | 94 | wait(0.5); |
hpham33 | 0:9fc296756703 | 95 | } |
hpham33 | 0:9fc296756703 | 96 | } |
hpham33 | 0:9fc296756703 | 97 | led7=1; |
hpham33 | 0:9fc296756703 | 98 | while(led7==1){ |
hpham33 | 0:9fc296756703 | 99 | if(pb7 !=1){ |
hpham33 | 0:9fc296756703 | 100 | mySpeaker.PlayNote(493.9, 0.5, 0.01); |
hpham33 | 0:9fc296756703 | 101 | led7 = 0; |
hpham33 | 0:9fc296756703 | 102 | wait(0.5); |
hpham33 | 0:9fc296756703 | 103 | } |
hpham33 | 0:9fc296756703 | 104 | } |
hpham33 | 0:9fc296756703 | 105 | } |
hpham33 | 0:9fc296756703 | 106 | } |