Final revision to radar/distance measuring tool. Radar feature not implemented within time frame due to issues with hardware and mbed.org servers
Dependencies: N5110 PowerControl SRF02 mbed
main.cpp
00001 /** 00002 @file main.cpp 00003 @brief Radar/Distance Measurement program implementation 00004 @author Karina Sodipo 00005 @date May 2015 00006 */ 00007 00008 #include "mbed.h" 00009 #include "N5110.h" 00010 #include "SRF02.h" 00011 #include "PowerControl/PowerControl.h" 00012 #include "PowerControl/EthernetPowerControl.h" 00013 00014 /** 00015 @see - mbed CookBook (Power Management) 00016 */ 00017 00018 /// function to power down the USB interface 00019 #define USR_POWERDOWN (0x104) 00020 int semihost_powerdown() { 00021 uint32_t arg; 00022 return __semihost(USR_POWERDOWN, &arg); 00023 } 00024 00025 00026 /** 00027 @namespace lcd 00028 @brief LCD screen display with VCC,SCE,RST,D/C,MOSI,SCLK,LED pins 00029 */ 00030 N5110 lcd(p7,p8,p9,p10,p11,p13,p26); // 00031 00032 /** 00033 @namespace sensor 00034 @brief SRF02 ultrasonic distance sensor 00035 */ 00036 SRF02 sensor(p28,p27); 00037 00038 /** 00039 @namespace leds 00040 @brief the on-board mbed LEDs in a BusOut class 00041 */ 00042 BusOut leds(LED4,LED3,LED2,LED1); 00043 00044 /** 00045 @namespace led 00046 @brief LED proximity indicator 00047 */ 00048 PwmOut led(p24); 00049 00050 /** 00051 @namespace screenInvert 00052 @brief button to switch from black-on-white to white-on-black 00053 */ 00054 InterruptIn screenInvert(p16); 00055 00056 /** 00057 @namespace unitSW 00058 @brief button to switch from cm to inches 00059 */ 00060 InterruptIn unitSW(p17); 00061 00062 /** 00063 @namespace radarDist 00064 @brief slide switch toggling radar and distance displays 00065 */ 00066 InterruptIn radarDist(p18); 00067 00068 /** 00069 @namespace POT 00070 @brief potentiometer to change backlight brightness on LCD screen 00071 */ 00072 AnalogIn POT(p20); 00073 00074 /** 00075 @namespace buzzer 00076 @brief Piezo buzzer for audio alerts (distance below 50cm / 20inch threshold AND countdown to sensor update) 00077 */ 00078 PwmOut buzzer(p21); 00079 00080 00081 00082 /// function to display the checkerboard pattern 00083 void checkerboard(); 00084 00085 /// function for LED and Piezo alerts in CM 00086 void proximityCm(); 00087 00088 /// function for LED and Piezo alerts in INCHES 00089 void proximityIn(); 00090 00091 /// function to define states of screenInvert button 00092 void initDisplay(); 00093 00094 /// function to define states of unitsSW button 00095 void initMeasureIn(); 00096 00097 /// function for potentiometer to change screen brightness 00098 void brightness(); 00099 00100 /// function to display readings/radar in black-on-white mode 00101 void normal(); 00102 00103 /// function to display readings/radar in white-on-black mode 00104 void invert(); 00105 00106 /// function to measure distance in cm 00107 void centimetre(); 00108 00109 /// function to measure distance in inches 00110 void inches(); 00111 00112 /// function for radar display 00113 void radar(); 00114 00115 00116 00117 /// buffer to store data sent to the screen 00118 char buffer[14]; 00119 00120 /// Change units to measure distance in, modified in units() ISR 00121 int unit = 0; /*!< 'unit' toggled in units() ISR */ 00122 00123 /// ISR for toggling between CM and INCH readings 00124 void units() { 00125 00126 unit++; 00127 if (unit > 1) { // check whether unit modes stays within bounds 00128 unit = 0; 00129 } 00130 00131 } 00132 00133 00134 00135 /// pixel display style, modified in pixels() ISR 00136 int pixel = 0; /*!< 'pixel' set in pixels() ISR */ 00137 00138 /// ISR for toggling between black-on-white and white-on-black display 00139 void pixels() { 00140 00141 pixel++; 00142 if (pixel > 1) { // check whether display modes stays within bounds 00143 pixel = 0; 00144 } 00145 } 00146 00147 00148 00149 int main () { 00150 00151 /// Power down Ethernet interface to save ~ 175mW 00152 PHY_PowerDown(); 00153 00154 /// Power down magic USB interface chip to save ~ 150mW 00155 int result = semihost_powerdown(); 00156 00157 /// Disable clock on unused peripherals 00158 Peripheral_PowerDown(0x2000); /// CAN controller 1 00159 Peripheral_PowerDown(0x4000); /// CAN controller 2 00160 00161 /// initialize the screen 00162 lcd.init(); 00163 00164 /// call pixels() on rising edge when button pressed 00165 screenInvert.mode(PullUp); 00166 screenInvert.rise(&pixels); 00167 00168 /// call units() on rising edge when button pressed 00169 unitSW.mode(PullUp); 00170 unitSW.rise(&units); 00171 00172 /* 00173 /// switch to radar display 00174 radarDist.rise(&radar); 00175 /// switch to distance reader in cm 00176 radarDist.fall(¢imetre); 00177 */ 00178 00179 /// Device welcome messages - print strings of characters at defined coordinates 00180 lcd.printString("Radar/Distance",0,1); 00181 lcd.printString("Measuring",15,3); 00182 lcd.printString("tool",30,5); 00183 wait(2); /// hold message for 2 secs 00184 lcd.refresh(); /// clear screen 00185 00186 lcd.printString("A project",15,1); 00187 lcd.printString("by",35,3); 00188 lcd.printString("Karina Sodipo",3,5); 00189 wait(2); 00190 lcd.refresh(); 00191 00192 lcd.printString("University",10,1); 00193 lcd.printString("of Leeds",15,3); 00194 lcd.printString("ELEC2645",15,5); 00195 wait(2); 00196 lcd.refresh(); 00197 00198 /// display a checkerboard pattern to illustrate transition from start-up to main device functionality 00199 checkerboard(); 00200 wait(2); 00201 lcd.clear(); /// clear the buffer 00202 lcd.refresh(); 00203 00204 while(1) { /// loop infinitely 00205 00206 brightness(); 00207 00208 initMeasureIn(); 00209 00210 initDisplay(); 00211 00212 } 00213 00214 } 00215 00216 00217 00218 /** 00219 @see - University of Leeds ELEC1620 Digital Electronics & Microcontrollers lecture slides 00220 */ 00221 /// function to display the checkerboard pattern 00222 void checkerboard() { 00223 for (int i = 0; i < 84; i+=2) { 00224 for (int j = 0; j < 48; j+=2) { 00225 lcd.setPixel(i,j); 00226 } 00227 } 00228 lcd.refresh(); 00229 } 00230 00231 00232 00233 /// function to indicate object proximity on LED and buzzer in CM 00234 /// Buzzer counts down to sensor update 00235 /// LED and buzzer indicate when proximity is < 50 cm 00236 void proximityCm() { 00237 00238 int distance = sensor.getDistanceCm(); 00239 00240 /// beep at max volume (dutycycle = 50%) for 3 sec 00241 if (distance < 50) { 00242 led = 1; 00243 buzzer = 0.5; 00244 wait(0.5); 00245 led = 0; 00246 buzzer = 0; 00247 wait(0.25); 00248 led = 1; 00249 buzzer = 0.5; 00250 wait(0.5); 00251 led = 0; 00252 buzzer = 0; 00253 wait(0.25); 00254 led = 1; 00255 buzzer = 0.5; 00256 wait(0.5); 00257 led = 0; 00258 buzzer = 0; 00259 wait(0.25); 00260 led = 1; 00261 buzzer = 0.5; 00262 wait(0.5); 00263 led = 0; 00264 buzzer = 0; 00265 wait(0.25); 00266 lcd.refresh(); 00267 } 00268 /// quieter buzzer countdown to next update 00269 else { 00270 buzzer = 1; 00271 wait(0.5); 00272 buzzer = 0; 00273 wait(0.5); 00274 buzzer = 1; 00275 wait(0.5); 00276 buzzer = 0; 00277 wait(0.5); 00278 buzzer = 1; 00279 wait(0.5); 00280 buzzer = 0; 00281 wait(0.5); 00282 lcd.refresh(); 00283 } 00284 00285 } 00286 00287 00288 00289 /// function to indicate object proximity on LED and buzzer in INCHES 00290 /// Buzzer counts down to sensor update 00291 /// LED and buzzer indicate when proximity is < 20 inches (~ 50 cm) 00292 void proximityIn() { 00293 00294 int distance = sensor.getDistanceInch(); 00295 00296 /// beep at max volume (dutycycle = 50%) for 3 sec 00297 if (distance < 20) { 00298 led = 1; 00299 buzzer = 0.5; 00300 wait(0.5); 00301 led = 0; 00302 buzzer = 0; 00303 wait(0.25); 00304 led = 1; 00305 buzzer = 0.5; 00306 wait(0.5); 00307 led = 0; 00308 buzzer = 0; 00309 wait(0.25); 00310 led = 1; 00311 buzzer = 0.5; 00312 wait(0.5); 00313 led = 0; 00314 buzzer = 0; 00315 wait(0.25); 00316 led = 1; 00317 buzzer = 0.5; 00318 wait(0.5); 00319 led = 0; 00320 buzzer = 0; 00321 wait(0.25); 00322 lcd.refresh(); 00323 } 00324 /// quieter buzzer countdown to next update 00325 else { 00326 buzzer = 1; 00327 wait(0.5); 00328 buzzer = 1; 00329 wait(0.5); 00330 buzzer = 1; 00331 wait(0.5); 00332 buzzer = 1; 00333 wait(0.5); 00334 buzzer = 1; 00335 wait(0.5); 00336 buzzer = 1; 00337 wait(0.5); 00338 /// clear screen and buffer 00339 lcd.clear(); 00340 } 00341 00342 } 00343 00344 00345 00346 /// function to change units of readings: 0 - centimetres, 1 - inches 00347 void initMeasureIn() { 00348 00349 if (unit == 0) { 00350 /// switch LED4 on to indicate unit 00351 leds = 8; 00352 centimetre(); 00353 } 00354 00355 if (unit == 1) { 00356 /// switch LED2 on to indicate unit 00357 leds = 2; 00358 inches(); 00359 } 00360 00361 } 00362 00363 00364 00365 /// function to change display modes: 0 - normal display, 1 - inverted display 00366 void initDisplay() { 00367 00368 if (pixel == 0) { 00369 normal(); 00370 } 00371 00372 if (pixel == 1) { 00373 invert(); 00374 } 00375 00376 } 00377 00378 00379 00380 /// function to control screen backlight brightness 00381 void brightness() { 00382 lcd.setBrightness(POT); 00383 } 00384 00385 00386 00387 /// function to set pixel display as black-on-white 00388 void normal() { 00389 lcd.normalMode(); 00390 } 00391 00392 00393 00394 /// function to set pixel display as white-on-black 00395 void invert() { 00396 lcd.inverseMode(); 00397 } 00398 00399 00400 00401 /// function to read distances in centimetres 00402 void centimetre() { 00403 00404 int distance = sensor.getDistanceCm(); 00405 /// Print the formatted output from the sensor 00406 int length = sprintf(buffer,"D = %d cm",distance); 00407 00408 /// Only allow data with length < or = 24 to be displayed 00409 if (length<= 14) 00410 lcd.printString(buffer,0,3); 00411 proximityCm(); 00412 /// short delay before next measurement 00413 wait(0.5); 00414 00415 } 00416 00417 00418 00419 /// function to read distances in inches 00420 void inches() { 00421 00422 int distance = sensor.getDistanceInch(); 00423 /// Print the formatted output from the sensor 00424 int length = sprintf(buffer,"D = %d inches",distance); 00425 00426 if (length<= 14) 00427 lcd.printString(buffer,0,3); 00428 proximityIn(); 00429 /// short delay before next measurement 00430 wait(0.5); 00431 00432 } 00433 00434 00435 00436 /// function for radar display - not called because distance reading feature takes precedence in while loop 00437 /// Attempt at using a switch statement unsuccessful 00438 void radar() { 00439 00440 lcd.clear(); 00441 00442 int distance = sensor.getDistanceCm(); 00443 00444 /// Assume max detectable distance is 240 cm. 00445 /// Dividing by 48 pixels across screen gives 5cm for every pixel. 00446 /// Set each pixel along the centre of the screen (x = 42) to 1 00447 /// for distance increases by 5 cm. 00448 /// Set any distance above 230cm to final pixel (41,48). 00449 if (distance < 5) { 00450 lcd.setPixel(42,1); 00451 } 00452 else if (distance < 10) { 00453 lcd.setPixel(42,2); 00454 } 00455 else if (distance < 15) { 00456 lcd.setPixel(42,3); 00457 } 00458 else if (distance < 20) { 00459 lcd.setPixel(42,4); 00460 } 00461 else if (distance < 25) { 00462 lcd.setPixel(42,5); 00463 } 00464 else if (distance < 30) { 00465 lcd.setPixel(42,6); 00466 } 00467 else if (distance < 35) { 00468 lcd.setPixel(42,7); 00469 } 00470 else if (distance < 40) { 00471 lcd.setPixel(42,8); 00472 } 00473 else if (distance < 45) { 00474 lcd.setPixel(42,9); 00475 } 00476 else if (distance < 50) { 00477 lcd.setPixel(42,10); 00478 } 00479 else if (distance < 55) { 00480 lcd.setPixel(42,11); 00481 } 00482 else if (distance < 60) { 00483 lcd.setPixel(42,12); 00484 } 00485 else if (distance < 65) { 00486 lcd.setPixel(42,13); 00487 } 00488 else if (distance < 70) { 00489 lcd.setPixel(42,14); 00490 } 00491 else if (distance < 75) { 00492 lcd.setPixel(42,15); 00493 } 00494 else if (distance < 80) { 00495 lcd.setPixel(42,16); 00496 } 00497 else if (distance < 85) { 00498 lcd.setPixel(42,17); 00499 } 00500 else if (distance < 90) { 00501 lcd.setPixel(42,18); 00502 } 00503 else if (distance < 95) { 00504 lcd.setPixel(42,19); 00505 } 00506 else if (distance < 100) { 00507 lcd.setPixel(42,20); 00508 } 00509 else if (distance < 105) { 00510 lcd.setPixel(42,21); 00511 } 00512 else if (distance < 110) { 00513 lcd.setPixel(42,22); 00514 } 00515 else if (distance < 115) { 00516 lcd.setPixel(42,23); 00517 } 00518 else if (distance < 120) { 00519 lcd.setPixel(42,24); 00520 } 00521 else if (distance < 125) { 00522 lcd.setPixel(42,25); 00523 } 00524 else if (distance < 130) { 00525 lcd.setPixel(42,26); 00526 } 00527 else if (distance < 135) { 00528 lcd.setPixel(42,27); 00529 } 00530 else if (distance < 140) { 00531 lcd.setPixel(42,28); 00532 } 00533 else if (distance < 145) { 00534 lcd.setPixel(42,29); 00535 } 00536 else if (distance < 150) { 00537 lcd.setPixel(42,30); 00538 } 00539 else if (distance < 155) { 00540 lcd.setPixel(42,31); 00541 } 00542 else if (distance < 160) { 00543 lcd.setPixel(42,32); 00544 } 00545 else if (distance < 165) { 00546 lcd.setPixel(42,33); 00547 } 00548 else if (distance < 170) { 00549 lcd.setPixel(42,34); 00550 } 00551 else if (distance < 175) { 00552 lcd.setPixel(42,35); 00553 } 00554 else if (distance < 180) { 00555 lcd.setPixel(42,36); 00556 } 00557 else if (distance < 185) { 00558 lcd.setPixel(42,37); 00559 } 00560 else if (distance < 190) { 00561 lcd.setPixel(42,38); 00562 } 00563 else if (distance < 195) { 00564 lcd.setPixel(42,39); 00565 } 00566 else if (distance < 200) { 00567 lcd.setPixel(42,40); 00568 } 00569 else if (distance < 205) { 00570 lcd.setPixel(42,41); 00571 } 00572 else if (distance < 210) { 00573 lcd.setPixel(42,42); 00574 } 00575 else if (distance < 215) { 00576 lcd.setPixel(42,43); 00577 } 00578 else if (distance < 220) { 00579 lcd.setPixel(42,44); 00580 } 00581 else if (distance < 225) { 00582 lcd.setPixel(42,45); 00583 } 00584 else if (distance < 230) { 00585 lcd.setPixel(42,46); 00586 } 00587 else { 00588 lcd.setPixel(42,47); 00589 } 00590 wait(3); 00591 lcd.refresh(); 00592 }
Generated on Thu Jul 21 2022 14:24:59 by 1.7.2