its a test

Dependencies:   mbed HC_SR04_Ultrasonic_Library

Committer:
JinghanYu
Date:
Sat May 18 17:12:42 2019 +0000
Revision:
0:c019b7e987f6
it's a test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JinghanYu 0:c019b7e987f6 1 #include "mbed.h"
JinghanYu 0:c019b7e987f6 2 #include "ultrasonic.h"
JinghanYu 0:c019b7e987f6 3 #include "SongPlayer.h"
JinghanYu 0:c019b7e987f6 4
JinghanYu 0:c019b7e987f6 5 #define mDo 523.25
JinghanYu 0:c019b7e987f6 6 #define mDoplus 554.37
JinghanYu 0:c019b7e987f6 7 #define mRe 587.33
JinghanYu 0:c019b7e987f6 8 #define mMi 659.26
JinghanYu 0:c019b7e987f6 9 #define mFa 698.46
JinghanYu 0:c019b7e987f6 10 #define mFaplus 740
JinghanYu 0:c019b7e987f6 11 #define mSol 783.99
JinghanYu 0:c019b7e987f6 12 #define mLa 880
JinghanYu 0:c019b7e987f6 13 #define mSi 987.77
JinghanYu 0:c019b7e987f6 14 #define hDo 1046.5
JinghanYu 0:c019b7e987f6 15 #define hRe 1174.7
JinghanYu 0:c019b7e987f6 16 #define hMi 1318.5
JinghanYu 0:c019b7e987f6 17 #define hFa 1396.9
JinghanYu 0:c019b7e987f6 18 #define hSol 1568.9
JinghanYu 0:c019b7e987f6 19 #define hLa 1760.0
JinghanYu 0:c019b7e987f6 20 #define hSi 1975.5
JinghanYu 0:c019b7e987f6 21 #define half 0.24
JinghanYu 0:c019b7e987f6 22 #define full 0.48
JinghanYu 0:c019b7e987f6 23 #define threehalf 0.72
JinghanYu 0:c019b7e987f6 24 #define twofull 0.96
JinghanYu 0:c019b7e987f6 25 #define DELAY_M 4000
JinghanYu 0:c019b7e987f6 26 #define DELAY_U 5500
JinghanYu 0:c019b7e987f6 27
JinghanYu 0:c019b7e987f6 28 float note_song1[9]= {mSol,mSol,mLa,mSol,mRe,mMi,mRe,mDo,0.0};
JinghanYu 0:c019b7e987f6 29 float duration_song1[9]= {full,full,full,full,full,half,half,twofull,0.0};
JinghanYu 0:c019b7e987f6 30 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};
JinghanYu 0:c019b7e987f6 31 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};
JinghanYu 0:c019b7e987f6 32
JinghanYu 0:c019b7e987f6 33 DigitalOut led(LED1);
JinghanYu 0:c019b7e987f6 34 DigitalIn mercury1(D8);
JinghanYu 0:c019b7e987f6 35 PwmOut music(D6);
JinghanYu 0:c019b7e987f6 36
JinghanYu 0:c019b7e987f6 37 void dist(int distance)
JinghanYu 0:c019b7e987f6 38 {
JinghanYu 0:c019b7e987f6 39 //put code here to happen when the distance is changed
JinghanYu 0:c019b7e987f6 40 if(distance<500){
JinghanYu 0:c019b7e987f6 41 SongPlayer mySpeaker(D6);
JinghanYu 0:c019b7e987f6 42 mySpeaker.PlaySong(note_song2,duration_song2);
JinghanYu 0:c019b7e987f6 43 wait_ms(DELAY_U);
JinghanYu 0:c019b7e987f6 44 }
JinghanYu 0:c019b7e987f6 45 else{
JinghanYu 0:c019b7e987f6 46 wait_ms(500);
JinghanYu 0:c019b7e987f6 47 }
JinghanYu 0:c019b7e987f6 48 printf("Distance changed to %dmm\r\n", distance);
JinghanYu 0:c019b7e987f6 49 }
JinghanYu 0:c019b7e987f6 50
JinghanYu 0:c019b7e987f6 51 ultrasonic mu(D9, D10, .5, 1, &dist); //Set the trigger pin to D8 and the echo pin to D9
JinghanYu 0:c019b7e987f6 52 //have updates every .5 seconds and a timeout after 1
JinghanYu 0:c019b7e987f6 53 //second, and call dist when the distance changes
JinghanYu 0:c019b7e987f6 54 int main() {
JinghanYu 0:c019b7e987f6 55
JinghanYu 0:c019b7e987f6 56 mu.startUpdates();//start mesuring the distance
JinghanYu 0:c019b7e987f6 57 while(1) {
JinghanYu 0:c019b7e987f6 58 if(mercury1 == 0){
JinghanYu 0:c019b7e987f6 59 led = 0;
JinghanYu 0:c019b7e987f6 60 wait_ms(500);
JinghanYu 0:c019b7e987f6 61 }
JinghanYu 0:c019b7e987f6 62 else{
JinghanYu 0:c019b7e987f6 63 led = 1;
JinghanYu 0:c019b7e987f6 64 SongPlayer mySpeaker(D6);
JinghanYu 0:c019b7e987f6 65 mySpeaker.PlaySong(note_song1,duration_song1);
JinghanYu 0:c019b7e987f6 66 wait_ms(DELAY_M);
JinghanYu 0:c019b7e987f6 67 }
JinghanYu 0:c019b7e987f6 68 mu.checkDistance(); //call checkDistance() as much as possible, as this is where
JinghanYu 0:c019b7e987f6 69 //the class checks if dist needs to be called.
JinghanYu 0:c019b7e987f6 70 }
JinghanYu 0:c019b7e987f6 71 }