semi integrated

Dependencies:   Speaker mbed 4DGL-uLCD-SE hcsr04

Files at this revision

API Documentation at this revision

Comitter:
fiachra
Date:
Thu Dec 17 16:04:27 2020 +0000
Parent:
12:730a95b81992
Commit message:
semi integrated;

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 730a95b81992 -r a3cdbad9daf4 main.cpp
--- a/main.cpp	Wed Dec 16 17:20:46 2020 +0000
+++ b/main.cpp	Thu Dec 17 16:04:27 2020 +0000
@@ -2,36 +2,68 @@
 #include "hcsr04.h"
 #include "Speaker.h"
 #include "uLCD_4DGL.h"
-
+//temp
+AnalogIn ain(p15);
+AnalogIn ain2(p16);
+//dist
 HCSR04  distSens(p25,p6);
+Speaker mySpeaker(p18);
+//general
 Serial pc(USBTX, USBRX);
-Speaker mySpeaker(p18);
 uLCD_4DGL uLCD(p9,p10,p11);
 
+//distance
 unsigned int dist;
 float averageDistance();
 void beeping();
-float i;
+float i = 1000;
+//temp
+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;
+    
 
 int main()
 {
+    //dist
     uLCD.text_width(1);
     uLCD.text_height(1);
     uLCD.locate(10, 12);
     uLCD.color(BLUE);
     uLCD.printf(" Reverse           Sensor");
     uLCD.rectangle(65, 117, 126, 127, 0x0000FF);
+     
     while(1) {
+        i++;
+        //distance
         distSens.start();
         averageDistance();
         beeping();
         uLCD.filled_rectangle(66, 118, 125, 127, 0x000000);
         pc.printf("cm:%d\n",dist );
-        uLCD.text_width(1);
-        uLCD.text_height(1);
         uLCD.locate(10, 15);
         uLCD.color(WHITE);
         uLCD.printf("cm:%1d\n",dist );
+        
+        //temperature NEED TO FIX THIS
+        if (i > 10)
+        {
+            uLCD.filled_rectangle(0, 0, 126, 80, 0x000000);
+            Temperature();
+            
+            i = 0;
+        }
+        
     }
 }
 float averageDistance()
@@ -68,4 +100,95 @@
     if (distance < 10) i=0.1;
     if (distance < 5) i=0.05;
     mySpeaker.PlayNote(600, i, 5.0);  
+}
+void Temperature(){
+ 
+    a = avgTemp();
+    b = avgTemp2();
+    TempConstraints();
+    Temp = (a + b)/2;
+    uLCD.text_width(1);
+    uLCD.text_height(1);
+    uLCD.locate(1,1);
+    uLCD.printf("%3.1f deg C\n\r", Temp);
+}
+ 
+void TempConstraints() {
+ 
+    if(degrees_c <= 4){
+        pc.printf("WARNING\nCOLD TEMPERATURES\nICE MAY BE PRESENT");
+    }
+    if(abs(a-b) > 4){
+        uLCD.printf("WARNING\nTEMPERATURE SENSOR\nFAULT\n");
+    }
+}  
+ 
+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
+float avgTemp(){
+    float a = getTemp();
+    float b = getTemp();
+    float c = getTemp();
+    float d = getTemp();
+    float e = getTemp();
+    float f = getTemp();
+    float g = getTemp();
+    float h = getTemp();
+    float i = getTemp();
+    float j = getTemp();
+    float k = getTemp();
+    float l = getTemp();
+    float m = getTemp();
+    float n = getTemp();
+    float o = getTemp();
+    float p = getTemp();
+    float q = getTemp();
+    float r = getTemp();
+    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();
+    float c = getTemp2();
+    float d = getTemp2();
+    float e = getTemp2();
+    float f = getTemp2();
+    float g = getTemp2();
+    float h = getTemp2();
+    float i = getTemp2();
+    float j = getTemp2();
+    float k = getTemp2();
+    float l = getTemp2();
+    float m = getTemp2();
+    float n = getTemp2();
+    float o = getTemp2();
+    float p = getTemp2();
+    float q = getTemp2();
+    float r = getTemp2();
+    float s = getTemp2();
+    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;
 }
\ No newline at end of file