Level 2 Project Range Device

Dependencies:   N5110 SDFileSystem SRF02 TMP102 mbed

Fork of Ranger by Philip Thompson

main.cpp

Committer:
el15pjt
Date:
2016-03-22
Revision:
1:5b991c2302e1
Parent:
0:0b180348c7e4
Child:
2:329597081c06

File content as of revision 1:5b991c2302e1:

/*
Philip Thompsonn
EL15PJT
Embedded System Project
EL2645


 Have added the N5110 Libary and set up a a basic disply of the Range reading
 as well as added LED indictors to range
*/


#include "mbed.h"
#include "SRF02.h"
#include "N5110.h"

BusOut output(PTB2,PTB3,PTB10,PTB11,PTC11,PTC10);

// Ranger object
SRF02 srf02(I2C_SDA,I2C_SCL);

// UART connection for PC
Serial pc(USBTX,USBRX);

//N5110 Object  VCC, SCE,RST, D/C, MOSI,SCLK, LED
N5110           lcd(PTE26,PTA0,PTC4,PTD0,PTD2,PTD1,PTC3);

// K64F on-board LEDs
DigitalOut r_led(LED_RED);
DigitalOut g_led(LED_GREEN);
DigitalOut b_led(LED_BLUE);

// K64F on-board switches
InterruptIn sw2(SW2);
InterruptIn sw3(SW3);

int distance;
int length;
int alert

// setup serial port
void init_serial();

// set up board
void init_K64F();

char buffer[14];  // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)

int main()
{
    lcd.init();
    init_serial();
    init_K64F();


    while(1) {

        // read sensor distance in cm and print over serial port
        int distance = srf02.getDistanceCm();

        //serial link reading of range
        pc.printf("Distance = %i Cm\n",distance);

        // short delay before next measurement
        wait(0.25);

        length = sprintf(buffer,"D = %i Cm",distance);

        if (length <= 14)

            lcd.printString(buffer,0,2);
        // need to refresh display after setting pixels
        lcd.refresh();

//Range Alert selection
        if (distance <= 200) {
            alert = 6;
        } else if (150 <= distance  < 200) {
            alert = 6;
        } else if (100 <=distance < 150) {
            alert = 5;
        } else if (50 <= distance < 100) {
            alert = 4;
        } else if ( 10 <=distance < 150) {
            alert = 3;
        } else if (distance <10) {
            alert = 2;
        } else {
            output = 1;
        }
    }

}


// Used to return a range readig thru a serial connection
    void init_serial() {
        // set to highest baud - ensure terminal software matches
        pc.baud(115200);
    }


//Set up board switches and LEDS
    void init_K64F() {
//on-board LEDs are active-low, so set pin high to turn them off.
        r_led = 1;
        g_led = 1;
        b_led = 1;

// since the on-board switches have external pull-ups, we should disable the internal pull-down
// resistors that are enabled by default using InterruptIn
        sw2.mode(PullNone);
        sw3.mode(PullNone);
    }