ELEC2645 Sensor Project Andrew Ruffley 200887845

Dependencies:   SDFileSystem SRF02 mbed

Committer:
Ruffley
Date:
Thu May 05 11:31:50 2016 +0000
Revision:
0:71f4b2285994
Final Code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Ruffley 0:71f4b2285994 1 #include "mbed.h"
Ruffley 0:71f4b2285994 2 #include "SRF02.h"
Ruffley 0:71f4b2285994 3 #include "N5110.h"
Ruffley 0:71f4b2285994 4 #include "SDFileSystem.h"
Ruffley 0:71f4b2285994 5 /** ELEC2645 Project
Ruffley 0:71f4b2285994 6 *Finalised Project for submission
Ruffley 0:71f4b2285994 7 */
Ruffley 0:71f4b2285994 8
Ruffley 0:71f4b2285994 9 // VCC, SCE, RST, D/C, MOSI, SCLK, LED
Ruffley 0:71f4b2285994 10 N5110 lcd (PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3); // mbed pins
Ruffley 0:71f4b2285994 11 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS, SD Card Connections
Ruffley 0:71f4b2285994 12 SRF02 Sensor(PTE25,PTE24);//Distance Sensor
Ruffley 0:71f4b2285994 13
Ruffley 0:71f4b2285994 14 // External LED's
Ruffley 0:71f4b2285994 15 DigitalOut LEDY(PTC5);
Ruffley 0:71f4b2285994 16 DigitalOut LEDG(PTC7);
Ruffley 0:71f4b2285994 17
Ruffley 0:71f4b2285994 18 // K64F on-board LEDs
Ruffley 0:71f4b2285994 19 DigitalOut r_led(LED_RED);
Ruffley 0:71f4b2285994 20 DigitalOut g_led(LED_GREEN);
Ruffley 0:71f4b2285994 21 DigitalOut b_led(LED_BLUE);
Ruffley 0:71f4b2285994 22
Ruffley 0:71f4b2285994 23 // K64F on-board switches
Ruffley 0:71f4b2285994 24 InterruptIn sw2(SW2);
Ruffley 0:71f4b2285994 25 InterruptIn sw3(SW3);
Ruffley 0:71f4b2285994 26
Ruffley 0:71f4b2285994 27 // External Switches
Ruffley 0:71f4b2285994 28 DigitalIn Sw1(PTB19);
Ruffley 0:71f4b2285994 29 DigitalIn Sw2(PTB18);
Ruffley 0:71f4b2285994 30
Ruffley 0:71f4b2285994 31 //Buzzer
Ruffley 0:71f4b2285994 32 PwmOut buzzer(PTA2);
Ruffley 0:71f4b2285994 33
Ruffley 0:71f4b2285994 34 void D_Sensor();//Timer and Sensor Function
Ruffley 0:71f4b2285994 35 void delete_file(char filename[]); //Delete Save file
Ruffley 0:71f4b2285994 36 void init_K64F();// set-up the on-board LEDs and switches
Ruffley 0:71f4b2285994 37 void Buzzer(); //Buzzer
Ruffley 0:71f4b2285994 38 void Parking_Sensor();//Parking Sensor Program
Ruffley 0:71f4b2285994 39 char buffer[14];
Ruffley 0:71f4b2285994 40
Ruffley 0:71f4b2285994 41 Timer timer; //Initiate timer
Ruffley 0:71f4b2285994 42 int T;//Create time variable
Ruffley 0:71f4b2285994 43
Ruffley 0:71f4b2285994 44 int main()
Ruffley 0:71f4b2285994 45
Ruffley 0:71f4b2285994 46 {
Ruffley 0:71f4b2285994 47 Sw1.mode(PullDown);
Ruffley 0:71f4b2285994 48 Sw2.mode(PullDown);
Ruffley 0:71f4b2285994 49
Ruffley 0:71f4b2285994 50 init_K64F();//initialise ob-board LED's and switches
Ruffley 0:71f4b2285994 51 lcd.init();//initialise screen
Ruffley 0:71f4b2285994 52 lcd.refresh(); //refresh
Ruffley 0:71f4b2285994 53
Ruffley 0:71f4b2285994 54 lcd.printString("ELEC 2645",15,1);
Ruffley 0:71f4b2285994 55 lcd.printString("Sensor",24,2);
Ruffley 0:71f4b2285994 56 lcd.printString("Andy Ruffley",7,4);
Ruffley 0:71f4b2285994 57 lcd.printString("200887845",16,5);
Ruffley 0:71f4b2285994 58 wait(3);
Ruffley 0:71f4b2285994 59 lcd.clear();
Ruffley 0:71f4b2285994 60
Ruffley 0:71f4b2285994 61 lcd.printString("Program Select",0,0);
Ruffley 0:71f4b2285994 62 lcd.printString("Parking Sensor",0,2);
Ruffley 0:71f4b2285994 63 lcd.printString("Motion Sensor",3,3);
Ruffley 0:71f4b2285994 64 lcd.printString("200887845",16,5);
Ruffley 0:71f4b2285994 65
Ruffley 0:71f4b2285994 66
Ruffley 0:71f4b2285994 67 while(1) {
Ruffley 0:71f4b2285994 68
Ruffley 0:71f4b2285994 69 int program = Sw1+Sw2+Sw2;
Ruffley 0:71f4b2285994 70 switch (program) {
Ruffley 0:71f4b2285994 71
Ruffley 0:71f4b2285994 72 case 1:
Ruffley 0:71f4b2285994 73 lcd.clear();
Ruffley 0:71f4b2285994 74 lcd.printString("Parking Sensor",0,2);
Ruffley 0:71f4b2285994 75 lcd.refresh();
Ruffley 0:71f4b2285994 76 wait(2);
Ruffley 0:71f4b2285994 77 lcd.clear();
Ruffley 0:71f4b2285994 78 while(1) {
Ruffley 0:71f4b2285994 79 Parking_Sensor();
Ruffley 0:71f4b2285994 80 }
Ruffley 0:71f4b2285994 81
Ruffley 0:71f4b2285994 82
Ruffley 0:71f4b2285994 83 case 2:
Ruffley 0:71f4b2285994 84 lcd.clear();
Ruffley 0:71f4b2285994 85 lcd.printString("Motion Sensor",3,3);
Ruffley 0:71f4b2285994 86 lcd.refresh();
Ruffley 0:71f4b2285994 87 wait(2);
Ruffley 0:71f4b2285994 88 lcd.clear();
Ruffley 0:71f4b2285994 89 while(1) {
Ruffley 0:71f4b2285994 90 D_Sensor();
Ruffley 0:71f4b2285994 91 }
Ruffley 0:71f4b2285994 92 }
Ruffley 0:71f4b2285994 93 }
Ruffley 0:71f4b2285994 94 }
Ruffley 0:71f4b2285994 95 //End of programme
Ruffley 0:71f4b2285994 96
Ruffley 0:71f4b2285994 97
Ruffley 0:71f4b2285994 98
Ruffley 0:71f4b2285994 99 //Start of functions
Ruffley 0:71f4b2285994 100
Ruffley 0:71f4b2285994 101 //Program 1
Ruffley 0:71f4b2285994 102 void Parking_Sensor()
Ruffley 0:71f4b2285994 103 {
Ruffley 0:71f4b2285994 104 int Array[5]; //Create an array
Ruffley 0:71f4b2285994 105 int Total = 0; //Create and set a variable
Ruffley 0:71f4b2285994 106 int count = 0; //Create and set a variable
Ruffley 0:71f4b2285994 107 int Distance; //Create a variable
Ruffley 0:71f4b2285994 108
Ruffley 0:71f4b2285994 109 for(count = 0; count < 5; count++) { //Set limits
Ruffley 0:71f4b2285994 110 Array[count] = Sensor.getDistanceCm(); //Take sensor readings
Ruffley 0:71f4b2285994 111 Total = Total + Array[count]; //Add sensor readings to variable
Ruffley 0:71f4b2285994 112 }
Ruffley 0:71f4b2285994 113 Distance = Total/5; // Calculate average
Ruffley 0:71f4b2285994 114 lcd.drawRect(0,0,5,47,2);//Draw white fill rectangle
Ruffley 0:71f4b2285994 115 lcd.drawRect(0,0,5,(Distance/4.3),1); //Draw black fill rectangle
Ruffley 0:71f4b2285994 116 float distance = sprintf(buffer,"%d cm",Distance);
Ruffley 0:71f4b2285994 117 if (distance <=14) {
Ruffley 0:71f4b2285994 118 lcd.printString(buffer,10,1); //print reading
Ruffley 0:71f4b2285994 119 // short delay before next measurement
Ruffley 0:71f4b2285994 120 wait(0.5);
Ruffley 0:71f4b2285994 121 }
Ruffley 0:71f4b2285994 122 if (Distance <30) {
Ruffley 0:71f4b2285994 123 Buzzer();//Activate Buzzer function
Ruffley 0:71f4b2285994 124 LEDY=1;
Ruffley 0:71f4b2285994 125 LEDG=0;
Ruffley 0:71f4b2285994 126 } else {
Ruffley 0:71f4b2285994 127 buzzer = 0;//Turn off buzzer
Ruffley 0:71f4b2285994 128 LEDY=0;
Ruffley 0:71f4b2285994 129 LEDG=1;
Ruffley 0:71f4b2285994 130 }
Ruffley 0:71f4b2285994 131 }
Ruffley 0:71f4b2285994 132
Ruffley 0:71f4b2285994 133 //program 2
Ruffley 0:71f4b2285994 134 void D_Sensor()
Ruffley 0:71f4b2285994 135 {
Ruffley 0:71f4b2285994 136 int Distance;
Ruffley 0:71f4b2285994 137 Distance = Sensor.getDistanceCm();//reads distance
Ruffley 0:71f4b2285994 138
Ruffley 0:71f4b2285994 139 float distance = sprintf(buffer,"%d cm",Distance);
Ruffley 0:71f4b2285994 140 if (distance <=14) {
Ruffley 0:71f4b2285994 141 lcd.printString(buffer,10,1);//prints distance
Ruffley 0:71f4b2285994 142 // short delay before next measurement
Ruffley 0:71f4b2285994 143 wait(0.5);
Ruffley 0:71f4b2285994 144 }
Ruffley 0:71f4b2285994 145 timer.start();
Ruffley 0:71f4b2285994 146 T = timer.read();
Ruffley 0:71f4b2285994 147 float Time_E = sprintf(buffer,"%d S",T);
Ruffley 0:71f4b2285994 148 if (Time_E <=14) {
Ruffley 0:71f4b2285994 149 lcd.printString(buffer,7,2); //prints time
Ruffley 0:71f4b2285994 150 }
Ruffley 0:71f4b2285994 151 if (Distance<50) {
Ruffley 0:71f4b2285994 152 FILE *fp; // this is our file pointer
Ruffley 0:71f4b2285994 153 delete_file("/sd/Time_Log.txt");
Ruffley 0:71f4b2285994 154 int Time = T;//Creates Variable and sets to T
Ruffley 0:71f4b2285994 155 fp = fopen("/sd/Time_Log.txt", "a");
Ruffley 0:71f4b2285994 156 lcd.printString("Saved",15,3);
Ruffley 0:71f4b2285994 157 fprintf(fp, "%d",Time); // ensure data type matches
Ruffley 0:71f4b2285994 158 fclose(fp); // ensure you close the file after writing
Ruffley 0:71f4b2285994 159 }
Ruffley 0:71f4b2285994 160 }
Ruffley 0:71f4b2285994 161
Ruffley 0:71f4b2285994 162
Ruffley 0:71f4b2285994 163
Ruffley 0:71f4b2285994 164 //Extra Functions
Ruffley 0:71f4b2285994 165
Ruffley 0:71f4b2285994 166 void Buzzer()
Ruffley 0:71f4b2285994 167 {
Ruffley 0:71f4b2285994 168 buzzer.period_ms(1); //1KHz frequency
Ruffley 0:71f4b2285994 169 buzzer = 0.05; //50% Duty cycle
Ruffley 0:71f4b2285994 170 wait(1);
Ruffley 0:71f4b2285994 171 buzzer.period_ms(2); //500Hz frequency
Ruffley 0:71f4b2285994 172 }
Ruffley 0:71f4b2285994 173
Ruffley 0:71f4b2285994 174 void delete_file(char filename[])
Ruffley 0:71f4b2285994 175 {
Ruffley 0:71f4b2285994 176
Ruffley 0:71f4b2285994 177 FILE *fp = fopen(filename, "r"); // try and open file
Ruffley 0:71f4b2285994 178 if (fp != NULL) { // if it does open...
Ruffley 0:71f4b2285994 179 fclose(fp); // close it
Ruffley 0:71f4b2285994 180 remove(filename); // and then delete
Ruffley 0:71f4b2285994 181
Ruffley 0:71f4b2285994 182 }
Ruffley 0:71f4b2285994 183 // if we can't open it, it doesn't exist and so we can't delete it
Ruffley 0:71f4b2285994 184 }
Ruffley 0:71f4b2285994 185
Ruffley 0:71f4b2285994 186 void init_K64F()
Ruffley 0:71f4b2285994 187 {
Ruffley 0:71f4b2285994 188 // on-board LEDs are active-low, so set pin high to turn them off.
Ruffley 0:71f4b2285994 189 r_led = 1;
Ruffley 0:71f4b2285994 190 g_led = 1;
Ruffley 0:71f4b2285994 191 b_led = 1;
Ruffley 0:71f4b2285994 192
Ruffley 0:71f4b2285994 193 // since the on-board switches have external pull-ups, we should disable the internal pull-down
Ruffley 0:71f4b2285994 194 // resistors that are enabled by default using InterruptIn
Ruffley 0:71f4b2285994 195 sw2.mode(PullNone);
Ruffley 0:71f4b2285994 196 sw3.mode(PullNone);
Ruffley 0:71f4b2285994 197
Ruffley 0:71f4b2285994 198 }