![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
First Draft, serial print change based on distance
main.cpp
- Committer:
- liam94
- Date:
- 2022-02-03
- Revision:
- 13:cc99df342c7f
- Parent:
- 10:be53044119d1
- Child:
- 14:837945ccd8c0
File content as of revision 13:cc99df342c7f:
#include "mbed.h" #include "ultrasonic.h" #include "N5110.h" #include "Joystick.h" #include "main.h" /***************************************************************************//** * * the main code below sets all the interrupts to pull down and also for the start * and back buttons it is sent to the specific start and back functions which sets * the flags which are then used in other functions. * * the mu.startUpdates() is where the ultrasonic sensor is told to start measuring * the distances. * the joystick and lcd are then initialised and the man menu function is then * called. * *******************************************************************************/ int main() { R.mode(PullDown); Start.mode(PullDown); Start.rise(Start_isr); Back.mode(PullDown); Back.rise(Back_isr); // printf("set buttons"); mu.startUpdates(); // printf ("Ultrasonic Updates started"); joystick.init(); // printf("initialise joystick") init_display(); main_menu(); } /***************************************************************************//** * * added function which initalise the display and sets the contrast, this is * called from the int main and so the LCD boots up on startup. * *******************************************************************************/ void init_display(){ // printf("initialise display"); lcd.init(); lcd.setContrast(0.4); lcd.clear(); } /***************************************************************************//** * * this function is the main menu for the program, it is called at the start and * depending on what string is selected, clicking the start uton will send the * user to the specified function. * *******************************************************************************/ void main_menu(){ // printf("main menu"); lcd.clear(); Start_flag = 0; int select = 0; LEDS = 63; // 63 on the bus output sets all LEDs to off while (1) { Direction d = joystick.get_direction(); // printf("Direction = %i\n",d); switch(select) { case 0: switch(d) { case N: select = 1; // printf("UP"); break; case S: select = 1; // printf("Down"); break; } break; case 1: switch(d) { case N: select = 0; // printf("UP"); break; case S: select = 0; // printf("Down"); break; } break; } wait(0.1); if (select == 0) { // printf("main menu - sense object"); lcd.clear(); lcd.printString("Detect",27,0); lcd.printString("Sense Object",7,2); lcd.printString("Calibration",7,3); lcd.drawCircle(3,19,2,FILL_TRANSPARENT); lcd.refresh(); wait(0.15); if (Start_flag == 1) { Start_flag = 0; sense_object(); } } else if (select == 1) { // printf("main menu - calibration"); lcd.clear(); lcd.printString("Detect",27,0); lcd.printString("Sense Object", 7,2); lcd.printString("Calibration",7,3); lcd.drawCircle(3,27,2,FILL_TRANSPARENT); lcd.refresh(); wait(0.15); if (Start_flag == 1) { Start_flag = 0; calibrate_object(); } } } } /***************************************************************************//** * * this function is for sensing the object * *******************************************************************************/ void sense_object(){ // printf("sense object"); // set inital state int state = 0; while(1) { LEDS = fsm[state]; // output current state printf("state = %d\r\n",state); // check which state we are in and see which the next state should be switch(state) { case 0:{ lcd.clear(); lcd.printString(" object at 0'",0,0); lcd.printString(" R + 60'",0,2); lcd.printString("Start to take",0,4); lcd.printString(" Reading ",0,5); lcd.refresh(); if (R == 1){ state = 1;} else state = 0; if (Start_flag == 1) { Start_flag = 0; object_sense1(); } break;} case 1:{ lcd.clear(); lcd.printString(" object at 60'",0,0); lcd.printString(" R + 60'",0,2); lcd.printString("Start to take",0,4); lcd.printString(" Reading ",0,5); lcd.refresh(); if (R == 1){ state = 2;} else state = 1; if (Start_flag == 1) { Start_flag = 0; object_sense2(); } break;} case 2:{ lcd.clear(); lcd.printString(" object at 120'",0,0); lcd.printString(" R + 60'",0,2); lcd.printString("Start to take",0,4); lcd.printString(" Reading ",0,5); lcd.refresh(); if (R == 1){ state = 3;} else state = 2; if (Start_flag == 1) { Start_flag = 0; object_sense3(); } break;} case 3:{ lcd.clear(); lcd.printString(" object at 180'",0,0); lcd.printString(" R + 60'",0,2); lcd.printString("Start to take",0,4); lcd.printString(" Reading ",0,5); lcd.refresh(); if (R == 1){ state = 4;} else state = 3; if (Start_flag == 1) { Start_flag = 0; object_sense4(); } break;} case 4:{ lcd.clear(); lcd.printString(" object at 240'",0,0); lcd.printString(" R + 60'",0,2); lcd.printString("Start to take",0,4); lcd.printString(" Reading ",0,5); lcd.refresh(); if (R == 1){ state = 5;} else state = 4; if (Start_flag == 1) { Start_flag = 0; object_sense5(); } break;} case 5:{ lcd.clear(); lcd.printString(" object at 300'",0,0); lcd.printString(" R + 60'",0,2); lcd.printString("Start to take",0,4); lcd.printString(" Reading ",0,5); lcd.refresh(); if (R == 1){ object_detection();} else state = 5; if (Start_flag == 1) { Start_flag = 0; object_sense6(); } break;} default:{ error("Invalid state"); //invalid state - call error routine // or could jump to starting state i.e. state = 0 break;} } if (Back_flag == 1) { Back_flag = 0; main_menu();} ThisThread::sleep_for(200); } } /***************************************************************************//** * * this function is for calibrating the distance in which the object centre point * should be away from the sensor, it checks the current distance and then though * the function "void dist" displays the current distance on the screen. * this function also tells the user the distance away from the target centre * point this should be, this is so that the board is the right distance away and * will be able to detect the object better. * * in order to print the distance onto the LCD the current distance is printed to * a buffer which is a max length of 14 (the amount of characters that can fit * width ways on the screen) this can then be printed on the screen using print * string. * *******************************************************************************/ void calibrate_object(){ sense = 0; while(1) { mu.checkDistance(); if (Back_flag == 1) { Back_flag = 0; main_menu();} wait (1); } } void dist(int distance) { printf("sense = %d\n", sense); // printf("Distance changed to %dmm\r\n", distance); lcd.clear(); char buffer[14]; int length = sprintf(buffer,"%dmm", distance); if (length <= 14) lcd.printString("set object to",0,0); lcd.printString(" 200mm",0,1); lcd.printString(buffer,24,3); lcd.refresh(); } /***************************************************************************//** * * this is where the magic happens * *******************************************************************************/ void object_sense1(){ sense = 1; Distance1 = mu.getCurrentDistance(); printf("distance at sense 1 = %d\r\n", Distance1); } void object_sense2(){ sense = 2; Distance2 = mu.getCurrentDistance(); printf("distance at sense 2 = %d\r\n", Distance2); } void object_sense3(){ sense = 3; Distance3 = mu.getCurrentDistance(); printf("distance at sense 3 = %d\r\n", Distance3); } void object_sense4(){ sense = 4; Distance4 = mu.getCurrentDistance(); printf("distance at sense 4 = %d\r\n", Distance4); } void object_sense5(){ sense = 5; Distance5 = mu.getCurrentDistance(); printf("distance at sense 5 = %d\r\n", Distance5); } void object_sense6(){ sense = 6; Distance6 = mu.getCurrentDistance(); printf("distance at sense 6 = %d\r\n", Distance6); } void object_detection(){ lcd.clear(); //printf("Distance 1 = %d\n", Distance1); //printf("glass[0] = %d\n", glass[0]); //printf("glass[6] = %d\n", glass[6]); if (abs(Distance1 - glass[0]) <= 10 and abs(Distance2 - glass[1]) <= 10 and abs(Distance3 - glass[2]) <= 10 and abs(Distance4 - glass[3]) <= 10 and abs(Distance5 - glass[4]) <= 10 and abs(Distance6 - glass[5]) <= 10){ printf("glass\n"); lcd.printString(" Object Found!",0,1); lcd.printString(" Pint Glass",0,3); lcd.refresh();} else{ printf("nothing\n"); lcd.printString(" No Object!",0,1); lcd.printString(" Please Try",0,3); lcd.printString("Again",24,4); lcd.refresh();} } /***************************************************************************//** * * these functions are for setting the flags for the start and back butons which * are used in the other functions. * *******************************************************************************/ void Start_isr() { // printf("start button pressed"); Start_flag = 1; } void Back_isr() { // printf("back button pressed"); Back_flag = 1; }