![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
First Draft, serial print change based on distance
Diff: main.cpp
- Revision:
- 8:7e48229d678c
- Parent:
- 7:7464fbb0f3e1
- Child:
- 9:ada61082bbaa
diff -r 7464fbb0f3e1 -r 7e48229d678c main.cpp --- a/main.cpp Wed Jan 26 23:08:24 2022 +0000 +++ b/main.cpp Fri Jan 28 13:01:19 2022 +0000 @@ -4,6 +4,18 @@ #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() { @@ -14,57 +26,79 @@ Start.rise(Start_isr); Back.mode(PullDown); Back.rise(Back_isr); - mu.startUpdates();//start mesuring the distance + // 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(); /// clear screen (init) + 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; //set all LEDs to off + output = 63; // 63 on the bus output sets all LEDs to off while (1) { Direction d = joystick.get_direction(); - printf("Direction = %i\n",d); + // printf("Direction = %i\n",d); switch(select) { case 0: switch(d) { - case 1: + case N: select = 1; - printf("UP"); + // printf("UP"); break; - case 5: + case S: select = 1; - printf("Down"); + // printf("Down"); break; } break; case 1: switch(d) { - case 1: + case N: select = 0; - printf("UP"); + // printf("UP"); break; - case 5: + case S: select = 0; - printf("Down"); + // printf("Down"); break; } break; @@ -72,6 +106,7 @@ } wait(0.1); if (select == 0) { + // printf("main menu - sense object"); lcd.clear(); lcd.printString("Detect",27,0); lcd.printString("Sense Object",7,2); @@ -84,6 +119,7 @@ sense_object(); } } else if (select == 1) { + // printf("main menu - calibration"); lcd.clear(); lcd.printString("Detect",27,0); lcd.printString("Sense Object", 7,2); @@ -99,8 +135,15 @@ } } +/***************************************************************************//** +* +* this function is for sensing the object +* +*******************************************************************************/ + void sense_object(){ - + + // printf("sense object"); // set inital state int state = 0; @@ -108,7 +151,9 @@ while(1) { output = fsm[state]; // output current state - // check which state we are in and see which the next state should be + // 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(); @@ -194,15 +239,29 @@ 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(); - mu.checkDistance(); //call checkDistance() as much as possible, as this is where - //the class checks if dist needs to be called. - if (Back_flag == 1) { Back_flag = 0; main_menu();} @@ -214,29 +273,36 @@ void dist(int distance) { - printf("Distance changed to %dmm\r\n", distance); + // printf("Distance changed to %dmm\r\n", distance); lcd.clear(); - char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) - // so can display a string of a maximum 14 characters in length - // or create formatted strings - ensure they aren't more than 14 characters long - int length = sprintf(buffer,"%dmm", distance); // print formatted data to buffer - // it is important the format specifier ensures the length will fit in the buffer - if (length <= 14) // if string will fit on display (assuming printing at x=0) + char buffer[14]; + int length = sprintf(buffer,"%dmm", distance); + if (length <= 14) - lcd.printString("set object to xxx",0,0); - lcd.printString(buffer,0,3); // display on screen - lcd.refresh(); + 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; } \ No newline at end of file