![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
First Draft, serial print change based on distance
main.cpp
- Committer:
- liam94
- Date:
- 2022-01-28
- Revision:
- 8:7e48229d678c
- Parent:
- 7:7464fbb0f3e1
- Child:
- 9:ada61082bbaa
File content as of revision 8:7e48229d678c:
#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); L.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; output = 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) { output = 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(" L - 60'",0,4); lcd.refresh(); if (R == 1){ state = 1;} else if (L == 1){ state = 5;} else state = 0; break; case 1: lcd.clear(); lcd.printString(" object at 60'",0,0); lcd.printString(" R + 60'",0,2); lcd.printString(" L - 60'",0,4); lcd.refresh(); if (R == 1){ state = 2;} else if (L == 1){ state = 0;} else state = 1; break; case 2: lcd.clear(); lcd.printString(" object at 120'",0,0); lcd.printString(" R + 60'",0,2); lcd.printString(" L - 60'",0,4); lcd.refresh(); if (R == 1){ state = 3;} else if (L == 1){ state = 1;} else state = 2; break; case 3: lcd.clear(); lcd.printString(" object at 180'",0,0); lcd.printString(" R + 60'",0,2); lcd.printString(" L - 60'",0,4); lcd.refresh(); if (R == 1){ state = 4;} else if (L == 1){ state = 2;} else state = 3; break; case 4: lcd.clear(); lcd.printString(" object at 240'",0,0); lcd.printString(" R + 60'",0,2); lcd.printString(" L - 60'",0,4); lcd.refresh(); if (R == 1){ state = 5;} else if (L == 1){ state = 3;} else state = 4; break; case 5: lcd.clear(); lcd.printString(" object at 300'",0,0); lcd.printString(" R + 60'",0,2); lcd.printString(" L - 60'",0,4); lcd.refresh(); if (R == 1){ state = 0;} else if (L == 1){ state = 4;} else state = 5; 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(){ while(1) { mu.checkDistance(); if (Back_flag == 1) { Back_flag = 0; main_menu();} wait (1); } } void dist(int distance) { // 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(" 1000mm",0,1); lcd.printString( buffer,0,3); 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; }