The purpose is to teach users to play a song by following the LEDs and play the corresponding pushbottons.
Diff: main.cpp
- Revision:
- 0:9fc296756703
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Oct 17 14:20:18 2013 +0000 @@ -0,0 +1,106 @@ +#include "mbed.h" + +DigitalIn pb1(p8); +DigitalIn pb2(p9); +DigitalIn pb3(p10); +DigitalIn pb4(p12); +DigitalIn pb5(p13); +DigitalIn pb6(p17); +DigitalIn pb7(p20); +//PwmOut speaker(p21); +DigitalOut led1(p30); +DigitalOut led2(p29); +DigitalOut led3(p28); +DigitalOut led4(p26); +DigitalOut led5(p25); +DigitalOut led6(p5); +DigitalOut led7(p6); +class Speaker +{ +public: + Speaker(PinName pin) : _pin(pin) { +// _pin(pin) means pass pin to the Speaker Constructor + } +// class method to play a note based on PwmOut class + void PlayNote(float frequency, float duration, float volume) { + _pin.period(1.0/frequency); + _pin = volume/2.0; + wait(duration); + _pin = 0.0; + } + +private: +// sets up specified pin for PWM using PwmOut class + PwmOut _pin; +}; +// no external PullUp resistor needed +// Pushbutton from P8 to GND. +int main() { + Speaker mySpeaker(p21); + pb1.mode(PullUp); + pb2.mode(PullUp); + pb3.mode(PullUp); + pb4.mode(PullUp); + pb5.mode(PullUp); + pb6.mode(PullUp); + pb7.mode(PullUp); + while(1){ + led1=1; + while(led1==1){ + if(pb1!=1) { + mySpeaker.PlayNote(261.6, 0.5, 0.01); + led1=0; + wait(0.5); + } + } + led2 = 1; + while(led2==1){ + if(pb2 !=1){ + mySpeaker.PlayNote(293.7, 0.5, 0.01); + led2 = 0; + wait(0.5); + } + } + + led3=1; + while(led3==1){ + if(pb3 !=1){ + mySpeaker.PlayNote(329.6, 0.5, 0.01); + led3 = 0; + wait(0.5); + } + } + led4=1; + while(led4==1){ + if(pb4 !=1){ + mySpeaker.PlayNote(349.2, 0.5, 0.01); + led4 = 0; + wait(0.5); + } + } + led5=1; + while(led5==1){ + if(pb5 !=1){ + mySpeaker.PlayNote(391.9, 0.5, 0.01); + led5 = 0; + wait(0.5); + } + } + led6=1; + while(led6==1){ + if(pb6 !=1){ + mySpeaker.PlayNote(440.0, 0.5, 0.01); + led6 = 0; + wait(0.5); + } + } + led7=1; + while(led7==1){ + if(pb7 !=1){ + mySpeaker.PlayNote(493.9, 0.5, 0.01); + led7 = 0; + wait(0.5); + } + } +} +} \ No newline at end of file