Code IR sensor counts and controls relay. Erkle grue pizza is good.

Dependencies:   BSP_DISCO_L476VG LCD_DISCO_L476VG

Files at this revision

API Documentation at this revision

Comitter:
mygore
Date:
Thu Oct 29 19:21:54 2020 +0000
Parent:
6:fa417472c812
Commit message:
Newest code- fixes false entry and false exit problems. Pizza

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r fa417472c812 -r 3570853380bf main.cpp
--- a/main.cpp	Sat Oct 24 16:45:52 2020 +0000
+++ b/main.cpp	Thu Oct 29 19:21:54 2020 +0000
@@ -1,8 +1,9 @@
 /* Project: Data Fitting Code
 Description: Base functionality for lighting controller.
+Works with IR sensor. Fixed negative problem.
 
 Author: Alex Mueller and Marissa Gore
-Date: 10/24/2020
+Date: 10/28/2020
 
 */
 
@@ -26,93 +27,161 @@
 void SensorValues(float value);
 int PersonId();
 int PeakLocation();
+int MultiplePeaks(int lowestValue); 
 void Counter(int count);
-void Status();
+int PeakLowValue();
+
 
 int main()
 {
     float value;
-    bool incident = false;           //Setting incident intially to false. Incident = when someone has waked through doorway
-    float baseline = 65;             //Initalize baseline to 65cm
-    int count = 0;                  //Initalize count to zero
+    bool incident = false;           //Setting incident intially to false. Incident = when someone has waked through doorway.
+    float baseline = 65;             //65cm
+    int count = 0;                  
     int person;
     uint8_t DisplayedString[7] = {0};
-    Ctrl = 1;                        //Start relay control off so doesn't flicker light
+    Ctrl = 1;                        //Start relay control off so doesn't flicker light.
 
    while(1)
    {
-       value = 30 * pow(ain.read(), -1.173);    //Reads in values from sensor - AnalogIn
-       printf("%f \n", value);                  //Prints sensor values to serial monitor
-       if(value < baseline)                     //If a sensor value drops below the baseline(65cm)
-       {
-           incident = true;                     //Incidient is set to true (a person walked through doorway)
-           SensorValues(value);                 //Read in sensor values (of the incident) into an array
+       value = 30 * pow(ain.read(), -1.173);    
+       printf("%f \n", value);                  
+       if(value < baseline)                     //If a sensor value drops below the baseline, incidient is set to 
+       {                                        // true(person walked through doorway and sensor values of incident are read into an array.
+           incident = true;                     
+           SensorValues(value);                 
         }
-        else if(incident == true && value >= baseline)      //If incident was previously set to true and the sensor values go back up to the baseline
-        {
-            person = PersonId();                    //Send to person Id funciton - determine if a person entered or exited
-            if(person == 1)                         //If person entered room
+        else if(incident == true && value >= baseline)      //If incident was previously set to true and the sensor valuesgo back up to the baseline,
+        {                                                   // send to person Id funciton to determine if a person entered or exited. 
+            person = PersonId();                            //Control relay and incriment/decriment count accordingly.
+            if(person == 1)                         
             {
-                Ctrl = 0;                 //Set low to turn on relay
-                count++;                 //Increase count by 1
-                Counter(count);         //Display count to LCD
+                Ctrl = 0;                 //Set low to turn on relay.
+                count++;                 
+                Counter(count);        
             }
-            else if(person == 2)                   //If person exited room
+            else if(person == 2)                  
             {
-                count--;                 //Decrease count by 1
-                Counter(count);         //Display count to LCD
-                if(count == 0)          //If the count is equal to zero, turn off light
+                count--;                 
+                Counter(count);         
+                if(count <= 0)          
                 {
-                    Ctrl = 1;        //Set high to turn off relay
+                    Ctrl = 1;          //Set high to turn off relay.
+                    count = 0;        //If count is negative set it to zero.
+                    Counter(count);
                 }
             }
-            incident = false;            //Incident is set back to false
-            numDataSamples = 0;         //Number of data samples are cleared to zero
+            incident = false;            
+            numDataSamples = 0;         
         }
-        ThisThread::sleep_for(10);      //10 second delay
+        ThisThread::sleep_for(10);      
     }  
 }      
 
-void SensorValues(float value)              //Function to read sensor values into array
+void SensorValues(float value)              //Function to read sensor values into array, numDataSamples are increased for number of data samples in incident.
 {
-   sampledData[numDataSamples] = value;     //Reads sensor values into array sampledData
-   numDataSamples++;                        //Increase for number of data samples in incident
+   sampledData[numDataSamples] = value;     
+   numDataSamples++;                        
 }   
 
-int PeakLocation()                          //Function to find minimum value of array
+int PeakLocation()                          //Function to find position of min value in incident array
 {
-    int min = sampledData[0];               //Initalize minimum value in array to first postion
+    int min = sampledData[0];               
     int saveLow = 0;        
-    for (int i = 1; i < numDataSamples; ++i)  //For loop through array     
+    for (int i = 1; i < numDataSamples; ++i)       
     {
-        if(sampledData[i] < min)               //If the array value is less than minimum
+        if(sampledData[i] < min)               
         {   
-            min = sampledData[i];              //Save that array value as minimum
-            saveLow = i;                      //Save the postion of the minimum array value
+            min = sampledData[i];              
+            saveLow = i;                      
+        }   
+    }
+    return saveLow;                         
+}
+
+int PeakLowValue()                      //Function to find minimum value in incident array        
+{
+    int min = sampledData[0];               
+    int saveLow = 0;        
+    for (int i = 1; i < numDataSamples; ++i)       
+    {
+        if(sampledData[i] < min)               
+        {   
+            min = sampledData[i];                                   
         }   
     }
-    return saveLow;                         //Return min value position
+    return min;                         
 }
 
-int PersonId()                              //Function to determine if person entered or exited room
+int MultiplePeaks(int lowestValue)              //Function to determine if there are multiple peaks. (multiple peaks = false entry/exit)
 {
-    int lowest = PeakLocation();            //Send to function to find min value position in array
-    
-    if(lowest < (numDataSamples/2))        // If Lowest(min value position) < midpoint, person exited
+    bool mul = false;
+    int lowFirst = sampledData[0];
+    int lowSecond = sampledData[0];
+    int firstPos = 0;
+    int secondPos = 0;
+    for(int i = 1; i < numDataSamples/2; i++)
     {
-        return 2;                         //Return 2 to main to let know person exited
+         if(sampledData[i] < lowFirst)               
+        {   
+            lowFirst = sampledData[i];  
+            firstPos = i;                                  
+        } 
     }
-    else if(lowest > (numDataSamples/2))  // If Lowest(min value position) > midpoint, person entered
+    for(int i = numDataSamples/2; i < numDataSamples; i++)
     {
-        return 1;                       //Return 1 to main to let know person entered
+         if(sampledData[i] < lowSecond)               
+        {   
+            lowSecond = sampledData[i];    
+            secondPos = i;                             
+        } 
+    }
+    if(firstPos <= (secondPos*0.85) || firstPos >=  (secondPos*0.85))
+    {
+        mul = false;
     }
     else
     {
-        return 0;                       //Return 0 if error
+        if(lowFirst < (lowestValue*0.9) && lowSecond < (lowestValue*0.9))
+        {
+            mul = true;
+        }
+        else
+        {
+            mul = false;
+        }
+    }  
+    return mul;
+}
+    
+int PersonId()                              //Function to determine if person entered or exited room. 
+{
+    int lowest = PeakLocation();   
+    int lowestValue = PeakLowValue();
+    bool mul = MultiplePeaks(lowestValue);    
+    
+    if(mul == true)
+    {
+        return 0;
     }
+    else
+    {     
+        if(lowest < (numDataSamples/3))        // If Lowest(min value position) < 1/3 of data, person exited
+        {
+            return 2;                         
+        }
+        else if(lowest > (2*numDataSamples/3))  // If Lowest(min value position) > 2/3 of data, person entered
+        {
+            return 1;                       
+        }
+        else
+        {
+            return 0;                       
+        }
+    } 
 }
 
-void Counter(int count)                 //Function to print count to LCD
+void Counter(int count)                 //Function to print count to LCD.
 {
     lcd.Clear();
     uint8_t DisplayedString[7] = {0};