Level 2 Project Range Device

Dependencies:   N5110 SDFileSystem SRF02 TMP102 mbed

Fork of Ranger by Philip Thompson

Committer:
el15pjt
Date:
Tue Mar 22 20:00:59 2016 +0000
Revision:
1:5b991c2302e1
Parent:
0:0b180348c7e4
Child:
2:329597081c06
alert veriable set

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el15pjt 0:0b180348c7e4 1 /*
el15pjt 0:0b180348c7e4 2 Philip Thompsonn
el15pjt 0:0b180348c7e4 3 EL15PJT
el15pjt 0:0b180348c7e4 4 Embedded System Project
el15pjt 0:0b180348c7e4 5 EL2645
el15pjt 1:5b991c2302e1 6
el15pjt 1:5b991c2302e1 7
el15pjt 0:0b180348c7e4 8 Have added the N5110 Libary and set up a a basic disply of the Range reading
el15pjt 0:0b180348c7e4 9 as well as added LED indictors to range
el15pjt 0:0b180348c7e4 10 */
el15pjt 0:0b180348c7e4 11
el15pjt 0:0b180348c7e4 12
el15pjt 0:0b180348c7e4 13 #include "mbed.h"
el15pjt 0:0b180348c7e4 14 #include "SRF02.h"
el15pjt 0:0b180348c7e4 15 #include "N5110.h"
el15pjt 0:0b180348c7e4 16
el15pjt 0:0b180348c7e4 17 BusOut output(PTB2,PTB3,PTB10,PTB11,PTC11,PTC10);
el15pjt 0:0b180348c7e4 18
el15pjt 0:0b180348c7e4 19 // Ranger object
el15pjt 0:0b180348c7e4 20 SRF02 srf02(I2C_SDA,I2C_SCL);
el15pjt 0:0b180348c7e4 21
el15pjt 0:0b180348c7e4 22 // UART connection for PC
el15pjt 0:0b180348c7e4 23 Serial pc(USBTX,USBRX);
el15pjt 0:0b180348c7e4 24
el15pjt 0:0b180348c7e4 25 //N5110 Object VCC, SCE,RST, D/C, MOSI,SCLK, LED
el15pjt 0:0b180348c7e4 26 N5110 lcd(PTE26,PTA0,PTC4,PTD0,PTD2,PTD1,PTC3);
el15pjt 0:0b180348c7e4 27
el15pjt 0:0b180348c7e4 28 // K64F on-board LEDs
el15pjt 0:0b180348c7e4 29 DigitalOut r_led(LED_RED);
el15pjt 0:0b180348c7e4 30 DigitalOut g_led(LED_GREEN);
el15pjt 0:0b180348c7e4 31 DigitalOut b_led(LED_BLUE);
el15pjt 0:0b180348c7e4 32
el15pjt 0:0b180348c7e4 33 // K64F on-board switches
el15pjt 0:0b180348c7e4 34 InterruptIn sw2(SW2);
el15pjt 0:0b180348c7e4 35 InterruptIn sw3(SW3);
el15pjt 0:0b180348c7e4 36
el15pjt 0:0b180348c7e4 37 int distance;
el15pjt 0:0b180348c7e4 38 int length;
el15pjt 1:5b991c2302e1 39 int alert
el15pjt 0:0b180348c7e4 40
el15pjt 0:0b180348c7e4 41 // setup serial port
el15pjt 0:0b180348c7e4 42 void init_serial();
el15pjt 1:5b991c2302e1 43
el15pjt 0:0b180348c7e4 44 // set up board
el15pjt 0:0b180348c7e4 45 void init_K64F();
el15pjt 0:0b180348c7e4 46
el15pjt 0:0b180348c7e4 47 char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
el15pjt 0:0b180348c7e4 48
el15pjt 0:0b180348c7e4 49 int main()
el15pjt 0:0b180348c7e4 50 {
el15pjt 0:0b180348c7e4 51 lcd.init();
el15pjt 0:0b180348c7e4 52 init_serial();
el15pjt 0:0b180348c7e4 53 init_K64F();
el15pjt 1:5b991c2302e1 54
el15pjt 1:5b991c2302e1 55
el15pjt 0:0b180348c7e4 56 while(1) {
el15pjt 0:0b180348c7e4 57
el15pjt 0:0b180348c7e4 58 // read sensor distance in cm and print over serial port
el15pjt 0:0b180348c7e4 59 int distance = srf02.getDistanceCm();
el15pjt 1:5b991c2302e1 60
el15pjt 0:0b180348c7e4 61 //serial link reading of range
el15pjt 0:0b180348c7e4 62 pc.printf("Distance = %i Cm\n",distance);
el15pjt 1:5b991c2302e1 63
el15pjt 0:0b180348c7e4 64 // short delay before next measurement
el15pjt 0:0b180348c7e4 65 wait(0.25);
el15pjt 0:0b180348c7e4 66
el15pjt 0:0b180348c7e4 67 length = sprintf(buffer,"D = %i Cm",distance);
el15pjt 0:0b180348c7e4 68
el15pjt 0:0b180348c7e4 69 if (length <= 14)
el15pjt 0:0b180348c7e4 70
el15pjt 0:0b180348c7e4 71 lcd.printString(buffer,0,2);
el15pjt 0:0b180348c7e4 72 // need to refresh display after setting pixels
el15pjt 0:0b180348c7e4 73 lcd.refresh();
el15pjt 0:0b180348c7e4 74
el15pjt 1:5b991c2302e1 75 //Range Alert selection
el15pjt 1:5b991c2302e1 76 if (distance <= 200) {
el15pjt 1:5b991c2302e1 77 alert = 6;
el15pjt 1:5b991c2302e1 78 } else if (150 <= distance < 200) {
el15pjt 1:5b991c2302e1 79 alert = 6;
el15pjt 1:5b991c2302e1 80 } else if (100 <=distance < 150) {
el15pjt 1:5b991c2302e1 81 alert = 5;
el15pjt 1:5b991c2302e1 82 } else if (50 <= distance < 100) {
el15pjt 1:5b991c2302e1 83 alert = 4;
el15pjt 1:5b991c2302e1 84 } else if ( 10 <=distance < 150) {
el15pjt 1:5b991c2302e1 85 alert = 3;
el15pjt 1:5b991c2302e1 86 } else if (distance <10) {
el15pjt 1:5b991c2302e1 87 alert = 2;
el15pjt 1:5b991c2302e1 88 } else {
el15pjt 1:5b991c2302e1 89 output = 1;
el15pjt 0:0b180348c7e4 90 }
el15pjt 0:0b180348c7e4 91 }
el15pjt 1:5b991c2302e1 92
el15pjt 0:0b180348c7e4 93 }
el15pjt 0:0b180348c7e4 94
el15pjt 1:5b991c2302e1 95
el15pjt 0:0b180348c7e4 96 // Used to return a range readig thru a serial connection
el15pjt 1:5b991c2302e1 97 void init_serial() {
el15pjt 1:5b991c2302e1 98 // set to highest baud - ensure terminal software matches
el15pjt 1:5b991c2302e1 99 pc.baud(115200);
el15pjt 1:5b991c2302e1 100 }
el15pjt 1:5b991c2302e1 101
el15pjt 1:5b991c2302e1 102
el15pjt 0:0b180348c7e4 103 //Set up board switches and LEDS
el15pjt 1:5b991c2302e1 104 void init_K64F() {
el15pjt 0:0b180348c7e4 105 //on-board LEDs are active-low, so set pin high to turn them off.
el15pjt 1:5b991c2302e1 106 r_led = 1;
el15pjt 1:5b991c2302e1 107 g_led = 1;
el15pjt 1:5b991c2302e1 108 b_led = 1;
el15pjt 0:0b180348c7e4 109
el15pjt 0:0b180348c7e4 110 // since the on-board switches have external pull-ups, we should disable the internal pull-down
el15pjt 0:0b180348c7e4 111 // resistors that are enabled by default using InterruptIn
el15pjt 1:5b991c2302e1 112 sw2.mode(PullNone);
el15pjt 1:5b991c2302e1 113 sw3.mode(PullNone);
el15pjt 1:5b991c2302e1 114 }