![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
First Draft, serial print change based on distance
main.cpp
- Committer:
- liam94
- Date:
- 2022-01-26
- Revision:
- 7:7464fbb0f3e1
- Parent:
- 6:18a4dd77057e
- Child:
- 8:7e48229d678c
File content as of revision 7:7464fbb0f3e1:
#include "mbed.h" #include "ultrasonic.h" #include "N5110.h" #include "Joystick.h" #include "main.h" int main() { R.mode(PullDown); L.mode(PullDown); Start.mode(PullDown); Start.rise(Start_isr); Back.mode(PullDown); Back.rise(Back_isr); mu.startUpdates();//start mesuring the distance joystick.init(); init_display(); main_menu(); } void init_display(){ lcd.init(); lcd.setContrast(0.4); lcd.clear(); /// clear screen (init) } void main_menu(){ lcd.clear(); Start_flag = 0; int select = 0; output = 63; //set all LEDs to off while (1) { Direction d = joystick.get_direction(); printf("Direction = %i\n",d); switch(select) { case 0: switch(d) { case 1: select = 1; printf("UP"); break; case 5: select = 1; printf("Down"); break; } break; case 1: switch(d) { case 1: select = 0; printf("UP"); break; case 5: select = 0; printf("Down"); break; } break; } wait(0.1); if (select == 0) { 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) { 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(); } } } } void sense_object(){ // set inital state int state = 0; while(1) { output = fsm[state]; // output current 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); } } void calibrate_object(){ while(1) { 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();} wait (1); } } void dist(int 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) lcd.printString("set object to xxx",0,0); lcd.printString(buffer,0,3); // display on screen lcd.refresh(); } void Start_isr() { Start_flag = 1; } void Back_isr() { Back_flag = 1; }