First Draft, serial print change based on distance

Committer:
liam94
Date:
Sat Jan 22 15:28:18 2022 +0000
Revision:
4:77500a7f951d
Parent:
3:0b0fbddb6f51
Child:
5:98845ccaaacd
added LCD text strings

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 3:0b0fbddb6f51 4 // added R and L buttons presses in so that the user has to press when they are ready to rotate object and in which direction
liam94 3:0b0fbddb6f51 5 DigitalIn L(PTB18);
liam94 3:0b0fbddb6f51 6 DigitalIn R(PTB3);
liam94 0:506531d0531c 7
liam94 2:3ace5b4ae9a7 8 // LEDs on PCB to be connected (1 to 6) - active-low 0 = on and 1 = off
liam94 2:3ace5b4ae9a7 9 BusOut output(PTA1,PTA2,PTC2,PTC3,PTC4,PTD3);
liam94 2:3ace5b4ae9a7 10
liam94 4:77500a7f951d 11 // VCC,SCE,RST,D/C,MOSI,SCLK,LED
liam94 4:77500a7f951d 12 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); // K64F - pwr from 3V3
liam94 4:77500a7f951d 13
liam94 2:3ace5b4ae9a7 14 // array of states in the FSM, each element is the output of the counter
liam94 2:3ace5b4ae9a7 15 // set the output in binary to make it easier, 0 is LED on, 1 is LED off
liam94 2:3ace5b4ae9a7 16 int fsm[6] = {0b111110, 0b111101, 0b111011, 0b110111, 0b101111, 0b011111};
liam94 2:3ace5b4ae9a7 17
liam94 2:3ace5b4ae9a7 18 void dist(int distance);
liam94 2:3ace5b4ae9a7 19
liam94 2:3ace5b4ae9a7 20
liam94 2:3ace5b4ae9a7 21 ultrasonic mu(PTD0, PTC12, .5, 1, &dist); //Set the trigger pin to PTD0 and the echo pin to PTC12
liam94 2:3ace5b4ae9a7 22 //have updates every .5 seconds and a timeout after 1
liam94 2:3ace5b4ae9a7 23 //second, and call dist when the distance changes
liam94 2:3ace5b4ae9a7 24
liam94 2:3ace5b4ae9a7 25 int main()
liam94 1:a1795335ef8c 26 {
liam94 2:3ace5b4ae9a7 27 // set inital state
liam94 2:3ace5b4ae9a7 28 int state = 0;
liam94 3:0b0fbddb6f51 29 R.mode(PullDown);
liam94 3:0b0fbddb6f51 30 L.mode(PullDown);
liam94 4:77500a7f951d 31 lcd.init();
liam94 4:77500a7f951d 32 lcd.setContrast(0.4);
liam94 2:3ace5b4ae9a7 33 mu.startUpdates();//start mesuring the distance
liam94 2:3ace5b4ae9a7 34 while(1)
liam94 2:3ace5b4ae9a7 35 {
liam94 4:77500a7f951d 36 output = fsm[state]; // output current state
liam94 4:77500a7f951d 37 lcd.clear(); // clear buffer at start of every loop
liam94 4:77500a7f951d 38 // can directly print strings at specified co-ordinates (must be less than 84 pixels to fit on display)
liam94 4:77500a7f951d 39
liam94 4:77500a7f951d 40 // check which state we are in and see which the next state should be
liam94 4:77500a7f951d 41 switch(state) {
liam94 4:77500a7f951d 42 case 0:
liam94 4:77500a7f951d 43 lcd.clear();
liam94 4:77500a7f951d 44 lcd.printString(" object at 0'",0,0);
liam94 4:77500a7f951d 45 lcd.printString(" R + 60'",0,2);
liam94 4:77500a7f951d 46 lcd.printString(" L - 60'",0,4);
liam94 4:77500a7f951d 47 lcd.refresh();
liam94 3:0b0fbddb6f51 48 if (R == 1){
liam94 3:0b0fbddb6f51 49 state = 1;}
liam94 3:0b0fbddb6f51 50 else if (L == 1){
liam94 3:0b0fbddb6f51 51 state = 5;}
liam94 3:0b0fbddb6f51 52 else state = 0;
liam94 2:3ace5b4ae9a7 53 break;
liam94 4:77500a7f951d 54 case 1:
liam94 4:77500a7f951d 55 lcd.clear();
liam94 4:77500a7f951d 56 lcd.printString(" object at 60'",0,0);
liam94 4:77500a7f951d 57 lcd.printString(" R + 60'",0,2);
liam94 4:77500a7f951d 58 lcd.printString(" L - 60'",0,4);
liam94 4:77500a7f951d 59 lcd.refresh();
liam94 3:0b0fbddb6f51 60 if (R == 1){
liam94 3:0b0fbddb6f51 61 state = 2;}
liam94 3:0b0fbddb6f51 62 else if (L == 1){
liam94 3:0b0fbddb6f51 63 state = 0;}
liam94 3:0b0fbddb6f51 64 else state = 1;
liam94 2:3ace5b4ae9a7 65 break;
liam94 4:77500a7f951d 66 case 2:
liam94 4:77500a7f951d 67 lcd.clear();
liam94 4:77500a7f951d 68 lcd.printString(" object at 120'",0,0);
liam94 4:77500a7f951d 69 lcd.printString(" R + 60'",0,2);
liam94 4:77500a7f951d 70 lcd.printString(" L - 60'",0,4);
liam94 4:77500a7f951d 71 lcd.refresh();
liam94 3:0b0fbddb6f51 72 if (R == 1){
liam94 3:0b0fbddb6f51 73 state = 3;}
liam94 3:0b0fbddb6f51 74 else if (L == 1){
liam94 3:0b0fbddb6f51 75 state = 1;}
liam94 3:0b0fbddb6f51 76 else state = 2;
liam94 2:3ace5b4ae9a7 77 break;
liam94 4:77500a7f951d 78 case 3:
liam94 4:77500a7f951d 79 lcd.clear();
liam94 4:77500a7f951d 80 lcd.printString(" object at 180'",0,0);
liam94 4:77500a7f951d 81 lcd.printString(" R + 60'",0,2);
liam94 4:77500a7f951d 82 lcd.printString(" L - 60'",0,4);
liam94 4:77500a7f951d 83 lcd.refresh();
liam94 3:0b0fbddb6f51 84 if (R == 1){
liam94 3:0b0fbddb6f51 85 state = 4;}
liam94 3:0b0fbddb6f51 86 else if (L == 1){
liam94 3:0b0fbddb6f51 87 state = 2;}
liam94 3:0b0fbddb6f51 88 else state = 3;
liam94 2:3ace5b4ae9a7 89 break;
liam94 4:77500a7f951d 90 case 4:
liam94 4:77500a7f951d 91 lcd.clear();
liam94 4:77500a7f951d 92 lcd.printString(" object at 240'",0,0);
liam94 4:77500a7f951d 93 lcd.printString(" R + 60'",0,2);
liam94 4:77500a7f951d 94 lcd.printString(" L - 60'",0,4);
liam94 4:77500a7f951d 95 lcd.refresh();
liam94 3:0b0fbddb6f51 96 if (R == 1){
liam94 3:0b0fbddb6f51 97 state = 5;}
liam94 3:0b0fbddb6f51 98 else if (L == 1){
liam94 3:0b0fbddb6f51 99 state = 3;}
liam94 3:0b0fbddb6f51 100 else state = 4;
liam94 2:3ace5b4ae9a7 101 break;
liam94 4:77500a7f951d 102 case 5:
liam94 4:77500a7f951d 103 lcd.clear();
liam94 4:77500a7f951d 104 lcd.printString(" object at 300'",0,0);
liam94 4:77500a7f951d 105 lcd.printString(" R + 60'",0,2);
liam94 4:77500a7f951d 106 lcd.printString(" L - 60'",0,4);
liam94 4:77500a7f951d 107 lcd.refresh();
liam94 3:0b0fbddb6f51 108 if (R == 1){
liam94 3:0b0fbddb6f51 109 state = 0;}
liam94 3:0b0fbddb6f51 110 else if (L == 1){
liam94 3:0b0fbddb6f51 111 state = 4;}
liam94 3:0b0fbddb6f51 112 else state = 5;
liam94 2:3ace5b4ae9a7 113 break;
liam94 2:3ace5b4ae9a7 114 default:
liam94 2:3ace5b4ae9a7 115 error("Invalid state"); //invalid state - call error routine
liam94 2:3ace5b4ae9a7 116 // or could jump to starting state i.e. state = 0
liam94 2:3ace5b4ae9a7 117 break;
liam94 2:3ace5b4ae9a7 118 }
liam94 4:77500a7f951d 119 ThisThread::sleep_for(100);
liam94 2:3ace5b4ae9a7 120
liam94 2:3ace5b4ae9a7 121 mu.checkDistance(); //call checkDistance() as much as possible, as this is where
liam94 2:3ace5b4ae9a7 122 //the class checks if dist needs to be called.
liam94 2:3ace5b4ae9a7 123 }
liam94 2:3ace5b4ae9a7 124 }
liam94 2:3ace5b4ae9a7 125
liam94 2:3ace5b4ae9a7 126 void dist(int distance){
liam94 2:3ace5b4ae9a7 127
liam94 1:a1795335ef8c 128 if (distance > 250){
liam94 1:a1795335ef8c 129 printf("Safe Distance %dmm\r\n", distance);
liam94 1:a1795335ef8c 130 }
liam94 2:3ace5b4ae9a7 131 }