Car safety monitoring system, monitoring distance, light and temperature.

Dependencies:   Speaker mbed mbed-rtos 4DGL-uLCD-SE hcsr04

Files at this revision

API Documentation at this revision

Comitter:
tadhgjones
Date:
Sun Dec 20 20:20:26 2020 +0000
Parent:
14:0bbfa2c1de62
Commit message:
Final revision of EE3023 Design Project

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 0bbfa2c1de62 -r d39446966d94 main.cpp
--- a/main.cpp	Sat Dec 19 19:04:00 2020 +0000
+++ b/main.cpp	Sun Dec 20 20:20:26 2020 +0000
@@ -2,44 +2,50 @@
 #include "hcsr04.h"
 #include "Speaker.h"
 #include "uLCD_4DGL.h"
-//temp
+
+//Temperature Inputs
 AnalogIn ain(p15);
 AnalogIn ain2(p16);
-//dist
+
+//Distance Inputs
 HCSR04  distSens(p25,p6);
 Speaker mySpeaker(p18);
-//general
+
+//Initialisation of LCD and Serial Monitor
 Serial pc(USBTX, USBRX);
 uLCD_4DGL uLCD(p9,p10,p11);
-//autolights
+
+//Autolights Inputs
 AnalogIn photodiode(p20);
 PwmOut myled(p21);
-//distance
+
+//Distance
 unsigned int dist;
 float averageDistance();
 void beeping();
 float i;
 float x = 0;
-//temp
+
+//Temperature
 void Temperature();
 void TempConstraints();
 float getTemp();
 float avgTemp();
 float getTemp2();
 float avgTemp2();
- 
 float voltage_in;
 float degrees_c;
 float avgT;
 float Temp;
 float a;
 float b;
-//autolights
+
+//Autolights
 void LED();
-
+ 
 int main()
 {
-    //dist
+    //distance
     uLCD.text_width(1);
     uLCD.text_height(1);
     uLCD.locate(10, 12);
@@ -64,6 +70,7 @@
         LED();
     }
 }
+//measure average distance from sensor
 float averageDistance()
 {
         int a = distSens.get_dist_cm();
@@ -80,6 +87,7 @@
         dist  = (a+b+c+d+e+f+g+h+i+j)/10;
         return dist;
 }
+//distance parameters and output
 void beeping()
 {
     int distance = averageDistance();
@@ -99,48 +107,53 @@
     if (distance < 5) i=1.2;
     mySpeaker.PlayNote(700*i, 0.2, 2*i);  
 }
+//measure and output temperature
 void Temperature(){
-    
+    uLCD.locate(1,1);
+    uLCD.printf("Temperature:\n\n");
     a = avgTemp();
     b = avgTemp2();
-    TempConstraints();
-    Temp = ((a + b)/2)-7;
+    Temp = ((a + b)/2)-15;
+    uLCD.text_width(2);
+    uLCD.text_height(2);
+    uLCD.printf("%3.1f\t", Temp);
     uLCD.text_width(1);
     uLCD.text_height(1);
-    uLCD.locate(1,1);
-    uLCD.printf("%3.1f deg C\n\r", Temp);
-    
+    uLCD.printf("o");
+    uLCD.text_width(2);
+    uLCD.text_height(2);
+    uLCD.printf("C");
+    uLCD.text_width(1);
+    uLCD.text_height(1);
+    TempConstraints();
 }
- 
+ //signal sensor fault or low temperature
 void TempConstraints() {
- 
     if(degrees_c <= 4){
         uLCD.text_width(1);
         uLCD.text_height(1);
-        uLCD.locate(1,1);
-        pc.printf("WARNING\nCOLD TEMPERATURES\nICE MAY BE PRESENT");
+        uLCD.locate(1,5);
+        pc.printf("ICE WARNING");
     }
     if(abs(a-b) > 4){
         uLCD.text_width(1);
         uLCD.text_height(1);
-        uLCD.locate(1,1);
-        uLCD.printf("WARNING\nTEMPERATURE SENSOR\nFAULT\n");
+        uLCD.locate(1,5);
+        uLCD.printf("TEMPERATURE FAULT");
     }
 }  
- 
+//convert sensor outut to temperature
 float getTemp(){
     voltage_in = ain * 3.3;
     degrees_c = ((voltage_in - 0.5) * 100.0);
     return degrees_c;
 }
- 
 float getTemp2(){
     voltage_in = ain2 * 3.3;
     degrees_c = ((voltage_in - 0.5) * 100.0);
     return degrees_c;
 }
- 
-//Thsi function obtaines the voltage from the first sensor
+//Thsi function obtaines the average voltage from the first sensor
 float avgTemp(){
     float a = getTemp();
     float b = getTemp();
@@ -163,14 +176,9 @@
     float s = getTemp();
     float t = getTemp();
     
-    //The function is called to get 20 Values of the required voltage in order
-    //to acieve an accurate resuilt and eliminate outlier effects
     avgT = (a+b+c+d+e+f+g+h+i+j+k+l+m+n+o+p+q+r+s+t)/20;
-    
     return avgT;
 }
- 
-//this function obtaines the voltage from the second sensor
 float avgTemp2(){
     float a = getTemp2();
     float b = getTemp2();
@@ -194,19 +202,27 @@
     float t = getTemp2();
     
     avgT = (a+b+c+d+e+f+g+h+i+j+k+l+m+n+o+p+q+r+s+t)/20;
-    
     return avgT;
 }
+//check LED and turn on and off lights as required
 void LED(){
-    
     if (photodiode*100 <= 27){
         myled = 1; 
-        uLCD.printf("\nautolights ON ");  
+        uLCD.locate(1,7);
+        uLCD.printf("Autolights:\n\n"); 
+        uLCD.text_width(2);
+        uLCD.text_height(2);
+        uLCD.printf("ON ");
+        uLCD.text_width(1);
+        uLCD.text_height(1);
     } else {
         myled = 0;
-                uLCD.printf("\nautolights OFF");  
-
-    }
-    
-    
+        uLCD.locate(1,7);
+        uLCD.printf("Autolights:\n\n"); 
+        uLCD.text_width(2);
+        uLCD.text_height(2);
+        uLCD.printf("OFF");
+        uLCD.text_width(1);
+        uLCD.text_height(1);
+    } 
 }
\ No newline at end of file