First Draft, serial print change based on distance

Committer:
liam94
Date:
Fri Feb 04 18:30:13 2022 +0000
Revision:
17:02676e9bbc73
Parent:
16:af15244242c2
final update to all comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
liam94 0:506531d0531c 1 #include "mbed.h"
liam94 0:506531d0531c 2 #include "ultrasonic.h"
liam94 0:506531d0531c 3 #include "N5110.h"
liam94 5:98845ccaaacd 4 #include "Joystick.h"
liam94 14:837945ccd8c0 5 #include "beep.h"
liam94 5:98845ccaaacd 6 #include "main.h"
liam94 4:77500a7f951d 7
liam94 8:7e48229d678c 8 /***************************************************************************//**
liam94 8:7e48229d678c 9 *
liam94 8:7e48229d678c 10 * the main code below sets all the interrupts to pull down and also for the start
liam94 8:7e48229d678c 11 * and back buttons it is sent to the specific start and back functions which sets
liam94 8:7e48229d678c 12 * the flags which are then used in other functions.
liam94 8:7e48229d678c 13 *
liam94 17:02676e9bbc73 14 * the Ultrasonic.startUpdates() is where the ultrasonic sensor is told to start
liam94 17:02676e9bbc73 15 * measuring the distances.
liam94 16:af15244242c2 16 * the joystick and lcd are then initialised and the main menu function is then
liam94 8:7e48229d678c 17 * called.
liam94 8:7e48229d678c 18 *
liam94 8:7e48229d678c 19 *******************************************************************************/
liam94 2:3ace5b4ae9a7 20
liam94 2:3ace5b4ae9a7 21 int main()
liam94 1:a1795335ef8c 22 {
liam94 5:98845ccaaacd 23
liam94 3:0b0fbddb6f51 24 R.mode(PullDown);
liam94 5:98845ccaaacd 25 Start.mode(PullDown);
liam94 5:98845ccaaacd 26 Start.rise(Start_isr);
liam94 5:98845ccaaacd 27 Back.mode(PullDown);
liam94 5:98845ccaaacd 28 Back.rise(Back_isr);
liam94 8:7e48229d678c 29 // printf("set buttons");
liam94 8:7e48229d678c 30
liam94 16:af15244242c2 31 Ultrasonic.startUpdates();
liam94 8:7e48229d678c 32 // printf ("Ultrasonic Updates started");
liam94 5:98845ccaaacd 33
liam94 5:98845ccaaacd 34 joystick.init();
liam94 8:7e48229d678c 35 // printf("initialise joystick")
liam94 5:98845ccaaacd 36 init_display();
liam94 8:7e48229d678c 37
liam94 5:98845ccaaacd 38 main_menu();
liam94 5:98845ccaaacd 39
liam94 5:98845ccaaacd 40 }
liam94 5:98845ccaaacd 41
liam94 8:7e48229d678c 42 /***************************************************************************//**
liam94 8:7e48229d678c 43 *
liam94 8:7e48229d678c 44 * added function which initalise the display and sets the contrast, this is
liam94 8:7e48229d678c 45 * called from the int main and so the LCD boots up on startup.
liam94 8:7e48229d678c 46 *
liam94 8:7e48229d678c 47 *******************************************************************************/
liam94 8:7e48229d678c 48
liam94 5:98845ccaaacd 49 void init_display(){
liam94 8:7e48229d678c 50
liam94 8:7e48229d678c 51 // printf("initialise display");
liam94 4:77500a7f951d 52 lcd.init();
liam94 4:77500a7f951d 53 lcd.setContrast(0.4);
liam94 8:7e48229d678c 54 lcd.clear();
liam94 5:98845ccaaacd 55
liam94 5:98845ccaaacd 56 }
liam94 8:7e48229d678c 57
liam94 8:7e48229d678c 58 /***************************************************************************//**
liam94 8:7e48229d678c 59 *
liam94 8:7e48229d678c 60 * this function is the main menu for the program, it is called at the start and
liam94 17:02676e9bbc73 61 * depending on what string is selected, clicking the start button will send the
liam94 8:7e48229d678c 62 * user to the specified function.
liam94 8:7e48229d678c 63 *
liam94 8:7e48229d678c 64 *******************************************************************************/
liam94 5:98845ccaaacd 65
liam94 5:98845ccaaacd 66 void main_menu(){
liam94 5:98845ccaaacd 67
liam94 8:7e48229d678c 68 // printf("main menu");
liam94 8:7e48229d678c 69
liam94 5:98845ccaaacd 70 lcd.clear();
liam94 17:02676e9bbc73 71 Start_flag = 0; // make sure this is set to 0 to begin with so that it doesnt operate erroneously
liam94 17:02676e9bbc73 72 int select = 0; // always want the main menu to start on the same value on reset/ returning from a different screen
liam94 13:cc99df342c7f 73 LEDS = 63; // 63 on the bus output sets all LEDs to off
liam94 5:98845ccaaacd 74
liam94 5:98845ccaaacd 75 while (1) {
liam94 5:98845ccaaacd 76
liam94 17:02676e9bbc73 77 Direction d = joystick.get_direction();
liam94 8:7e48229d678c 78 // printf("Direction = %i\n",d);
liam94 5:98845ccaaacd 79
liam94 5:98845ccaaacd 80 switch(select) {
liam94 5:98845ccaaacd 81 case 0:
liam94 5:98845ccaaacd 82 switch(d) {
liam94 8:7e48229d678c 83 case N:
liam94 5:98845ccaaacd 84 select = 1;
liam94 8:7e48229d678c 85 // printf("UP");
liam94 5:98845ccaaacd 86 break;
liam94 8:7e48229d678c 87 case S:
liam94 5:98845ccaaacd 88 select = 1;
liam94 8:7e48229d678c 89 // printf("Down");
liam94 5:98845ccaaacd 90 break;
liam94 5:98845ccaaacd 91 }
liam94 5:98845ccaaacd 92 break;
liam94 5:98845ccaaacd 93 case 1:
liam94 5:98845ccaaacd 94 switch(d) {
liam94 8:7e48229d678c 95 case N:
liam94 5:98845ccaaacd 96 select = 0;
liam94 8:7e48229d678c 97 // printf("UP");
liam94 5:98845ccaaacd 98 break;
liam94 8:7e48229d678c 99 case S:
liam94 5:98845ccaaacd 100 select = 0;
liam94 8:7e48229d678c 101 // printf("Down");
liam94 5:98845ccaaacd 102 break;
liam94 5:98845ccaaacd 103 }
liam94 5:98845ccaaacd 104 break;
liam94 5:98845ccaaacd 105
liam94 5:98845ccaaacd 106 }
liam94 5:98845ccaaacd 107 wait(0.1);
liam94 5:98845ccaaacd 108 if (select == 0) {
liam94 8:7e48229d678c 109 // printf("main menu - sense object");
liam94 5:98845ccaaacd 110 lcd.clear();
liam94 5:98845ccaaacd 111 lcd.printString("Detect",27,0);
liam94 5:98845ccaaacd 112 lcd.printString("Sense Object",7,2);
liam94 5:98845ccaaacd 113 lcd.printString("Calibration",7,3);
liam94 5:98845ccaaacd 114 lcd.drawCircle(3,19,2,FILL_TRANSPARENT);
liam94 5:98845ccaaacd 115 lcd.refresh();
liam94 5:98845ccaaacd 116 wait(0.15);
liam94 5:98845ccaaacd 117 if (Start_flag == 1) {
liam94 5:98845ccaaacd 118 Start_flag = 0;
liam94 5:98845ccaaacd 119 sense_object();
liam94 5:98845ccaaacd 120 }
liam94 5:98845ccaaacd 121 } else if (select == 1) {
liam94 8:7e48229d678c 122 // printf("main menu - calibration");
liam94 5:98845ccaaacd 123 lcd.clear();
liam94 5:98845ccaaacd 124 lcd.printString("Detect",27,0);
liam94 5:98845ccaaacd 125 lcd.printString("Sense Object", 7,2);
liam94 5:98845ccaaacd 126 lcd.printString("Calibration",7,3);
liam94 5:98845ccaaacd 127 lcd.drawCircle(3,27,2,FILL_TRANSPARENT);
liam94 5:98845ccaaacd 128 lcd.refresh();
liam94 5:98845ccaaacd 129 wait(0.15);
liam94 5:98845ccaaacd 130 if (Start_flag == 1) {
liam94 5:98845ccaaacd 131 Start_flag = 0;
liam94 6:18a4dd77057e 132 calibrate_object();
liam94 5:98845ccaaacd 133 }
liam94 5:98845ccaaacd 134 }
liam94 5:98845ccaaacd 135 }
liam94 5:98845ccaaacd 136 }
liam94 5:98845ccaaacd 137
liam94 8:7e48229d678c 138 /***************************************************************************//**
liam94 8:7e48229d678c 139 *
liam94 17:02676e9bbc73 140 * this function is for sensing the object, a switch statement is utilised here
liam94 17:02676e9bbc73 141 * just as it was in the main menu but here it is used for indicating the six
liam94 17:02676e9bbc73 142 * seperate readings that are to be taken.
liam94 17:02676e9bbc73 143 * the start button is used to take the measuement and the R button is used to
liam94 17:02676e9bbc73 144 * rotate the object
liam94 8:7e48229d678c 145 *
liam94 8:7e48229d678c 146 *******************************************************************************/
liam94 8:7e48229d678c 147
liam94 5:98845ccaaacd 148 void sense_object(){
liam94 8:7e48229d678c 149
liam94 8:7e48229d678c 150 // printf("sense object");
liam94 5:98845ccaaacd 151
liam94 5:98845ccaaacd 152 // set inital state
liam94 5:98845ccaaacd 153 int state = 0;
liam94 5:98845ccaaacd 154
liam94 2:3ace5b4ae9a7 155 while(1)
liam94 2:3ace5b4ae9a7 156 {
liam94 17:02676e9bbc73 157 LEDS = fsm[state]; // this sets the LED bus output current state of the FSM
liam94 17:02676e9bbc73 158 //printf("state = %d\r\n",state);
liam94 8:7e48229d678c 159
liam94 8:7e48229d678c 160 // check which state we are in and see which the next state should be
liam94 4:77500a7f951d 161 switch(state) {
liam94 9:ada61082bbaa 162 case 0:{
liam94 4:77500a7f951d 163 lcd.clear();
liam94 4:77500a7f951d 164 lcd.printString(" object at 0'",0,0);
liam94 4:77500a7f951d 165 lcd.printString(" R + 60'",0,2);
liam94 13:cc99df342c7f 166 lcd.printString("Start to take",0,4);
liam94 13:cc99df342c7f 167 lcd.printString(" Reading ",0,5);
liam94 4:77500a7f951d 168 lcd.refresh();
liam94 3:0b0fbddb6f51 169 if (R == 1){
liam94 3:0b0fbddb6f51 170 state = 1;}
liam94 3:0b0fbddb6f51 171 else state = 0;
liam94 9:ada61082bbaa 172
liam94 9:ada61082bbaa 173 if (Start_flag == 1) {
liam94 9:ada61082bbaa 174 Start_flag = 0;
liam94 9:ada61082bbaa 175 object_sense1();
liam94 9:ada61082bbaa 176 }
liam94 9:ada61082bbaa 177
liam94 9:ada61082bbaa 178 break;}
liam94 10:be53044119d1 179
liam94 9:ada61082bbaa 180 case 1:{
liam94 4:77500a7f951d 181 lcd.clear();
liam94 4:77500a7f951d 182 lcd.printString(" object at 60'",0,0);
liam94 4:77500a7f951d 183 lcd.printString(" R + 60'",0,2);
liam94 13:cc99df342c7f 184 lcd.printString("Start to take",0,4);
liam94 13:cc99df342c7f 185 lcd.printString(" Reading ",0,5);
liam94 4:77500a7f951d 186 lcd.refresh();
liam94 3:0b0fbddb6f51 187 if (R == 1){
liam94 3:0b0fbddb6f51 188 state = 2;}
liam94 3:0b0fbddb6f51 189 else state = 1;
liam94 9:ada61082bbaa 190
liam94 9:ada61082bbaa 191 if (Start_flag == 1) {
liam94 9:ada61082bbaa 192 Start_flag = 0;
liam94 9:ada61082bbaa 193 object_sense2();
liam94 9:ada61082bbaa 194 }
liam94 10:be53044119d1 195
liam94 10:be53044119d1 196 break;}
liam94 9:ada61082bbaa 197 case 2:{
liam94 4:77500a7f951d 198 lcd.clear();
liam94 4:77500a7f951d 199 lcd.printString(" object at 120'",0,0);
liam94 4:77500a7f951d 200 lcd.printString(" R + 60'",0,2);
liam94 13:cc99df342c7f 201 lcd.printString("Start to take",0,4);
liam94 13:cc99df342c7f 202 lcd.printString(" Reading ",0,5);
liam94 4:77500a7f951d 203 lcd.refresh();
liam94 3:0b0fbddb6f51 204 if (R == 1){
liam94 3:0b0fbddb6f51 205 state = 3;}
liam94 3:0b0fbddb6f51 206 else state = 2;
liam94 9:ada61082bbaa 207
liam94 9:ada61082bbaa 208 if (Start_flag == 1) {
liam94 9:ada61082bbaa 209 Start_flag = 0;
liam94 9:ada61082bbaa 210 object_sense3();
liam94 9:ada61082bbaa 211 }
liam94 10:be53044119d1 212
liam94 10:be53044119d1 213 break;}
liam94 10:be53044119d1 214
liam94 9:ada61082bbaa 215 case 3:{
liam94 4:77500a7f951d 216 lcd.clear();
liam94 4:77500a7f951d 217 lcd.printString(" object at 180'",0,0);
liam94 4:77500a7f951d 218 lcd.printString(" R + 60'",0,2);
liam94 13:cc99df342c7f 219 lcd.printString("Start to take",0,4);
liam94 13:cc99df342c7f 220 lcd.printString(" Reading ",0,5);
liam94 4:77500a7f951d 221 lcd.refresh();
liam94 3:0b0fbddb6f51 222 if (R == 1){
liam94 3:0b0fbddb6f51 223 state = 4;}
liam94 3:0b0fbddb6f51 224 else state = 3;
liam94 9:ada61082bbaa 225
liam94 9:ada61082bbaa 226 if (Start_flag == 1) {
liam94 9:ada61082bbaa 227 Start_flag = 0;
liam94 9:ada61082bbaa 228 object_sense4();
liam94 9:ada61082bbaa 229 }
liam94 10:be53044119d1 230
liam94 10:be53044119d1 231 break;}
liam94 10:be53044119d1 232
liam94 9:ada61082bbaa 233 case 4:{
liam94 4:77500a7f951d 234 lcd.clear();
liam94 4:77500a7f951d 235 lcd.printString(" object at 240'",0,0);
liam94 4:77500a7f951d 236 lcd.printString(" R + 60'",0,2);
liam94 13:cc99df342c7f 237 lcd.printString("Start to take",0,4);
liam94 13:cc99df342c7f 238 lcd.printString(" Reading ",0,5);
liam94 4:77500a7f951d 239 lcd.refresh();
liam94 3:0b0fbddb6f51 240 if (R == 1){
liam94 3:0b0fbddb6f51 241 state = 5;}
liam94 3:0b0fbddb6f51 242 else state = 4;
liam94 9:ada61082bbaa 243
liam94 9:ada61082bbaa 244 if (Start_flag == 1) {
liam94 9:ada61082bbaa 245 Start_flag = 0;
liam94 9:ada61082bbaa 246 object_sense5();
liam94 9:ada61082bbaa 247 }
liam94 10:be53044119d1 248
liam94 10:be53044119d1 249 break;}
liam94 10:be53044119d1 250
liam94 9:ada61082bbaa 251 case 5:{
liam94 4:77500a7f951d 252 lcd.clear();
liam94 4:77500a7f951d 253 lcd.printString(" object at 300'",0,0);
liam94 4:77500a7f951d 254 lcd.printString(" R + 60'",0,2);
liam94 13:cc99df342c7f 255 lcd.printString("Start to take",0,4);
liam94 13:cc99df342c7f 256 lcd.printString(" Reading ",0,5);
liam94 4:77500a7f951d 257 lcd.refresh();
liam94 3:0b0fbddb6f51 258 if (R == 1){
liam94 10:be53044119d1 259 object_detection();}
liam94 3:0b0fbddb6f51 260 else state = 5;
liam94 10:be53044119d1 261
liam94 9:ada61082bbaa 262 if (Start_flag == 1) {
liam94 9:ada61082bbaa 263 Start_flag = 0;
liam94 9:ada61082bbaa 264 object_sense6();
liam94 9:ada61082bbaa 265 }
liam94 10:be53044119d1 266
liam94 10:be53044119d1 267 break;}
liam94 9:ada61082bbaa 268
liam94 9:ada61082bbaa 269 default:{
liam94 2:3ace5b4ae9a7 270 error("Invalid state"); //invalid state - call error routine
liam94 2:3ace5b4ae9a7 271 // or could jump to starting state i.e. state = 0
liam94 9:ada61082bbaa 272 break;}
liam94 2:3ace5b4ae9a7 273 }
liam94 6:18a4dd77057e 274 if (Back_flag == 1) {
liam94 6:18a4dd77057e 275 Back_flag = 0;
liam94 6:18a4dd77057e 276 main_menu();}
liam94 6:18a4dd77057e 277
liam94 5:98845ccaaacd 278 ThisThread::sleep_for(200);
liam94 5:98845ccaaacd 279 }
liam94 5:98845ccaaacd 280 }
liam94 8:7e48229d678c 281
liam94 8:7e48229d678c 282 /***************************************************************************//**
liam94 8:7e48229d678c 283 *
liam94 8:7e48229d678c 284 * this function is for calibrating the distance in which the object centre point
liam94 8:7e48229d678c 285 * should be away from the sensor, it checks the current distance and then though
liam94 8:7e48229d678c 286 * the function "void dist" displays the current distance on the screen.
liam94 8:7e48229d678c 287 * this function also tells the user the distance away from the target centre
liam94 8:7e48229d678c 288 * point this should be, this is so that the board is the right distance away and
liam94 8:7e48229d678c 289 * will be able to detect the object better.
liam94 8:7e48229d678c 290 *
liam94 8:7e48229d678c 291 * in order to print the distance onto the LCD the current distance is printed to
liam94 8:7e48229d678c 292 * a buffer which is a max length of 14 (the amount of characters that can fit
liam94 8:7e48229d678c 293 * width ways on the screen) this can then be printed on the screen using print
liam94 8:7e48229d678c 294 * string.
liam94 8:7e48229d678c 295 *
liam94 8:7e48229d678c 296 *******************************************************************************/
liam94 8:7e48229d678c 297
liam94 6:18a4dd77057e 298 void calibrate_object(){
liam94 6:18a4dd77057e 299
liam94 15:d8ff594535fc 300 //sense = 0;
liam94 9:ada61082bbaa 301
liam94 6:18a4dd77057e 302 while(1)
liam94 6:18a4dd77057e 303 {
liam94 16:af15244242c2 304 Ultrasonic.checkDistance();
liam94 7:7464fbb0f3e1 305
liam94 6:18a4dd77057e 306 if (Back_flag == 1) {
liam94 6:18a4dd77057e 307 Back_flag = 0;
liam94 6:18a4dd77057e 308 main_menu();}
liam94 6:18a4dd77057e 309
liam94 6:18a4dd77057e 310 wait (1);
liam94 6:18a4dd77057e 311 }
liam94 6:18a4dd77057e 312 }
liam94 9:ada61082bbaa 313
liam94 7:7464fbb0f3e1 314 void dist(int distance)
liam94 7:7464fbb0f3e1 315 {
liam94 15:d8ff594535fc 316 //printf("sense = %d\n", sense);
liam94 8:7e48229d678c 317 // printf("Distance changed to %dmm\r\n", distance);
liam94 9:ada61082bbaa 318
liam94 9:ada61082bbaa 319 lcd.clear();
liam94 7:7464fbb0f3e1 320
liam94 9:ada61082bbaa 321 char buffer[14];
liam94 9:ada61082bbaa 322 int length = sprintf(buffer,"%dmm", distance);
liam94 9:ada61082bbaa 323 if (length <= 14)
liam94 7:7464fbb0f3e1 324
liam94 9:ada61082bbaa 325 lcd.printString("set object to",0,0);
liam94 9:ada61082bbaa 326 lcd.printString(" 200mm",0,1);
liam94 9:ada61082bbaa 327 lcd.printString(buffer,24,3);
liam94 9:ada61082bbaa 328 lcd.refresh();
liam94 10:be53044119d1 329
liam94 7:7464fbb0f3e1 330 }
liam94 8:7e48229d678c 331
liam94 8:7e48229d678c 332 /***************************************************************************//**
liam94 8:7e48229d678c 333 *
liam94 17:02676e9bbc73 334 * these functions are called when the start button is pressed oin the sense object
liam94 17:02676e9bbc73 335 * function, the purpose of these is to store the measured values for comparison
liam94 17:02676e9bbc73 336 * and to make a buzzer sound to indicate to the user that the value has been
liam94 17:02676e9bbc73 337 * measured
liam94 10:be53044119d1 338 *
liam94 10:be53044119d1 339 *******************************************************************************/
liam94 10:be53044119d1 340
liam94 10:be53044119d1 341 void object_sense1(){
liam94 10:be53044119d1 342
liam94 15:d8ff594535fc 343 //sense = 1;
liam94 10:be53044119d1 344
liam94 16:af15244242c2 345 Distance1 = Ultrasonic.getCurrentDistance();
liam94 13:cc99df342c7f 346
liam94 14:837945ccd8c0 347 buzzer.beep(1000,2);
liam94 14:837945ccd8c0 348 wait(0.5);
liam94 14:837945ccd8c0 349 buzzer.beep(0,0);
liam94 14:837945ccd8c0 350
liam94 17:02676e9bbc73 351 //printf("distance at sense 1 = %d\r\n", Distance1);
liam94 10:be53044119d1 352
liam94 10:be53044119d1 353 }
liam94 10:be53044119d1 354
liam94 10:be53044119d1 355 void object_sense2(){
liam94 10:be53044119d1 356
liam94 15:d8ff594535fc 357 //sense = 2;
liam94 10:be53044119d1 358
liam94 16:af15244242c2 359 Distance2 = Ultrasonic.getCurrentDistance();
liam94 14:837945ccd8c0 360
liam94 14:837945ccd8c0 361 buzzer.beep(1000,2);
liam94 14:837945ccd8c0 362 wait(0.5);
liam94 14:837945ccd8c0 363 buzzer.beep(0,0);
liam94 10:be53044119d1 364
liam94 17:02676e9bbc73 365 //printf("distance at sense 2 = %d\r\n", Distance2);
liam94 10:be53044119d1 366
liam94 10:be53044119d1 367 }
liam94 10:be53044119d1 368
liam94 10:be53044119d1 369 void object_sense3(){
liam94 10:be53044119d1 370
liam94 15:d8ff594535fc 371 //sense = 3;
liam94 10:be53044119d1 372
liam94 16:af15244242c2 373 Distance3 = Ultrasonic.getCurrentDistance();
liam94 14:837945ccd8c0 374
liam94 14:837945ccd8c0 375 buzzer.beep(1000,2);
liam94 14:837945ccd8c0 376 wait(0.5);
liam94 14:837945ccd8c0 377 buzzer.beep(0,0);
liam94 10:be53044119d1 378
liam94 17:02676e9bbc73 379 //printf("distance at sense 3 = %d\r\n", Distance3);
liam94 10:be53044119d1 380
liam94 10:be53044119d1 381 }
liam94 10:be53044119d1 382
liam94 10:be53044119d1 383 void object_sense4(){
liam94 10:be53044119d1 384
liam94 15:d8ff594535fc 385 //sense = 4;
liam94 10:be53044119d1 386
liam94 16:af15244242c2 387 Distance4 = Ultrasonic.getCurrentDistance();
liam94 14:837945ccd8c0 388
liam94 14:837945ccd8c0 389 buzzer.beep(1000,2);
liam94 14:837945ccd8c0 390 wait(0.5);
liam94 14:837945ccd8c0 391 buzzer.beep(0,0);
liam94 10:be53044119d1 392
liam94 17:02676e9bbc73 393 //printf("distance at sense 4 = %d\r\n", Distance4);
liam94 10:be53044119d1 394
liam94 10:be53044119d1 395 }
liam94 10:be53044119d1 396
liam94 10:be53044119d1 397 void object_sense5(){
liam94 10:be53044119d1 398
liam94 15:d8ff594535fc 399 //sense = 5;
liam94 10:be53044119d1 400
liam94 16:af15244242c2 401 Distance5 = Ultrasonic.getCurrentDistance();
liam94 10:be53044119d1 402
liam94 14:837945ccd8c0 403 buzzer.beep(1000,2);
liam94 14:837945ccd8c0 404 wait(0.5);
liam94 14:837945ccd8c0 405 buzzer.beep(0,0);
liam94 14:837945ccd8c0 406
liam94 17:02676e9bbc73 407 //printf("distance at sense 5 = %d\r\n", Distance5);
liam94 10:be53044119d1 408
liam94 10:be53044119d1 409 }
liam94 10:be53044119d1 410
liam94 10:be53044119d1 411 void object_sense6(){
liam94 10:be53044119d1 412
liam94 15:d8ff594535fc 413 //sense = 6;
liam94 10:be53044119d1 414
liam94 16:af15244242c2 415 Distance6 = Ultrasonic.getCurrentDistance();
liam94 10:be53044119d1 416
liam94 14:837945ccd8c0 417 buzzer.beep(1000,2);
liam94 14:837945ccd8c0 418 wait(0.5);
liam94 14:837945ccd8c0 419 buzzer.beep(0,0);
liam94 14:837945ccd8c0 420
liam94 17:02676e9bbc73 421 //printf("distance at sense 6 = %d\r\n", Distance6);
liam94 10:be53044119d1 422
liam94 10:be53044119d1 423 }
liam94 17:02676e9bbc73 424
liam94 17:02676e9bbc73 425 /***************************************************************************//**
liam94 17:02676e9bbc73 426 *
liam94 17:02676e9bbc73 427 * the object detection function is where the comparison between the measured
liam94 17:02676e9bbc73 428 * values and the defined object arrays occurs, if the measured values match the
liam94 17:02676e9bbc73 429 * arrays then the program will see it as an object found and will display this on
liam94 17:02676e9bbc73 430 * the screen, otherwise it will ask the user to try again. a +/- 5mm deadband has
liam94 17:02676e9bbc73 431 * been added to allow for minor erros in the measured values.
liam94 17:02676e9bbc73 432 *
liam94 17:02676e9bbc73 433 *******************************************************************************/
liam94 17:02676e9bbc73 434
liam94 10:be53044119d1 435 void object_detection(){
liam94 10:be53044119d1 436
liam94 10:be53044119d1 437 lcd.clear();
liam94 10:be53044119d1 438
liam94 10:be53044119d1 439 //printf("Distance 1 = %d\n", Distance1);
liam94 10:be53044119d1 440 //printf("glass[0] = %d\n", glass[0]);
liam94 10:be53044119d1 441 //printf("glass[6] = %d\n", glass[6]);
liam94 16:af15244242c2 442 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){
liam94 17:02676e9bbc73 443 //printf("glass\n");
liam94 13:cc99df342c7f 444 lcd.printString(" Object Found!",0,1);
liam94 13:cc99df342c7f 445 lcd.printString(" Pint Glass",0,3);
liam94 10:be53044119d1 446 lcd.refresh();}
liam94 16:af15244242c2 447
liam94 16:af15244242c2 448 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){
liam94 17:02676e9bbc73 449 //printf("Firestick Box\n");
liam94 16:af15244242c2 450 lcd.printString(" Object Found!",0,1);
liam94 16:af15244242c2 451 lcd.printString(" Firestick Box",0,3);
liam94 16:af15244242c2 452 lcd.refresh();}
liam94 16:af15244242c2 453
liam94 16:af15244242c2 454 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){
liam94 17:02676e9bbc73 455 //printf("Disaronno Bottle\n");
liam94 16:af15244242c2 456 lcd.printString(" Object Found!",0,1);
liam94 17:02676e9bbc73 457 lcd.printString(" Disaronno!",0,3);
liam94 16:af15244242c2 458 lcd.refresh();}
liam94 10:be53044119d1 459
liam94 10:be53044119d1 460 else{
liam94 17:02676e9bbc73 461 //printf("nothing\n");
liam94 13:cc99df342c7f 462 lcd.printString(" No Object!",0,1);
liam94 13:cc99df342c7f 463 lcd.printString(" Please Try",0,3);
liam94 10:be53044119d1 464 lcd.printString("Again",24,4);
liam94 10:be53044119d1 465 lcd.refresh();}
liam94 10:be53044119d1 466 }
liam94 10:be53044119d1 467
liam94 10:be53044119d1 468 /***************************************************************************//**
liam94 10:be53044119d1 469 *
liam94 8:7e48229d678c 470 * these functions are for setting the flags for the start and back butons which
liam94 8:7e48229d678c 471 * are used in the other functions.
liam94 8:7e48229d678c 472 *
liam94 8:7e48229d678c 473 *******************************************************************************/
liam94 8:7e48229d678c 474
liam94 5:98845ccaaacd 475 void Start_isr()
liam94 5:98845ccaaacd 476 {
liam94 8:7e48229d678c 477 // printf("start button pressed");
liam94 5:98845ccaaacd 478 Start_flag = 1;
liam94 2:3ace5b4ae9a7 479 }
liam94 2:3ace5b4ae9a7 480
liam94 5:98845ccaaacd 481 void Back_isr()
liam94 5:98845ccaaacd 482 {
liam94 8:7e48229d678c 483 // printf("back button pressed");
liam94 5:98845ccaaacd 484 Back_flag = 1;
liam94 1:a1795335ef8c 485 }
liam94 5:98845ccaaacd 486