Bing Xian Lim / Mbed 2 deprecated lab4pt1_copy

Dependencies:   HC_SR04_Ultrasonic_Library mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "motordriver.h"
00003 #include "ultrasonic.h"
00004 #include "SongPlayer.h"
00005 int x=1;
00006 
00007 
00008 void dist(int distance)
00009 {
00010     if (distance<100){
00011         x=0;
00012         }
00013     //put code here to execute when the distance has changed
00014     printf("Distance %d mm\r\n", distance);
00015 
00016 }
00017 
00018 
00019 ultrasonic mu(p9, p10, .1, 1, &dist);  
00020 //BusOut myled(LED1,LED2,LED3,LED4);
00021 DigitalOut led1(LED1);
00022 DigitalOut led2(LED2);
00023 DigitalOut led3(LED3);
00024 DigitalOut led4(LED4);
00025 Serial blue(p28,p27);
00026 AnalogIn ain(A0); //p15
00027 SongPlayer mySpeaker(p26);
00028 Motor A(p23, p6, p5, 1); // pwm, fwd, rev, can brake 
00029 Motor B(p21, p7, p8, 1); // pwm, fwd, rev, can brake
00030 
00031 float note[18]= {1568.0,1396.9,1244.5,1244.5,1396.9,1568.0,1568.0,1568.0,1396.9,
00032                  1244.5,1396.9,1568.0,1396.9,1244.5,1174.7,1244.5,1244.5, 0.0
00033                 };
00034 float duration[18]= {0.48,0.24,0.72,0.48,0.24,0.48,0.24,0.24,0.24,
00035                      0.24,0.24,0.24,0.24,0.48,0.24,0.48,0.48, 0.0
00036                     };
00037 
00038 int main()
00039 {
00040 
00041     mu.startUpdates();//start measuring the distance
00042     char bnum=0;
00043     char bhit=0;
00044     
00045     while(x==1){
00046         mu.checkDistance(); 
00047         
00048         led1 = (ain > 0.15f) ? 1 : 0;
00049         led2 = (ain > 0.30f) ? 1 : 0;
00050         led3 = (ain > 0.45f) ? 1 : 0;
00051         led4 = (ain > 0.7f) ? 1 : 0;
00052         A.speed(0.4); 
00053         B.speed(0.4);
00054 
00055         
00056         
00057 
00058        // printf("Distance %d mm\r\n", distance);
00059         
00060     }
00061         mySpeaker.PlaySong(note,duration);
00062     // loops forever while song continues to play to end using interrupts
00063         A.speed(0); 
00064         B.speed(0);
00065     while(1){
00066             if (blue.getc()=='!') {
00067                 if (blue.getc()=='B') { //button data packet
00068                     bnum = blue.getc(); //button number
00069                     bhit = blue.getc(); //1=hit, 0=release
00070                     if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK?
00071                        // myled = bnum - '0'; //current button number will appear on LEDs
00072                         switch (bnum) {
00073                             case '1': //number button 1
00074                                 if (bhit=='1') {
00075                                     A.speed(0); 
00076                                     B.speed(0);
00077                                 } else {
00078                                     //add release code here
00079                                 }
00080                                 break;
00081                             case '2': //number button 2
00082                                 if (bhit=='1') {
00083                                     //add hit code here
00084                                 } else {
00085                                     //add release code here
00086                                 }
00087                                 break;
00088                             case '3': //number button 3
00089                                 if (bhit=='1') {
00090                                     //add hit code here
00091                                 } else {
00092                                     //add release code here
00093                                 }
00094                                 break;
00095                             case '4': //number button 4
00096                                 if (bhit=='1') {
00097                                     //add hit code here
00098                                 } else {
00099                                     //add release code here
00100                                 }
00101                                 break;
00102                             case '5': //button 5 up arrow
00103                                 if (bhit=='1') {
00104                                     A.speed(1); 
00105                                     B.speed(1);
00106                                     wait(0.02);
00107                                 } else {
00108                                     //add release code here
00109                                 }
00110                                 break;
00111                             case '6': //button 6 down arrow
00112                                 if (bhit=='1') {
00113                                     A.speed(-1); 
00114                                     B.speed(-1);
00115                                     wait(0.02);
00116                                 } else {
00117                                     //add release code here
00118                                 }
00119                                 break;
00120                             case '7': //button 7 left arrow
00121                                 if (bhit=='1') {
00122                                     A.speed(-1); 
00123                                     B.speed(1);
00124                                     wait(0.02);
00125                                 } else {
00126                                     //add release code here
00127                                 }
00128                                 break;
00129                             case '8': //button 8 right arrow
00130                                 if (bhit=='1') {
00131                                     A.speed(1); 
00132                                     B.speed(-1);
00133                                     wait(0.02);
00134                                 } else {
00135                                     //add release code here
00136                                 }
00137                                 break;
00138                             default:
00139                                 A.speed(0.3); 
00140                                 B.speed(0.3);
00141                                 break;
00142                         }
00143                     }
00144                 }
00145            }
00146      }
00147 }