its a test
Dependencies: mbed HC_SR04_Ultrasonic_Library
Revision 0:c019b7e987f6, committed 2019-05-18
- Comitter:
- JinghanYu
- Date:
- Sat May 18 17:12:42 2019 +0000
- Commit message:
- it's a test
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HC_SR04_Ultrasonic_Library.lib Sat May 18 17:12:42 2019 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/ejteb/code/HC_SR04_Ultrasonic_Library/#e0f9c9fb4cf3
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SongPlayer.h Sat May 18 17:12:42 2019 +0000 @@ -0,0 +1,41 @@ +#include "mbed.h" +// new class to play a note on Speaker based on PwmOut class +class SongPlayer +{ +public: + SongPlayer(PinName pin) : _pin(pin) { +// _pin(pin) means pass pin to the constructor + } +// class method to play a note based on PwmOut class + void PlaySong(float frequency[], float duration[], float volume=1.0) { + vol = volume; + notecount = 0; + _pin.period(1.0/frequency[notecount]); + _pin = volume/2.0; + noteduration.attach(this,&SongPlayer::nextnote, duration[notecount]); + // setup timer to interrupt for next note to play + frequencyptr = frequency; + durationptr = duration; + //returns after first note starts to play + } + void nextnote(); +private: + Timeout noteduration; + PwmOut _pin; + int notecount; + float vol; + float * frequencyptr; + float * durationptr; +}; +//Interrupt Routine to play next note +void SongPlayer::nextnote() +{ + _pin = 0.0; + notecount++; //setup next note in song + if (durationptr[notecount]!=0.0) { + _pin.period(1.0/frequencyptr[notecount]); + noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]); + _pin = vol/2.0; + } else + _pin = 0.0; //turn off on last note +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sat May 18 17:12:42 2019 +0000 @@ -0,0 +1,71 @@ +#include "mbed.h" +#include "ultrasonic.h" +#include "SongPlayer.h" + +#define mDo 523.25 +#define mDoplus 554.37 +#define mRe 587.33 +#define mMi 659.26 +#define mFa 698.46 +#define mFaplus 740 +#define mSol 783.99 +#define mLa 880 +#define mSi 987.77 +#define hDo 1046.5 +#define hRe 1174.7 +#define hMi 1318.5 +#define hFa 1396.9 +#define hSol 1568.9 +#define hLa 1760.0 +#define hSi 1975.5 +#define half 0.24 +#define full 0.48 +#define threehalf 0.72 +#define twofull 0.96 +#define DELAY_M 4000 +#define DELAY_U 5500 + +float note_song1[9]= {mSol,mSol,mLa,mSol,mRe,mMi,mRe,mDo,0.0}; +float duration_song1[9]= {full,full,full,full,full,half,half,twofull,0.0}; +float note_song2[20]= {mSol,mFaplus,mSol,hDo,mSol,mMi,mRe,mDoplus,mRe,mSol,mFa,mRe,mDo,mMi,mSol,mMi,mSol,mMi,mDo,0.0}; +float duration_song2[20]= {half,half,half,half,half,half,half,half,half,half,half,half,half,half,half,half,half,half,full,0.0}; + +DigitalOut led(LED1); +DigitalIn mercury1(D8); +PwmOut music(D6); + +void dist(int distance) +{ + //put code here to happen when the distance is changed + if(distance<500){ + SongPlayer mySpeaker(D6); + mySpeaker.PlaySong(note_song2,duration_song2); + wait_ms(DELAY_U); + } + else{ + wait_ms(500); + } + printf("Distance changed to %dmm\r\n", distance); +} + +ultrasonic mu(D9, D10, .5, 1, &dist); //Set the trigger pin to D8 and the echo pin to D9 + //have updates every .5 seconds and a timeout after 1 + //second, and call dist when the distance changes +int main() { + + mu.startUpdates();//start mesuring the distance + while(1) { + if(mercury1 == 0){ + led = 0; + wait_ms(500); + } + else{ + led = 1; + SongPlayer mySpeaker(D6); + mySpeaker.PlaySong(note_song1,duration_song1); + wait_ms(DELAY_M); + } + mu.checkDistance(); //call checkDistance() as much as possible, as this is where + //the class checks if dist needs to be called. + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sat May 18 17:12:42 2019 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file