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

Dependencies:   BSP_DISCO_L476VG LCD_DISCO_L476VG

Revision:
0:0949c00010ff
Child:
1:28d1cf3f31f7
diff -r 000000000000 -r 0949c00010ff main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Oct 19 20:57:39 2020 +0000
@@ -0,0 +1,167 @@
+/* Project: Data Fitting Code
+Description: Base functionality for lighting controller.
+
+Author: Alex Mueller and Marissa Gore
+Date: 10/18/2020
+
+*/
+
+//Includes
+#include "mbed.h"               //includes mbed header file
+#include "LCD_DISCO_L476VG.h"   //includes LCD libraries
+#include <stdio.h>              //includes studio header file
+#include <fstream>              //include file handing library
+
+
+DigitalOut Ctrl(PA_2);              //Controls relay
+DigitalIn button(PA_0);             //Push button changes display mode
+AnalogIn ain(PA_1);                 //Sensor
+
+LCD_DISCO_L476VG lcd;
+
+//Global variables
+float sampledData[10000];
+int numDataSamples = 0;
+//int StateEx = 1;
+
+//Initalize functions
+void SensorValues(float value);
+void Timer();                    //not done
+int PersonId();
+int PeakLocation();
+void Counter(int count);
+void Status();
+//void DisplayHandler(int count);
+
+int main()
+{
+    float value;
+    bool incident = false; 
+    float baseline = 65;             //75cm
+    int count = 0;
+    int person;
+    uint8_t DisplayedString[7] = {0};
+
+   while(1)
+   {
+//       printf("%i\n", count*50);
+//       DisplayHandler(count);
+       value = 30 * pow(ain.read(), -1.173);
+       printf("%f \n", value);
+       if(value < baseline)
+       {
+           incident = true;
+           SensorValues(value);
+        }
+        else if(incident == true && value >= baseline)
+        {
+            person = PersonId();
+            if(person == 1)
+            {
+                Ctrl = 1;           //change to set low to turn on relay
+                count++;
+                Counter(count);
+            }
+            else if(person == 0)
+            {
+                count--;
+                Counter(count);
+                if(count == 0)
+                {
+                    Ctrl = 0;       //change to set high to turn off relay
+                }
+            }
+            incident = false;
+            numDataSamples = 0;
+        }
+        ThisThread::sleep_for(5);
+    }  
+}      
+
+void SensorValues(float value)
+{
+   sampledData[numDataSamples] = value;
+   numDataSamples++;
+}   
+
+////void Timer()
+////{
+////    pizza
+////}
+
+int PeakLocation()
+{
+    int min = sampledData[0];     
+    int saveLow;        
+    for (int i = 1; i < numDataSamples; ++i)       
+    {
+        if(sampledData[i] < min)         
+        {   
+            min = sampledData[i];    
+            saveLow = i;      
+        }   
+    }
+    return saveLow;   
+}
+
+int PersonId()
+{
+    int lowest = PeakLocation();
+    
+    if(lowest < (numDataSamples*(1/2 - 0.1)))        // Lowest < midpoint - 10%
+    {
+        return -1;
+    }
+    else if(lowest > (numDataSamples*(1/2 + 0.1)))  // Lowest > midpoint + 10%
+    {
+        return 1;
+    }
+    else
+    {
+        return 0;
+    }
+}
+
+void Counter(int count)
+{
+    lcd.Clear();
+    uint8_t DisplayedString[7] = {0};
+    sprintf((char *)DisplayedString, "%d\n", count);
+    lcd.DisplayString(DisplayedString);
+    
+//    if(button == 1)
+//    {
+//    StateEx =2;
+//    }
+}
+
+//void Status()
+//{
+//    lcd.Clear();
+//    if(Ctrl == 1)
+//    {
+//         lcd.DisplayString((uint8_t *)"OFF");
+//    }
+//    if(Ctrl == 0)
+//    {
+//         lcd.DisplayString((uint8_t *)"ON");  
+//    }
+//    if(button == 1)
+//    {
+//    StateEx =1;
+//    }
+//}
+
+//void DisplayHandler(int count)
+//{
+//        switch(StateEx)
+//        {
+//            case PersonCount: Counter(count);
+//                            break;
+//            case LEDstatus: Status(); 
+//                            break;
+//            case ErrorState: printf("Error the cookie monster is going to eat you"); 
+//                            break;
+//        }
+//        wait(0.1);
+//}
\ No newline at end of file