![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Coursework
main.cpp@24:a88c4941a25e, 2022-01-21 (annotated)
- Committer:
- sesa514652
- Date:
- Fri Jan 21 22:00:58 2022 +0000
- Revision:
- 24:a88c4941a25e
- Parent:
- 23:f72175fb49ed
- Child:
- 25:9798017b837d
Checking if statement for scans, problem with data which is input .. try with scan
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sesa514652 | 0:1f799c7cce2b | 1 | #include "mbed.h" |
sesa514652 | 0:1f799c7cce2b | 2 | #include "Joystick.h" |
sesa514652 | 0:1f799c7cce2b | 3 | #include "N5110.h" |
sesa514652 | 0:1f799c7cce2b | 4 | #include "hcsr04.h" |
sesa514652 | 3:6ecb75a2675c | 5 | #include "Piezo.h" |
sesa514652 | 8:770d168713cc | 6 | #include "string" |
sesa514652 | 20:4e1f2917a857 | 7 | #include "array" |
sesa514652 | 22:82b01fdfc61f | 8 | #include <iostream> |
sesa514652 | 12:ceea378d5691 | 9 | // FSM Menu |
sesa514652 | 12:ceea378d5691 | 10 | // defines directions as 0/1. Note UPPERCASE |
sesa514652 | 12:ceea378d5691 | 11 | #define UP 0 |
sesa514652 | 12:ceea378d5691 | 12 | #define DOWN 1 |
sesa514652 | 12:ceea378d5691 | 13 | // set inital state |
sesa514652 | 12:ceea378d5691 | 14 | volatile int state = 0; |
sesa514652 | 12:ceea378d5691 | 15 | // set initial direction |
sesa514652 | 12:ceea378d5691 | 16 | int direction = UP; |
sesa514652 | 12:ceea378d5691 | 17 | // array of states in the FSM, each element is the output of the counter |
sesa514652 | 12:ceea378d5691 | 18 | // set the output in binary to make it easier, 0 is LED on, 1 is LED off |
sesa514652 | 12:ceea378d5691 | 19 | string fsm[4] = {"buttonA_isr","buttonB_isr","buttonAsss_isr","buttonA_isr"}; |
sesa514652 | 18:bb36d96142dd | 20 | volatile int PressA; |
sesa514652 | 18:bb36d96142dd | 21 | volatile int PressB; |
sesa514652 | 10:42e70b596099 | 22 | |
sesa514652 | 5:9b2c976ca318 | 23 | // y x button |
sesa514652 | 0:1f799c7cce2b | 24 | Joystick joystick(PTB10,PTB11,PTC16); |
sesa514652 | 0:1f799c7cce2b | 25 | HCSR04 sensor(D14, D15); |
sesa514652 | 3:6ecb75a2675c | 26 | Piezo Buzzer(PTC10); |
sesa514652 | 8:770d168713cc | 27 | //Bringing in buttons |
sesa514652 | 8:770d168713cc | 28 | InterruptIn buttonA(PTB9); |
sesa514652 | 8:770d168713cc | 29 | InterruptIn buttonB(PTD0); |
sesa514652 | 8:770d168713cc | 30 | InterruptIn buttonX(PTC17); |
sesa514652 | 8:770d168713cc | 31 | InterruptIn buttonY(PTC12); |
sesa514652 | 11:1dec05b7d1c1 | 32 | InterruptIn buttonStart(PTC5); |
sesa514652 | 11:1dec05b7d1c1 | 33 | InterruptIn buttonBack(PTB19); |
sesa514652 | 11:1dec05b7d1c1 | 34 | InterruptIn buttonLeft(PTB18); |
sesa514652 | 11:1dec05b7d1c1 | 35 | InterruptIn buttonRight(PTB3); |
sesa514652 | 0:1f799c7cce2b | 36 | |
sesa514652 | 11:1dec05b7d1c1 | 37 | // create an event queue |
sesa514652 | 11:1dec05b7d1c1 | 38 | EventQueue queue; // events are needed to get arround the printf issue |
sesa514652 | 5:9b2c976ca318 | 39 | //rows,cols |
sesa514652 | 0:1f799c7cce2b | 40 | int sprite[8][5] = { |
sesa514652 | 0:1f799c7cce2b | 41 | { 0,0,1,0,0 }, |
sesa514652 | 0:1f799c7cce2b | 42 | { 0,1,1,1,0 }, |
sesa514652 | 0:1f799c7cce2b | 43 | { 0,0,1,0,0 }, |
sesa514652 | 0:1f799c7cce2b | 44 | { 0,1,1,1,0 }, |
sesa514652 | 0:1f799c7cce2b | 45 | { 1,1,1,1,1 }, |
sesa514652 | 0:1f799c7cce2b | 46 | { 1,1,1,1,1 }, |
sesa514652 | 0:1f799c7cce2b | 47 | { 1,1,0,1,1 }, |
sesa514652 | 0:1f799c7cce2b | 48 | { 1,1,0,1,1 }, |
sesa514652 | 5:9b2c976ca318 | 49 | }; |
sesa514652 | 11:1dec05b7d1c1 | 50 | void init_K64F(); // Start K64fBoard |
sesa514652 | 11:1dec05b7d1c1 | 51 | |
sesa514652 | 11:1dec05b7d1c1 | 52 | void buttonA_isr(); // Button A interrupt service routine |
sesa514652 | 11:1dec05b7d1c1 | 53 | void buttonB_isr(); // Button B interrupt service routine |
sesa514652 | 11:1dec05b7d1c1 | 54 | void buttonX_isr(); // Button X interrupt service routine |
sesa514652 | 11:1dec05b7d1c1 | 55 | void buttonY_isr(); // Button Y interrupt service routine |
sesa514652 | 11:1dec05b7d1c1 | 56 | void buttonStart_isr(); // Button Start interrupt service routine |
sesa514652 | 11:1dec05b7d1c1 | 57 | void buttonBack_isr(); // Button Back interrupt service routine |
sesa514652 | 11:1dec05b7d1c1 | 58 | void buttonLeft_isr(); // Button Left interrupt service routine |
sesa514652 | 11:1dec05b7d1c1 | 59 | void buttonRight_isr(); // Button Right interrupt service routine |
sesa514652 | 11:1dec05b7d1c1 | 60 | volatile int g_buttonA_flag = 0; // Global Flag |
sesa514652 | 11:1dec05b7d1c1 | 61 | volatile int g_buttonB_flag = 0; // Global Flag |
sesa514652 | 11:1dec05b7d1c1 | 62 | volatile int g_buttonX_flag = 0; // Global Flag |
sesa514652 | 11:1dec05b7d1c1 | 63 | volatile int g_buttonY_flag = 0; // Global Flag |
sesa514652 | 11:1dec05b7d1c1 | 64 | volatile int g_buttonStart_flag = 0; // Global Flag |
sesa514652 | 11:1dec05b7d1c1 | 65 | volatile int g_buttonBack_flag = 0; // Global Flag |
sesa514652 | 11:1dec05b7d1c1 | 66 | volatile int g_buttonLeft_flag = 0; // Global Flag |
sesa514652 | 11:1dec05b7d1c1 | 67 | volatile int g_buttonRight_flag = 0; // Global Flag |
sesa514652 | 18:bb36d96142dd | 68 | int buttonCountA(); |
sesa514652 | 18:bb36d96142dd | 69 | int buttonCountB(); |
sesa514652 | 11:1dec05b7d1c1 | 70 | //volatile int g_buttonA_counter = 0; // Global counter |
sesa514652 | 8:770d168713cc | 71 | //Test function after research C++ lanuage 05/01/22 |
sesa514652 | 4:167ce930c9d5 | 72 | int cube(int num){ |
sesa514652 | 11:1dec05b7d1c1 | 73 | return num*num; |
sesa514652 | 5:9b2c976ca318 | 74 | }; |
sesa514652 | 10:42e70b596099 | 75 | // Menu Items |
sesa514652 | 10:42e70b596099 | 76 | int page1; |
sesa514652 | 10:42e70b596099 | 77 | int page2; |
sesa514652 | 10:42e70b596099 | 78 | int page3; |
sesa514652 | 10:42e70b596099 | 79 | string Listitem1 = "Game"; // guess distance |
sesa514652 | 10:42e70b596099 | 80 | string Listitem2 = "Measure"; // measure distance of object |
sesa514652 | 10:42e70b596099 | 81 | string Listitem3 = "Detect"; // alarm when object is dectected |
sesa514652 | 5:9b2c976ca318 | 82 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
sesa514652 | 9:cdd7d9a123f9 | 83 | /* Test of the interup an operating the buzzer this kills the board |
sesa514652 | 9:cdd7d9a123f9 | 84 | void buzzme(){ |
sesa514652 | 9:cdd7d9a123f9 | 85 | Buzzer.play(200,120); |
sesa514652 | 9:cdd7d9a123f9 | 86 | wait_ms(5); |
sesa514652 | 9:cdd7d9a123f9 | 87 | Buzzer.play(200,120);; |
sesa514652 | 9:cdd7d9a123f9 | 88 | } |
sesa514652 | 9:cdd7d9a123f9 | 89 | */ |
sesa514652 | 11:1dec05b7d1c1 | 90 | void printme(){ |
sesa514652 | 9:cdd7d9a123f9 | 91 | |
sesa514652 | 11:1dec05b7d1c1 | 92 | volatile double x ; |
sesa514652 | 11:1dec05b7d1c1 | 93 | x = 1; |
sesa514652 | 10:42e70b596099 | 94 | printf("%f\n",x); |
sesa514652 | 11:1dec05b7d1c1 | 95 | // return 0; |
sesa514652 | 9:cdd7d9a123f9 | 96 | } |
sesa514652 | 10:42e70b596099 | 97 | |
sesa514652 | 24:a88c4941a25e | 98 | int Scan1[10]; |
sesa514652 | 24:a88c4941a25e | 99 | int Scan2[10]; |
sesa514652 | 24:a88c4941a25e | 100 | int Scan1Input(){; |
sesa514652 | 23:f72175fb49ed | 101 | cin >> Scan1[0]; |
sesa514652 | 23:f72175fb49ed | 102 | cin >> Scan1[1]; |
sesa514652 | 23:f72175fb49ed | 103 | cin >> Scan1[2]; |
sesa514652 | 23:f72175fb49ed | 104 | cin >> Scan1[3]; |
sesa514652 | 23:f72175fb49ed | 105 | cin >> Scan1[4]; |
sesa514652 | 23:f72175fb49ed | 106 | cin >> Scan1[5]; |
sesa514652 | 23:f72175fb49ed | 107 | cin >> Scan1[6]; |
sesa514652 | 23:f72175fb49ed | 108 | cin >> Scan1[7]; |
sesa514652 | 24:a88c4941a25e | 109 | cin >> Scan1[8]; |
sesa514652 | 24:a88c4941a25e | 110 | cin >> Scan1[9]; |
sesa514652 | 23:f72175fb49ed | 111 | return Scan1[0,1,2,3,4,5,6,7,8,9,10];} |
sesa514652 | 10:42e70b596099 | 112 | |
sesa514652 | 10:42e70b596099 | 113 | //void returnme(){ |
sesa514652 | 10:42e70b596099 | 114 | //int num; |
sesa514652 | 10:42e70b596099 | 115 | // return num*num; |
sesa514652 | 10:42e70b596099 | 116 | // }; |
sesa514652 | 0:1f799c7cce2b | 117 | int main() { |
sesa514652 | 18:bb36d96142dd | 118 | //initialise Joystick |
sesa514652 | 5:9b2c976ca318 | 119 | joystick.init(); |
sesa514652 | 5:9b2c976ca318 | 120 | |
sesa514652 | 11:1dec05b7d1c1 | 121 | // create a thread that'll run the event queue's dispatch function |
sesa514652 | 11:1dec05b7d1c1 | 122 | Thread eventThread; |
sesa514652 | 11:1dec05b7d1c1 | 123 | eventThread.start(callback(&queue, &EventQueue::dispatch_forever)); |
sesa514652 | 11:1dec05b7d1c1 | 124 | |
sesa514652 | 11:1dec05b7d1c1 | 125 | buttonA.rise(queue.event(&buttonA_isr)); // This is used to call the function putting it in queue allowing the screen toupdat no muxt error |
sesa514652 | 11:1dec05b7d1c1 | 126 | buttonB.rise(queue.event(&buttonB_isr)); |
sesa514652 | 11:1dec05b7d1c1 | 127 | buttonX.rise(queue.event(&buttonX_isr)); |
sesa514652 | 11:1dec05b7d1c1 | 128 | buttonY.rise(queue.event(&buttonY_isr)); |
sesa514652 | 11:1dec05b7d1c1 | 129 | buttonStart.rise(queue.event(&buttonStart_isr)); // This is used to call the function putting it in queue allowing the screen toupdat no muxt error |
sesa514652 | 11:1dec05b7d1c1 | 130 | buttonBack.rise(queue.event(&buttonBack_isr)); |
sesa514652 | 11:1dec05b7d1c1 | 131 | buttonLeft.rise(queue.event(&buttonLeft_isr)); |
sesa514652 | 11:1dec05b7d1c1 | 132 | buttonRight.rise(queue.event(&buttonRight_isr)); |
sesa514652 | 5:9b2c976ca318 | 133 | //first need to initialise display |
sesa514652 | 5:9b2c976ca318 | 134 | lcd.init(); |
sesa514652 | 8:770d168713cc | 135 | |
sesa514652 | 12:ceea378d5691 | 136 | // Button A is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister |
sesa514652 | 8:770d168713cc | 137 | buttonA.mode(PullDown); |
sesa514652 | 10:42e70b596099 | 138 | // Button B is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister |
sesa514652 | 10:42e70b596099 | 139 | buttonB.mode(PullDown); |
sesa514652 | 10:42e70b596099 | 140 | // Button X is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister |
sesa514652 | 10:42e70b596099 | 141 | buttonX.mode(PullDown); |
sesa514652 | 10:42e70b596099 | 142 | // Button Y is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister |
sesa514652 | 10:42e70b596099 | 143 | buttonY.mode(PullDown); |
sesa514652 | 11:1dec05b7d1c1 | 144 | // Button Start is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister |
sesa514652 | 11:1dec05b7d1c1 | 145 | buttonStart.mode(PullDown); |
sesa514652 | 11:1dec05b7d1c1 | 146 | // Button Back is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister |
sesa514652 | 11:1dec05b7d1c1 | 147 | buttonBack.mode(PullDown); |
sesa514652 | 11:1dec05b7d1c1 | 148 | // Button Left is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister |
sesa514652 | 11:1dec05b7d1c1 | 149 | buttonLeft.mode(PullDown); |
sesa514652 | 11:1dec05b7d1c1 | 150 | // Button Right is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister |
sesa514652 | 11:1dec05b7d1c1 | 151 | buttonRight.mode(PullDown); |
sesa514652 | 8:770d168713cc | 152 | |
sesa514652 | 18:bb36d96142dd | 153 | |
sesa514652 | 8:770d168713cc | 154 | |
sesa514652 | 8:770d168713cc | 155 | |
sesa514652 | 5:9b2c976ca318 | 156 | |
sesa514652 | 5:9b2c976ca318 | 157 | //change set contrast in range 0.0 to 1.0 |
sesa514652 | 5:9b2c976ca318 | 158 | //0.5 appears to be a good starting point |
sesa514652 | 5:9b2c976ca318 | 159 | lcd.setContrast(0.5); |
sesa514652 | 5:9b2c976ca318 | 160 | |
sesa514652 | 5:9b2c976ca318 | 161 | //Buzzer.period(10.0f); |
sesa514652 | 5:9b2c976ca318 | 162 | //Buzzer.pulsewidth(1); |
sesa514652 | 6:f7f30e0e3bed | 163 | //Test Struct after research 05/01/22 |
sesa514652 | 6:f7f30e0e3bed | 164 | struct ObjectDefine{ |
sesa514652 | 7:ce70a873aa70 | 165 | int qwer; |
sesa514652 | 6:f7f30e0e3bed | 166 | float Distance; |
sesa514652 | 7:ce70a873aa70 | 167 | char te; |
sesa514652 | 7:ce70a873aa70 | 168 | double dubs; |
sesa514652 | 7:ce70a873aa70 | 169 | string namestruct; |
sesa514652 | 8:770d168713cc | 170 | // string ; |
sesa514652 | 6:f7f30e0e3bed | 171 | }; |
sesa514652 | 7:ce70a873aa70 | 172 | // Test Class 06/01/22 |
sesa514652 | 10:42e70b596099 | 173 | class myClass{ |
sesa514652 | 7:ce70a873aa70 | 174 | public: |
sesa514652 | 7:ce70a873aa70 | 175 | string nameclass ; |
sesa514652 | 7:ce70a873aa70 | 176 | double dubss; |
sesa514652 | 7:ce70a873aa70 | 177 | int inty; |
sesa514652 | 7:ce70a873aa70 | 178 | }; |
sesa514652 | 7:ce70a873aa70 | 179 | myClass Class1; |
sesa514652 | 7:ce70a873aa70 | 180 | Class1.nameclass ="firstclass"; |
sesa514652 | 10:42e70b596099 | 181 | ObjectDefine Object1; |
sesa514652 | 10:42e70b596099 | 182 | Object1.qwer = 12; |
sesa514652 | 6:f7f30e0e3bed | 183 | Object1.Distance = 34; |
sesa514652 | 7:ce70a873aa70 | 184 | Object1.te = 'A'; |
sesa514652 | 7:ce70a873aa70 | 185 | Object1.dubs = 23; |
sesa514652 | 10:42e70b596099 | 186 | Object1.namestruct = "name"; |
sesa514652 | 18:bb36d96142dd | 187 | |
sesa514652 | 0:1f799c7cce2b | 188 | while(1) { |
sesa514652 | 22:82b01fdfc61f | 189 | // section to try to get POC of number comparitor without using the US for repeatablity// to get if statements correct possibly change to switch |
sesa514652 | 23:f72175fb49ed | 190 | //int Scan1[10]; |
sesa514652 | 22:82b01fdfc61f | 191 | int num2; |
sesa514652 | 22:82b01fdfc61f | 192 | int num3; |
sesa514652 | 22:82b01fdfc61f | 193 | int num4; |
sesa514652 | 22:82b01fdfc61f | 194 | cout << "Test value collection"; |
sesa514652 | 23:f72175fb49ed | 195 | Scan1Input(); |
sesa514652 | 24:a88c4941a25e | 196 | cout << Scan1[0];cout <<"/n"; |
sesa514652 | 24:a88c4941a25e | 197 | cout << Scan1[1];"/n"; |
sesa514652 | 24:a88c4941a25e | 198 | cout << Scan1[2];"/n"; |
sesa514652 | 24:a88c4941a25e | 199 | cout << Scan1[3];"/n"; |
sesa514652 | 24:a88c4941a25e | 200 | cout << Scan1[4];"/n"; |
sesa514652 | 24:a88c4941a25e | 201 | cout << Scan1[5];"/n"; |
sesa514652 | 24:a88c4941a25e | 202 | cout << Scan1[6];"/n"; |
sesa514652 | 24:a88c4941a25e | 203 | cout << Scan1[7];"/n"; |
sesa514652 | 24:a88c4941a25e | 204 | cout << Scan1[8];"/n"; |
sesa514652 | 24:a88c4941a25e | 205 | cout << Scan1[9];"/n"; |
sesa514652 | 24:a88c4941a25e | 206 | |
sesa514652 | 24:a88c4941a25e | 207 | |
sesa514652 | 24:a88c4941a25e | 208 | |
sesa514652 | 24:a88c4941a25e | 209 | // cout << Scan1[2]; |
sesa514652 | 24:a88c4941a25e | 210 | // cout << Scan1[6]; |
sesa514652 | 24:a88c4941a25e | 211 | // IF Statement to decide on shape to draw |
sesa514652 | 24:a88c4941a25e | 212 | if (Scan1[0] > Scan1[1]){ |
sesa514652 | 24:a88c4941a25e | 213 | cout <<'Higher'; |
sesa514652 | 24:a88c4941a25e | 214 | }else if (Scan1[0] = Scan1[1]){ |
sesa514652 | 24:a88c4941a25e | 215 | cout <<'Equal';} |
sesa514652 | 24:a88c4941a25e | 216 | else if (Scan1[0] < Scan1[1]){ |
sesa514652 | 24:a88c4941a25e | 217 | cout <<'Lower';} |
sesa514652 | 24:a88c4941a25e | 218 | |
sesa514652 | 24:a88c4941a25e | 219 | /* |
sesa514652 | 24:a88c4941a25e | 220 | if (Scan1[1] > Scan1[2]){ |
sesa514652 | 24:a88c4941a25e | 221 | cout <<'+'; |
sesa514652 | 24:a88c4941a25e | 222 | }else if (Scan1[1]=Scan1[2]){ |
sesa514652 | 24:a88c4941a25e | 223 | cout <<'=';} |
sesa514652 | 24:a88c4941a25e | 224 | else { |
sesa514652 | 24:a88c4941a25e | 225 | cout <<'-';} |
sesa514652 | 24:a88c4941a25e | 226 | if (Scan1[2] > Scan1[3]){ |
sesa514652 | 24:a88c4941a25e | 227 | cout <<'+'; |
sesa514652 | 24:a88c4941a25e | 228 | }else if (Scan1[2]=Scan1[3]){ |
sesa514652 | 24:a88c4941a25e | 229 | cout <<'=';} |
sesa514652 | 23:f72175fb49ed | 230 | else { |
sesa514652 | 23:f72175fb49ed | 231 | cout <<'-';} |
sesa514652 | 24:a88c4941a25e | 232 | |
sesa514652 | 24:a88c4941a25e | 233 | if (Scan1[3] > Scan1[4]){ |
sesa514652 | 24:a88c4941a25e | 234 | cout <<'+'; |
sesa514652 | 24:a88c4941a25e | 235 | }else if (Scan1[3]=Scan1[4]){ |
sesa514652 | 24:a88c4941a25e | 236 | cout <<'=';} |
sesa514652 | 24:a88c4941a25e | 237 | else { |
sesa514652 | 24:a88c4941a25e | 238 | cout <<'-';} |
sesa514652 | 24:a88c4941a25e | 239 | if (Scan1[4] > Scan1[5]){ |
sesa514652 | 24:a88c4941a25e | 240 | cout <<'+'; |
sesa514652 | 24:a88c4941a25e | 241 | }else if (Scan1[4]=Scan1[5]){ |
sesa514652 | 24:a88c4941a25e | 242 | cout <<'=';} |
sesa514652 | 24:a88c4941a25e | 243 | else { |
sesa514652 | 24:a88c4941a25e | 244 | cout <<'-';} |
sesa514652 | 24:a88c4941a25e | 245 | if (Scan1[5] > Scan1[6]){ |
sesa514652 | 24:a88c4941a25e | 246 | cout <<'+'; |
sesa514652 | 24:a88c4941a25e | 247 | }else if (Scan1[5]=Scan1[6]){ |
sesa514652 | 24:a88c4941a25e | 248 | cout <<'=';} |
sesa514652 | 24:a88c4941a25e | 249 | else { |
sesa514652 | 24:a88c4941a25e | 250 | cout <<'-';} |
sesa514652 | 24:a88c4941a25e | 251 | if (Scan1[6] > Scan1[7]){ |
sesa514652 | 24:a88c4941a25e | 252 | cout <<'+'; |
sesa514652 | 24:a88c4941a25e | 253 | }else if (Scan1[6]=Scan1[7]){ |
sesa514652 | 24:a88c4941a25e | 254 | cout <<'=';} |
sesa514652 | 24:a88c4941a25e | 255 | else { |
sesa514652 | 24:a88c4941a25e | 256 | cout <<'-';} |
sesa514652 | 24:a88c4941a25e | 257 | if (Scan1[7] > Scan1[8]){ |
sesa514652 | 24:a88c4941a25e | 258 | cout <<'+'; |
sesa514652 | 24:a88c4941a25e | 259 | }else if (Scan1[7]=Scan1[8]){ |
sesa514652 | 24:a88c4941a25e | 260 | cout <<'=';} |
sesa514652 | 24:a88c4941a25e | 261 | else { |
sesa514652 | 24:a88c4941a25e | 262 | cout <<'-';} |
sesa514652 | 24:a88c4941a25e | 263 | |
sesa514652 | 24:a88c4941a25e | 264 | if (Scan1[8] > Scan1[9]){ |
sesa514652 | 24:a88c4941a25e | 265 | cout <<'+'; |
sesa514652 | 24:a88c4941a25e | 266 | }else if (Scan1[8]=Scan1[9]){ |
sesa514652 | 24:a88c4941a25e | 267 | cout <<'=';} |
sesa514652 | 24:a88c4941a25e | 268 | else { |
sesa514652 | 24:a88c4941a25e | 269 | cout <<'-';} |
sesa514652 | 24:a88c4941a25e | 270 | if (Scan1[9] > Scan1[10]){ |
sesa514652 | 24:a88c4941a25e | 271 | cout <<'+'; |
sesa514652 | 24:a88c4941a25e | 272 | }else if (Scan1[9]=Scan1[10]){ |
sesa514652 | 24:a88c4941a25e | 273 | cout <<'=';} |
sesa514652 | 24:a88c4941a25e | 274 | else { |
sesa514652 | 24:a88c4941a25e | 275 | cout <<'-';} |
sesa514652 | 24:a88c4941a25e | 276 | */ |
sesa514652 | 21:2e37f80e8b5b | 277 | |
sesa514652 | 22:82b01fdfc61f | 278 | ///////////////////////////////////////////////////////////////// |
sesa514652 | 20:4e1f2917a857 | 279 | switch(PressA){ |
sesa514652 | 21:2e37f80e8b5b | 280 | default: |
sesa514652 | 21:2e37f80e8b5b | 281 | lcd.clear(); |
sesa514652 | 21:2e37f80e8b5b | 282 | lcd.printString("Main Menu",0,0); |
sesa514652 | 21:2e37f80e8b5b | 283 | lcd.drawLine(0,10,80,10,2); |
sesa514652 | 21:2e37f80e8b5b | 284 | |
sesa514652 | 21:2e37f80e8b5b | 285 | lcd.printString("A to begin new Scan",0,2); |
sesa514652 | 21:2e37f80e8b5b | 286 | lcd.printString("B to Display last object",0,3); |
sesa514652 | 21:2e37f80e8b5b | 287 | lcd.refresh(); |
sesa514652 | 21:2e37f80e8b5b | 288 | wait_ms(50); |
sesa514652 | 21:2e37f80e8b5b | 289 | |
sesa514652 | 21:2e37f80e8b5b | 290 | break; |
sesa514652 | 22:82b01fdfc61f | 291 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
sesa514652 | 21:2e37f80e8b5b | 292 | case 1: |
sesa514652 | 21:2e37f80e8b5b | 293 | PressA =1; |
sesa514652 | 21:2e37f80e8b5b | 294 | lcd.clear(); |
sesa514652 | 21:2e37f80e8b5b | 295 | long firstScan[10]; |
sesa514652 | 21:2e37f80e8b5b | 296 | lcd.printString("First Scan Left Right",0,0); |
sesa514652 | 21:2e37f80e8b5b | 297 | lcd.refresh(); |
sesa514652 | 21:2e37f80e8b5b | 298 | for (int i = 0; i < 5; i++) { |
sesa514652 | 21:2e37f80e8b5b | 299 | printf(" i is: %d cm",i); |
sesa514652 | 21:2e37f80e8b5b | 300 | } |
sesa514652 | 20:4e1f2917a857 | 301 | wait_ms(50); // required to stop Black screen issue |
sesa514652 | 20:4e1f2917a857 | 302 | break; |
sesa514652 | 22:82b01fdfc61f | 303 | //////////////////////////////////////////////////////////////////////////// |
sesa514652 | 20:4e1f2917a857 | 304 | case 2: |
sesa514652 | 20:4e1f2917a857 | 305 | PressA= 2; |
sesa514652 | 22:82b01fdfc61f | 306 | long secondScan[10]; |
sesa514652 | 22:82b01fdfc61f | 307 | |
sesa514652 | 22:82b01fdfc61f | 308 | secondScan[5] = sensor.distance(); |
sesa514652 | 22:82b01fdfc61f | 309 | printf("Distance : %d cm", secondScan[5]); |
sesa514652 | 22:82b01fdfc61f | 310 | lcd.clear(); |
sesa514652 | 22:82b01fdfc61f | 311 | lcd.printString("2nd Scan up down",0,0); |
sesa514652 | 22:82b01fdfc61f | 312 | lcd.drawLine(0,10,80,10,2); |
sesa514652 | 20:4e1f2917a857 | 313 | break; |
sesa514652 | 22:82b01fdfc61f | 314 | |
sesa514652 | 22:82b01fdfc61f | 315 | //////////////////////////////////////////////////////////////////// |
sesa514652 | 20:4e1f2917a857 | 316 | case 3: |
sesa514652 | 20:4e1f2917a857 | 317 | PressA= 3; |
sesa514652 | 20:4e1f2917a857 | 318 | lcd.clear(); |
sesa514652 | 20:4e1f2917a857 | 319 | lcd.printString("Menu 3",0,0); |
sesa514652 | 21:2e37f80e8b5b | 320 | long us = sensor.distance(); |
sesa514652 | 21:2e37f80e8b5b | 321 | printf("Distance : %d cm",us); |
sesa514652 | 20:4e1f2917a857 | 322 | lcd.refresh(); |
sesa514652 | 20:4e1f2917a857 | 323 | wait_ms(50); //required to stop Black screen issue |
sesa514652 | 20:4e1f2917a857 | 324 | break; |
sesa514652 | 20:4e1f2917a857 | 325 | } |
sesa514652 | 20:4e1f2917a857 | 326 | |
sesa514652 | 20:4e1f2917a857 | 327 | } |
sesa514652 | 23:f72175fb49ed | 328 | |
sesa514652 | 20:4e1f2917a857 | 329 | /* |
sesa514652 | 11:1dec05b7d1c1 | 330 | int button_counter; |
sesa514652 | 10:42e70b596099 | 331 | if (g_buttonA_flag){ |
sesa514652 | 11:1dec05b7d1c1 | 332 | button_counter = button_counter +1; |
sesa514652 | 11:1dec05b7d1c1 | 333 | //case (test) |
sesa514652 | 10:42e70b596099 | 334 | g_buttonA_flag = 0; // if it has, clear the flag |
sesa514652 | 10:42e70b596099 | 335 | |
sesa514652 | 10:42e70b596099 | 336 | // send message over serial port - can observe in CoolTerm etc. |
sesa514652 | 11:1dec05b7d1c1 | 337 | printf("Button A pressed %f\n", button_counter); |
sesa514652 | 10:42e70b596099 | 338 | } |
sesa514652 | 10:42e70b596099 | 339 | if (g_buttonB_flag){ |
sesa514652 | 10:42e70b596099 | 340 | g_buttonB_flag = 0; // if it has, clear the flag |
sesa514652 | 8:770d168713cc | 341 | |
sesa514652 | 10:42e70b596099 | 342 | // send message over serial port - can observe in CoolTerm etc. |
sesa514652 | 10:42e70b596099 | 343 | printf("Button B pressed\n"); |
sesa514652 | 10:42e70b596099 | 344 | } |
sesa514652 | 10:42e70b596099 | 345 | if (g_buttonX_flag){ |
sesa514652 | 10:42e70b596099 | 346 | g_buttonX_flag = 0; // if it has, clear the flag |
sesa514652 | 12:ceea378d5691 | 347 | // swap direction when button has been pressed |
sesa514652 | 12:ceea378d5691 | 348 | // (could just use ! but want this to be explicit to aid understanding) |
sesa514652 | 12:ceea378d5691 | 349 | if (direction == UP) { |
sesa514652 | 12:ceea378d5691 | 350 | direction = DOWN; |
sesa514652 | 12:ceea378d5691 | 351 | } |
sesa514652 | 12:ceea378d5691 | 352 | else { |
sesa514652 | 12:ceea378d5691 | 353 | direction = UP; |
sesa514652 | 12:ceea378d5691 | 354 | } |
sesa514652 | 10:42e70b596099 | 355 | |
sesa514652 | 10:42e70b596099 | 356 | // send message over serial port - can observe in CoolTerm etc. |
sesa514652 | 10:42e70b596099 | 357 | printf("Button X pressed\n"); |
sesa514652 | 10:42e70b596099 | 358 | lcd.clear(); |
sesa514652 | 10:42e70b596099 | 359 | lcd.printString("Button Pressed!",0,0); |
sesa514652 | 10:42e70b596099 | 360 | lcd.refresh(); |
sesa514652 | 11:1dec05b7d1c1 | 361 | wait(1); |
sesa514652 | 10:42e70b596099 | 362 | } |
sesa514652 | 10:42e70b596099 | 363 | if (g_buttonY_flag){ |
sesa514652 | 10:42e70b596099 | 364 | g_buttonY_flag = 0; // if it has, clear the flag |
sesa514652 | 10:42e70b596099 | 365 | |
sesa514652 | 10:42e70b596099 | 366 | // send message over serial port - can observe in CoolTerm etc. |
sesa514652 | 10:42e70b596099 | 367 | printf("Button Y pressed\n"); |
sesa514652 | 10:42e70b596099 | 368 | } |
sesa514652 | 5:9b2c976ca318 | 369 | Buzzer.play(200,120); |
sesa514652 | 5:9b2c976ca318 | 370 | wait_ms(5); |
sesa514652 | 5:9b2c976ca318 | 371 | Buzzer.play(200,120); |
sesa514652 | 1:757728321abd | 372 | long distanced = sensor.distance(); |
sesa514652 | 1:757728321abd | 373 | if (distanced >= 400 || distanced <= 2) |
sesa514652 | 5:9b2c976ca318 | 374 | { |
sesa514652 | 1:757728321abd | 375 | printf("Out of range"); |
sesa514652 | 5:9b2c976ca318 | 376 | //Calling dummy function 05/01/22 |
sesa514652 | 4:167ce930c9d5 | 377 | int answer = cube(2); |
sesa514652 | 5:9b2c976ca318 | 378 | printf("%f\n",answer); |
sesa514652 | 1:757728321abd | 379 | wait(1.0); |
sesa514652 | 5:9b2c976ca318 | 380 | } |
sesa514652 | 5:9b2c976ca318 | 381 | else |
sesa514652 | 5:9b2c976ca318 | 382 | { |
sesa514652 | 5:9b2c976ca318 | 383 | printf("Distance : %d cm",distanced); |
sesa514652 | 5:9b2c976ca318 | 384 | wait(1.0); // 1 sec |
sesa514652 | 1:757728321abd | 385 | } |
sesa514652 | 5:9b2c976ca318 | 386 | lcd.drawCircle(WIDTH/2,HEIGHT/2,distanced,FILL_BLACK); |
sesa514652 | 5:9b2c976ca318 | 387 | //lcd.refresh must be used to update the lcd Display |
sesa514652 | 5:9b2c976ca318 | 388 | lcd.refresh(); |
sesa514652 | 5:9b2c976ca318 | 389 | wait(5.0); |
sesa514652 | 5:9b2c976ca318 | 390 | //these are default settings so not strictly needed |
sesa514652 | 0:1f799c7cce2b | 391 | lcd.normalMode(); // normal colour mode |
sesa514652 | 5:9b2c976ca318 | 392 | lcd.setBrightness(0.5); // put LED backlight on 50% |
sesa514652 | 0:1f799c7cce2b | 393 | lcd.clear(); |
sesa514652 | 0:1f799c7cce2b | 394 | lcd.printString("Hello, World!",0,0); |
sesa514652 | 0:1f799c7cce2b | 395 | Vector2D coord = joystick.get_coord(); |
sesa514652 | 0:1f799c7cce2b | 396 | lcd.refresh(); |
sesa514652 | 0:1f799c7cce2b | 397 | printf("Coord = %f,%f\n",coord.x,coord.y); |
sesa514652 | 0:1f799c7cce2b | 398 | char buffer[14]; |
sesa514652 | 0:1f799c7cce2b | 399 | Vector2D mapped_coord = joystick.get_mapped_coord(); |
sesa514652 | 0:1f799c7cce2b | 400 | printf("Mapped coord = %f,%f\n",mapped_coord.x,mapped_coord.y); |
sesa514652 | 0:1f799c7cce2b | 401 | |
sesa514652 | 5:9b2c976ca318 | 402 | int length = sprintf(buffer,"T = %2f",coord.x); |
sesa514652 | 5:9b2c976ca318 | 403 | //it is important the format specifier ensures the length will fit in the buffer |
sesa514652 | 0:1f799c7cce2b | 404 | if (length <= 14) // if string will fit on display (assuming printing at x=0) |
sesa514652 | 5:9b2c976ca318 | 405 | lcd.printString(buffer,0,1); // display on screen |
sesa514652 | 0:1f799c7cce2b | 406 | lcd.refresh(); |
sesa514652 | 5:9b2c976ca318 | 407 | lcd.clear(); |
sesa514652 | 5:9b2c976ca318 | 408 | //times by 50 to try get the point moving across screen |
sesa514652 | 0:1f799c7cce2b | 409 | float x50 = mapped_coord.x*5; |
sesa514652 | 0:1f799c7cce2b | 410 | float y50 = mapped_coord.y*8; |
sesa514652 | 5:9b2c976ca318 | 411 | //to see what the values are |
sesa514652 | 5:9b2c976ca318 | 412 | printf(" *50 = %f,%f\n",x50,y50); |
sesa514652 | 5:9b2c976ca318 | 413 | //example of how to draw circles |
sesa514652 | 1:757728321abd | 414 | lcd.printChar('o',WIDTH/2,HEIGHT/2); |
sesa514652 | 5:9b2c976ca318 | 415 | lcd.refresh(); |
sesa514652 | 0:1f799c7cce2b | 416 | float mag = joystick.get_mag(); |
sesa514652 | 0:1f799c7cce2b | 417 | float angle = joystick.get_angle(); |
sesa514652 | 0:1f799c7cce2b | 418 | printf("Mag = %f Angle = %f\n",mag,angle); |
sesa514652 | 0:1f799c7cce2b | 419 | Direction d = joystick.get_direction(); |
sesa514652 | 0:1f799c7cce2b | 420 | printf("Direction = %i\n",d); |
sesa514652 | 0:1f799c7cce2b | 421 | if (joystick.button_pressed() ) { |
sesa514652 | 0:1f799c7cce2b | 422 | printf("Button Pressed\n"); |
sesa514652 | 9:cdd7d9a123f9 | 423 | wait(5); |
sesa514652 | 0:1f799c7cce2b | 424 | } |
sesa514652 | 8:770d168713cc | 425 | |
sesa514652 | 8:770d168713cc | 426 | } |
sesa514652 | 16:2646c1c4e62e | 427 | } |
sesa514652 | 20:4e1f2917a857 | 428 | */ |
sesa514652 | 23:f72175fb49ed | 429 | } |
sesa514652 | 18:bb36d96142dd | 430 | |
sesa514652 | 18:bb36d96142dd | 431 | int buttonCountA(){ |
sesa514652 | 18:bb36d96142dd | 432 | PressA++; |
sesa514652 | 18:bb36d96142dd | 433 | if (PressA>3){ |
sesa514652 | 18:bb36d96142dd | 434 | PressA= 0; |
sesa514652 | 18:bb36d96142dd | 435 | } |
sesa514652 | 18:bb36d96142dd | 436 | return PressA; |
sesa514652 | 18:bb36d96142dd | 437 | } |
sesa514652 | 18:bb36d96142dd | 438 | int buttonCountB(){ |
sesa514652 | 18:bb36d96142dd | 439 | PressB++; |
sesa514652 | 18:bb36d96142dd | 440 | if (PressB>3){ |
sesa514652 | 18:bb36d96142dd | 441 | PressB= 0; |
sesa514652 | 18:bb36d96142dd | 442 | } |
sesa514652 | 18:bb36d96142dd | 443 | return PressB; |
sesa514652 | 18:bb36d96142dd | 444 | } |
sesa514652 | 15:234321e80678 | 445 | |
sesa514652 | 12:ceea378d5691 | 446 | //Button A event-triggered interrupt |
sesa514652 | 10:42e70b596099 | 447 | void buttonA_isr() |
sesa514652 | 10:42e70b596099 | 448 | { |
sesa514652 | 12:ceea378d5691 | 449 | |
sesa514652 | 18:bb36d96142dd | 450 | long distanced = sensor.distance(); |
sesa514652 | 18:bb36d96142dd | 451 | buttonCountA(); |
sesa514652 | 18:bb36d96142dd | 452 | |
sesa514652 | 16:2646c1c4e62e | 453 | |
sesa514652 | 12:ceea378d5691 | 454 | |
sesa514652 | 12:ceea378d5691 | 455 | g_buttonA_flag = 1; // set flag in ISR |
sesa514652 | 12:ceea378d5691 | 456 | |
sesa514652 | 18:bb36d96142dd | 457 | printf("press count = %i\n",PressA); |
sesa514652 | 12:ceea378d5691 | 458 | |
sesa514652 | 12:ceea378d5691 | 459 | // lcd.clear(); |
sesa514652 | 15:234321e80678 | 460 | //lcd.printString("Button A Pressed",0,0); |
sesa514652 | 12:ceea378d5691 | 461 | |
sesa514652 | 15:234321e80678 | 462 | // lcd.refresh(); // set flag in ISR |
sesa514652 | 12:ceea378d5691 | 463 | } |
sesa514652 | 12:ceea378d5691 | 464 | |
sesa514652 | 11:1dec05b7d1c1 | 465 | |
sesa514652 | 10:42e70b596099 | 466 | //Button B event-triggered interrupt |
sesa514652 | 10:42e70b596099 | 467 | void buttonB_isr() |
sesa514652 | 10:42e70b596099 | 468 | { |
sesa514652 | 18:bb36d96142dd | 469 | buttonCountB(); |
sesa514652 | 10:42e70b596099 | 470 | g_buttonB_flag = 1; // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 471 | lcd.clear(); |
sesa514652 | 11:1dec05b7d1c1 | 472 | lcd.printString("Button B Pressed",0,0); |
sesa514652 | 11:1dec05b7d1c1 | 473 | lcd.refresh(); // set flag in ISR |
sesa514652 | 18:bb36d96142dd | 474 | printf("press count = %i\n",PressB); |
sesa514652 | 10:42e70b596099 | 475 | } |
sesa514652 | 10:42e70b596099 | 476 | //Button X event-triggered interrupt |
sesa514652 | 10:42e70b596099 | 477 | void buttonX_isr() |
sesa514652 | 10:42e70b596099 | 478 | { |
sesa514652 | 10:42e70b596099 | 479 | g_buttonX_flag = 1; // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 480 | lcd.clear(); |
sesa514652 | 11:1dec05b7d1c1 | 481 | lcd.printString("Button X Pressed",0,0); |
sesa514652 | 11:1dec05b7d1c1 | 482 | lcd.refresh(); // set flag in ISR |
sesa514652 | 10:42e70b596099 | 483 | } |
sesa514652 | 10:42e70b596099 | 484 | //Button Y event-triggered interrupt |
sesa514652 | 10:42e70b596099 | 485 | void buttonY_isr() |
sesa514652 | 10:42e70b596099 | 486 | { |
sesa514652 | 10:42e70b596099 | 487 | g_buttonY_flag = 1; // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 488 | lcd.clear(); |
sesa514652 | 11:1dec05b7d1c1 | 489 | lcd.printString("Button Y Pressed",0,0); |
sesa514652 | 11:1dec05b7d1c1 | 490 | lcd.refresh(); // set flag in ISR |
sesa514652 | 10:42e70b596099 | 491 | } |
sesa514652 | 11:1dec05b7d1c1 | 492 | //Button Start event-triggered interrupt |
sesa514652 | 11:1dec05b7d1c1 | 493 | void buttonStart_isr() |
sesa514652 | 11:1dec05b7d1c1 | 494 | { |
sesa514652 | 11:1dec05b7d1c1 | 495 | g_buttonStart_flag = 1; // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 496 | lcd.clear(); |
sesa514652 | 11:1dec05b7d1c1 | 497 | lcd.printString("Button Start Pressed",0,0); |
sesa514652 | 11:1dec05b7d1c1 | 498 | lcd.refresh(); // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 499 | } |
sesa514652 | 11:1dec05b7d1c1 | 500 | //Button Back event-triggered interrupt |
sesa514652 | 11:1dec05b7d1c1 | 501 | void buttonBack_isr() |
sesa514652 | 11:1dec05b7d1c1 | 502 | { |
sesa514652 | 11:1dec05b7d1c1 | 503 | g_buttonBack_flag = 1; // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 504 | lcd.clear(); |
sesa514652 | 11:1dec05b7d1c1 | 505 | lcd.printString("Button Back Pressed",0,0); |
sesa514652 | 11:1dec05b7d1c1 | 506 | lcd.refresh(); // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 507 | } |
sesa514652 | 11:1dec05b7d1c1 | 508 | //Button Left event-triggered interrupt |
sesa514652 | 11:1dec05b7d1c1 | 509 | void buttonLeft_isr() |
sesa514652 | 11:1dec05b7d1c1 | 510 | { |
sesa514652 | 11:1dec05b7d1c1 | 511 | g_buttonLeft_flag = 1; // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 512 | lcd.clear(); |
sesa514652 | 11:1dec05b7d1c1 | 513 | lcd.printString("Button Left Pressed",0,0); |
sesa514652 | 11:1dec05b7d1c1 | 514 | lcd.refresh(); // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 515 | } |
sesa514652 | 11:1dec05b7d1c1 | 516 | //Button Right event-triggered interrupt |
sesa514652 | 11:1dec05b7d1c1 | 517 | void buttonRight_isr() |
sesa514652 | 11:1dec05b7d1c1 | 518 | { |
sesa514652 | 11:1dec05b7d1c1 | 519 | g_buttonRight_flag = 1; // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 520 | lcd.clear(); |
sesa514652 | 11:1dec05b7d1c1 | 521 | lcd.printString("Button Right Pressed",0,0); |
sesa514652 | 11:1dec05b7d1c1 | 522 | lcd.refresh(); // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 523 | } |