ELEC2645 Sensor Project Andrew Ruffley 200887845
Dependencies: SDFileSystem SRF02 mbed
Revision 0:71f4b2285994, committed 2016-05-05
- Comitter:
- Ruffley
- Date:
- Thu May 05 11:31:50 2016 +0000
- Commit message:
- Final Code
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/N5110.lib Thu May 05 11:31:50 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/Ruffley/code/N5110/#86b2ddebac90
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDFileSystem.lib Thu May 05 11:31:50 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/SDFileSystem/#7b35d1709458
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SRF02.lib Thu May 05 11:31:50 2016 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/eencae/code/SRF02/#8e6587d88773
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu May 05 11:31:50 2016 +0000 @@ -0,0 +1,198 @@ +#include "mbed.h" +#include "SRF02.h" +#include "N5110.h" +#include "SDFileSystem.h" +/** ELEC2645 Project +*Finalised Project for submission +*/ + +// VCC, SCE, RST, D/C, MOSI, SCLK, LED +N5110 lcd (PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3); // mbed pins +SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS, SD Card Connections +SRF02 Sensor(PTE25,PTE24);//Distance Sensor + +// External LED's +DigitalOut LEDY(PTC5); +DigitalOut LEDG(PTC7); + +// 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); + +// External Switches +DigitalIn Sw1(PTB19); +DigitalIn Sw2(PTB18); + +//Buzzer +PwmOut buzzer(PTA2); + +void D_Sensor();//Timer and Sensor Function +void delete_file(char filename[]); //Delete Save file +void init_K64F();// set-up the on-board LEDs and switches +void Buzzer(); //Buzzer +void Parking_Sensor();//Parking Sensor Program +char buffer[14]; + +Timer timer; //Initiate timer +int T;//Create time variable + +int main() + +{ + Sw1.mode(PullDown); + Sw2.mode(PullDown); + + init_K64F();//initialise ob-board LED's and switches + lcd.init();//initialise screen + lcd.refresh(); //refresh + + lcd.printString("ELEC 2645",15,1); + lcd.printString("Sensor",24,2); + lcd.printString("Andy Ruffley",7,4); + lcd.printString("200887845",16,5); + wait(3); + lcd.clear(); + + lcd.printString("Program Select",0,0); + lcd.printString("Parking Sensor",0,2); + lcd.printString("Motion Sensor",3,3); + lcd.printString("200887845",16,5); + + + while(1) { + + int program = Sw1+Sw2+Sw2; + switch (program) { + + case 1: + lcd.clear(); + lcd.printString("Parking Sensor",0,2); + lcd.refresh(); + wait(2); + lcd.clear(); + while(1) { + Parking_Sensor(); + } + + + case 2: + lcd.clear(); + lcd.printString("Motion Sensor",3,3); + lcd.refresh(); + wait(2); + lcd.clear(); + while(1) { + D_Sensor(); + } + } + } +} +//End of programme + + + +//Start of functions + +//Program 1 +void Parking_Sensor() +{ + int Array[5]; //Create an array + int Total = 0; //Create and set a variable + int count = 0; //Create and set a variable + int Distance; //Create a variable + + for(count = 0; count < 5; count++) { //Set limits + Array[count] = Sensor.getDistanceCm(); //Take sensor readings + Total = Total + Array[count]; //Add sensor readings to variable + } + Distance = Total/5; // Calculate average + lcd.drawRect(0,0,5,47,2);//Draw white fill rectangle + lcd.drawRect(0,0,5,(Distance/4.3),1); //Draw black fill rectangle + float distance = sprintf(buffer,"%d cm",Distance); + if (distance <=14) { + lcd.printString(buffer,10,1); //print reading + // short delay before next measurement + wait(0.5); + } + if (Distance <30) { + Buzzer();//Activate Buzzer function + LEDY=1; + LEDG=0; + } else { + buzzer = 0;//Turn off buzzer + LEDY=0; + LEDG=1; + } +} + +//program 2 +void D_Sensor() +{ + int Distance; + Distance = Sensor.getDistanceCm();//reads distance + + float distance = sprintf(buffer,"%d cm",Distance); + if (distance <=14) { + lcd.printString(buffer,10,1);//prints distance + // short delay before next measurement + wait(0.5); + } + timer.start(); + T = timer.read(); + float Time_E = sprintf(buffer,"%d S",T); + if (Time_E <=14) { + lcd.printString(buffer,7,2); //prints time + } + if (Distance<50) { + FILE *fp; // this is our file pointer + delete_file("/sd/Time_Log.txt"); + int Time = T;//Creates Variable and sets to T + fp = fopen("/sd/Time_Log.txt", "a"); + lcd.printString("Saved",15,3); + fprintf(fp, "%d",Time); // ensure data type matches + fclose(fp); // ensure you close the file after writing + } +} + + + +//Extra Functions + +void Buzzer() +{ + buzzer.period_ms(1); //1KHz frequency + buzzer = 0.05; //50% Duty cycle + wait(1); + buzzer.period_ms(2); //500Hz frequency +} + +void delete_file(char filename[]) +{ + + FILE *fp = fopen(filename, "r"); // try and open file + if (fp != NULL) { // if it does open... + fclose(fp); // close it + remove(filename); // and then delete + + } + // if we can't open it, it doesn't exist and so we can't delete it +} + +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); + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.h Thu May 05 11:31:50 2016 +0000 @@ -0,0 +1,138 @@ +/** +@file main.h +@brief Header file for functions, defines and global variables. + +@author Andrew Ruffley 200887845 +@brief University of Leeds ELEC2645 Project +@date May 2015 +*/ + +#ifndef MAIN_H +#define MAIN_H + +#include "mbed.h" +#include "SRF02.h" +#include "N5110.h" +#include "SDFileSystem.h" + + +// --------------- INPUT PERIPHERALS -------------- + + +/** +@namespace sensor +@brief Ultra-Sonic distance sensor requires SDA and SCL lines +*/ +SRF02 sensor(PTE25, PTE24); + +/** +@namespace Sw1 +@brief Digital input button that is used to select program +*/ +DigitalIn Sw1(PTB19); + +/** +@namespace Sw2 +@brief Digital input button that is used to select program +*/ +DigitalIn Sw2(PTB18); + + +// -------------- OUTPUT PERIPHERALS -------------- + +/** +@namespace lcd +@brief Outputs for the N5110 lcd display, lcd (VCC,SCE,RST,D/C,MOSI,SCLK,LED) +*/ +N5110 lcd(PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3); + +/** +@namespace sd +@brief Outputs for the sd card, sd(PTE3, PTE1, PTE2, PTE4, "sd") +*/ +SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS, SD Card Connections + +/** +@namespace Buzzer +@brief A buzzer that can produce different notes dependant on the frquency of the signal inputted to it. Used as an auditory alert +*/ +PwmOut buzzer(PTA2); + +/** +@namespace led_Yellow +@brief Yellow indicator LED +*/ +DigitalOut LEDY(PTC5); + +/** +@namespace led_Green +@brief Green indicator LED +*/ +DigitalOut LEDG(PTC7); + +/** +@namespace r_led +@brief K64F on-board LED +*/ +DigitalOut r_led(LED_RED); + +/** +@namespace g_led +@brief K64F on-board LED +*/ +DigitalOut g_led(LED_GREEN); + +/** +@namespace b_led +@brief K64F on-board LED +*/ +DigitalOut b_led(LED_BLUE); + + +// -------------- VARIABLES -------------- + +int Array[5]; /*!<Creates an array */ +int Total = 0; /*!<Creates and set a variable */ +int count = 0; /*!<Creates and set a variable */ +int Distance; /*!<Creates a variable */ +int T; /*!<Creates variable */ +int Time = T; /*!<Creates Variable and sets to T */ + + +// -------------- Functions -------------- + +/** +@namespace Parking_Sensor +@brief Parking Sensor Program +@brief Calculates average distance and creates graphic reprisentation +@brief Turns on buzzer if distance drops below 30cm +*/ +void Parking_Sensor(); + +/** +@namespace D_Sensor +@brief Timer and Sensor Function +@brief Creates timer and reads distance +@brief If distance drops below 50cm saves time to sd +*/ +void D_Sensor(); + +/** +@namespace delete_file +@brief Delete Save file +*/ +void delete_file(char filename[]); + +/** +@namespace init_K64F +@brief set-up the on-board LEDs and switches +*/ +void init_K64F(); + +/** +@namespace Buzzer +@brief Buzzer control +*/ +void Buzzer(); //Buzzer + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu May 05 11:31:50 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/aae6fcc7d9bb \ No newline at end of file