First Draft, serial print change based on distance

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "ultrasonic.h"
00003 #include "N5110.h"
00004 #include "Joystick.h"
00005 #include "beep.h"
00006 #include "main.h"
00007 
00008 /***************************************************************************//**
00009 *
00010 * the main code below sets all the interrupts to pull down and also for the start
00011 * and back buttons it is sent to the specific start and back functions which sets
00012 * the flags which are then used in other functions.
00013 *
00014 * the Ultrasonic.startUpdates() is where the ultrasonic sensor is told to start 
00015 * measuring the distances.
00016 * the joystick and lcd are then initialised and the main menu function is then
00017 * called.
00018 *
00019 *******************************************************************************/
00020 
00021 int main()
00022 {
00023 
00024     R.mode(PullDown);
00025     Start.mode(PullDown);
00026     Start.rise(Start_isr);
00027     Back.mode(PullDown);
00028     Back.rise(Back_isr);
00029     // printf("set buttons");
00030     
00031     Ultrasonic.startUpdates();
00032     // printf ("Ultrasonic Updates started");
00033     
00034     joystick.init();
00035     // printf("initialise joystick")
00036     init_display();
00037     
00038     main_menu();
00039     
00040 }
00041 
00042 /***************************************************************************//**
00043 *
00044 * added function which initalise the display and sets the contrast, this is
00045 * called from the int main and so the LCD boots up on startup.
00046 *
00047 *******************************************************************************/
00048 
00049 void init_display(){
00050     
00051     // printf("initialise display");    
00052     lcd.init();
00053     lcd.setContrast(0.4);
00054     lcd.clear(); 
00055         
00056     }
00057 
00058 /***************************************************************************//**
00059 *
00060 * this function is the main menu for the program, it is called at the start and 
00061 * depending on what string is selected, clicking the start button will send the
00062 * user to the specified function.
00063 *
00064 *******************************************************************************/      
00065     
00066 void main_menu(){
00067     
00068     // printf("main menu");
00069     
00070     lcd.clear();
00071     Start_flag = 0; // make sure this is set to 0 to begin with so that it doesnt operate erroneously
00072     int select = 0; // always want the main menu to start on the same value on reset/ returning from a different screen
00073     LEDS = 63; // 63 on the bus output sets all LEDs to off
00074 
00075     while (1) {
00076         
00077     Direction d = joystick.get_direction();  
00078     // printf("Direction = %i\n",d);
00079     
00080         switch(select) {
00081             case 0:
00082                 switch(d) {
00083                     case N:
00084                         select = 1;
00085                     // printf("UP");
00086                         break;
00087                     case S:
00088                         select = 1;
00089                     // printf("Down");
00090                         break;
00091 }
00092                 break;
00093             case 1:
00094                 switch(d) {
00095                     case N:
00096                         select = 0;
00097                     // printf("UP");
00098                         break;
00099                     case S:
00100                         select = 0;
00101                     // printf("Down");
00102                         break;
00103 }
00104                 break;
00105  
00106         }
00107                 wait(0.1);
00108         if (select == 0) {
00109             // printf("main menu - sense object");
00110             lcd.clear();
00111             lcd.printString("Detect",27,0);
00112             lcd.printString("Sense Object",7,2);
00113             lcd.printString("Calibration",7,3);
00114             lcd.drawCircle(3,19,2,FILL_TRANSPARENT);
00115             lcd.refresh();
00116             wait(0.15);
00117             if (Start_flag == 1) {
00118                 Start_flag = 0;
00119                 sense_object();
00120             }
00121         } else if (select == 1) {
00122             // printf("main menu - calibration");
00123             lcd.clear();
00124             lcd.printString("Detect",27,0);
00125             lcd.printString("Sense Object", 7,2);
00126             lcd.printString("Calibration",7,3);
00127             lcd.drawCircle(3,27,2,FILL_TRANSPARENT);
00128             lcd.refresh();
00129             wait(0.15);
00130             if (Start_flag == 1) {
00131                 Start_flag = 0;
00132                 calibrate_object();
00133             }
00134         }
00135     }
00136 }
00137 
00138 /***************************************************************************//**
00139 *
00140 * this function is for sensing the object, a switch statement is utilised here 
00141 * just as it was in the main menu but here it is used for indicating the six 
00142 * seperate readings that are to be taken.
00143 * the start button is used to take the measuement and the R button is used to
00144 * rotate the object
00145 *
00146 *******************************************************************************/ 
00147 
00148 void sense_object(){
00149     
00150     // printf("sense object");  
00151         
00152     // set inital state 
00153     int state = 0;
00154     
00155     while(1)
00156     {
00157     LEDS = fsm[state];  // this sets the LED bus output current state of the FSM
00158      //printf("state = %d\r\n",state);
00159     
00160     // check which state we are in and see which the next state should be
00161     switch(state) {
00162         case 0:{
00163         lcd.clear();
00164         lcd.printString(" object at 0'",0,0);
00165         lcd.printString("   R + 60'",0,2);
00166         lcd.printString("Start to take",0,4);
00167         lcd.printString("   Reading ",0,5);
00168         lcd.refresh();
00169             if (R == 1){
00170                 state = 1;}
00171             else state = 0;
00172             
00173              if (Start_flag == 1) {
00174                 Start_flag = 0;
00175                 object_sense1();
00176             }
00177 
00178                 break;}
00179                 
00180         case 1:{
00181         lcd.clear();
00182         lcd.printString(" object at 60'",0,0);
00183         lcd.printString("   R + 60'",0,2);
00184         lcd.printString("Start to take",0,4);
00185         lcd.printString("   Reading ",0,5);
00186         lcd.refresh();
00187             if (R == 1){
00188                 state = 2;}
00189             else state = 1;
00190             
00191             if (Start_flag == 1) {
00192                 Start_flag = 0;
00193                 object_sense2();
00194             }
00195             
00196             break;}
00197         case 2:{
00198         lcd.clear();
00199         lcd.printString(" object at 120'",0,0);
00200         lcd.printString("   R + 60'",0,2);
00201         lcd.printString("Start to take",0,4);
00202         lcd.printString("   Reading ",0,5);
00203         lcd.refresh();
00204             if (R == 1){
00205                 state = 3;}
00206             else state = 2;
00207                 
00208             if (Start_flag == 1) {
00209                 Start_flag = 0;
00210                 object_sense3();
00211             }
00212             
00213             break;}
00214             
00215         case 3:{
00216         lcd.clear();
00217         lcd.printString(" object at 180'",0,0);
00218         lcd.printString("   R + 60'",0,2);
00219         lcd.printString("Start to take",0,4);
00220         lcd.printString("   Reading ",0,5);
00221         lcd.refresh();
00222             if (R == 1){
00223                 state = 4;}
00224             else state = 3;
00225                 
00226             if (Start_flag == 1) {
00227                 Start_flag = 0;
00228                 object_sense4();
00229             }
00230             
00231             break;}
00232             
00233         case 4:{
00234         lcd.clear();
00235         lcd.printString(" object at 240'",0,0);
00236         lcd.printString("   R + 60'",0,2);
00237         lcd.printString("Start to take",0,4);
00238         lcd.printString("   Reading ",0,5);
00239         lcd.refresh();
00240             if (R == 1){
00241                 state = 5;}
00242             else state = 4;
00243                 
00244             if (Start_flag == 1) {
00245                 Start_flag = 0;
00246                 object_sense5();
00247             }
00248             
00249             break;}
00250             
00251          case 5:{
00252         lcd.clear();
00253         lcd.printString(" object at 300'",0,0);
00254         lcd.printString("   R + 60'",0,2);
00255         lcd.printString("Start to take",0,4);
00256         lcd.printString("   Reading ",0,5);
00257         lcd.refresh();
00258             if (R == 1){
00259                 object_detection();}
00260             else state = 5;
00261 
00262             if (Start_flag == 1) {
00263                 Start_flag = 0;
00264                 object_sense6();
00265             }
00266             
00267             break;}     
00268               
00269             default:{
00270                 error("Invalid state");  //invalid state - call error routine
00271                 // or could jump to starting state i.e. state = 0
00272                 break;}
00273         }
00274         if (Back_flag == 1) {
00275             Back_flag = 0;
00276             main_menu();}
00277             
00278         ThisThread::sleep_for(200);
00279         }
00280         }
00281 
00282 /***************************************************************************//**
00283 *
00284 * this function is for calibrating the distance in which the object centre point
00285 * should be away from the sensor, it checks the current distance and then though
00286 * the function "void dist" displays the current distance on the screen.
00287 * this function also tells the user the distance away from the target centre 
00288 * point this should be, this is so that the board is the right distance away and
00289 * will be able to detect the object better.
00290 *
00291 * in order to print the distance onto the LCD the current distance is printed to
00292 * a buffer which is a max length of 14 (the amount of characters that can fit
00293 * width ways on the screen) this can then be printed on the screen using print
00294 * string.
00295 *
00296 *******************************************************************************/
00297      
00298 void calibrate_object(){
00299      
00300      //sense = 0;
00301      
00302      while(1)
00303     {
00304         Ultrasonic.checkDistance();    
00305         
00306         if (Back_flag == 1) {
00307             Back_flag = 0;
00308             main_menu();}
00309             
00310         wait (1);
00311         }
00312         }
00313 
00314 void dist(int distance)
00315 {
00316      //printf("sense = %d\n", sense);
00317     // printf("Distance changed to %dmm\r\n", distance);
00318     
00319             lcd.clear();
00320      
00321             char buffer[14];
00322             int length = sprintf(buffer,"%dmm", distance);
00323             if (length <= 14)
00324     
00325             lcd.printString("set object to",0,0);
00326             lcd.printString("    200mm",0,1);
00327             lcd.printString(buffer,24,3);
00328             lcd.refresh();
00329      
00330     }
00331 
00332 /***************************************************************************//**
00333 *
00334 * these functions are called when the start button is pressed oin the sense object
00335 * function, the purpose of these is to store the measured values for comparison
00336 * and to make a buzzer sound to indicate to the user that the value has been
00337 * measured
00338 *
00339 *******************************************************************************/
00340 
00341 void object_sense1(){
00342      
00343         //sense = 1;
00344      
00345         Distance1 = Ultrasonic.getCurrentDistance();
00346         
00347         buzzer.beep(1000,2);
00348         wait(0.5);
00349         buzzer.beep(0,0);
00350         
00351         //printf("distance at sense 1 = %d\r\n", Distance1);
00352 
00353         }
00354 
00355 void object_sense2(){
00356      
00357         //sense = 2;
00358      
00359         Distance2 = Ultrasonic.getCurrentDistance();
00360         
00361         buzzer.beep(1000,2);
00362         wait(0.5);
00363         buzzer.beep(0,0);
00364 
00365         //printf("distance at sense 2 = %d\r\n", Distance2);
00366 
00367         }
00368         
00369 void object_sense3(){
00370      
00371         //sense = 3;
00372      
00373         Distance3 = Ultrasonic.getCurrentDistance();
00374         
00375         buzzer.beep(1000,2);
00376         wait(0.5);
00377         buzzer.beep(0,0);
00378 
00379         //printf("distance at sense 3 = %d\r\n", Distance3);
00380   
00381         }
00382         
00383 void object_sense4(){
00384      
00385         //sense = 4;
00386      
00387         Distance4 = Ultrasonic.getCurrentDistance();
00388         
00389         buzzer.beep(1000,2);
00390         wait(0.5);
00391         buzzer.beep(0,0);
00392 
00393         //printf("distance at sense 4 = %d\r\n", Distance4);
00394      
00395         }
00396         
00397 void object_sense5(){
00398      
00399         //sense = 5;
00400      
00401         Distance5 = Ultrasonic.getCurrentDistance();
00402         
00403         buzzer.beep(1000,2);
00404         wait(0.5);
00405         buzzer.beep(0,0);
00406         
00407         //printf("distance at sense 5 = %d\r\n", Distance5);
00408      
00409         }
00410         
00411 void object_sense6(){
00412      
00413         //sense = 6;
00414      
00415         Distance6 = Ultrasonic.getCurrentDistance();
00416         
00417         buzzer.beep(1000,2);
00418         wait(0.5);
00419         buzzer.beep(0,0);
00420         
00421         //printf("distance at sense 6 = %d\r\n", Distance6);
00422  
00423         }
00424 
00425 /***************************************************************************//**
00426 *
00427 * the object detection function is where the comparison between the measured
00428 * values and the defined object arrays occurs, if the measured values match the
00429 * arrays then the program will see it as an object found and will display this on
00430 * the screen, otherwise it will ask the user to try again. a +/- 5mm deadband has
00431 * been added to allow for minor erros in the measured values.
00432 *
00433 *******************************************************************************/
00434 
00435 void object_detection(){
00436             
00437             lcd.clear();
00438     
00439             //printf("Distance 1 = %d\n", Distance1);
00440             //printf("glass[0] = %d\n", glass[0]);
00441             //printf("glass[6] = %d\n", glass[6]);
00442             if (abs(Distance1 - glass[0]) <= 5 and abs(Distance2 - glass[1]) <= 5 and abs(Distance3 - glass[2]) <= 5 and abs(Distance4 - glass[3]) <= 5 and abs(Distance5 - glass[4]) <= 5 and abs(Distance6 - glass[5]) <= 5){       
00443             //printf("glass\n");
00444             lcd.printString(" Object Found!",0,1);
00445             lcd.printString("  Pint Glass",0,3);
00446             lcd.refresh();}
00447             
00448             else if (abs(Distance1 - firestick_box[0]) <= 5 and abs(Distance2 - firestick_box[1]) <= 5 and abs(Distance3 - firestick_box[2]) <= 5 and abs(Distance4 - firestick_box[3]) <= 5 and abs(Distance5 - firestick_box[4]) <= 5 and abs(Distance6 - firestick_box[5]) <= 5){       
00449             //printf("Firestick Box\n");
00450             lcd.printString(" Object Found!",0,1);
00451             lcd.printString(" Firestick Box",0,3);
00452             lcd.refresh();}
00453             
00454             else if (abs(Distance1 - Disaronno_bottle[0]) <= 5 and abs(Distance2 - Disaronno_bottle[1]) <= 5 and abs(Distance3 - Disaronno_bottle[2]) <= 5 and abs(Distance4 - Disaronno_bottle[3]) <= 5 and abs(Distance5 - Disaronno_bottle[4]) <= 5 and abs(Distance6 - Disaronno_bottle[5]) <= 5){       
00455             //printf("Disaronno Bottle\n");
00456             lcd.printString(" Object Found!",0,1);
00457             lcd.printString("  Disaronno!",0,3);
00458             lcd.refresh();}
00459                      
00460             else{
00461             //printf("nothing\n");
00462             lcd.printString("  No Object!",0,1);
00463             lcd.printString("  Please Try",0,3);
00464             lcd.printString("Again",24,4);
00465             lcd.refresh();}
00466             }
00467     
00468 /***************************************************************************//**
00469 *
00470 * these functions are for setting the flags for the start and back butons which
00471 * are used in the other functions.
00472 *
00473 *******************************************************************************/
00474       
00475 void Start_isr()
00476     {
00477     // printf("start button pressed"); 
00478     Start_flag = 1;
00479     }
00480 
00481 void Back_isr()
00482     {
00483     // printf("back button pressed"); 
00484     Back_flag = 1;
00485     }
00486