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

Dependencies:   BSP_DISCO_L476VG LCD_DISCO_L476VG

main.cpp

Committer:
mygore
Date:
2020-10-24
Revision:
6:fa417472c812
Parent:
5:f67b64d82cf3
Child:
7:3570853380bf

File content as of revision 6:fa417472c812:

/* Project: Data Fitting Code
Description: Base functionality for lighting controller.

Author: Alex Mueller and Marissa Gore
Date: 10/24/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
AnalogIn ain(PA_1);                 //Sensor

LCD_DISCO_L476VG lcd;

//Global variables
float sampledData[10000];
int numDataSamples = 0;

//Initalize functions
void SensorValues(float value);
int PersonId();
int PeakLocation();
void Counter(int count);
void Status();

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
    int person;
    uint8_t DisplayedString[7] = {0};
    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
        }
        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
            {
                Ctrl = 0;                 //Set low to turn on relay
                count++;                 //Increase count by 1
                Counter(count);         //Display count to LCD
            }
            else if(person == 2)                   //If person exited room
            {
                count--;                 //Decrease count by 1
                Counter(count);         //Display count to LCD
                if(count == 0)          //If the count is equal to zero, turn off light
                {
                    Ctrl = 1;        //Set high to turn off relay
                }
            }
            incident = false;            //Incident is set back to false
            numDataSamples = 0;         //Number of data samples are cleared to zero
        }
        ThisThread::sleep_for(10);      //10 second delay
    }  
}      

void SensorValues(float value)              //Function to read sensor values into array
{
   sampledData[numDataSamples] = value;     //Reads sensor values into array sampledData
   numDataSamples++;                        //Increase for number of data samples in incident
}   

int PeakLocation()                          //Function to find minimum value of array
{
    int min = sampledData[0];               //Initalize minimum value in array to first postion
    int saveLow = 0;        
    for (int i = 1; i < numDataSamples; ++i)  //For loop through array     
    {
        if(sampledData[i] < min)               //If the array value is less than minimum
        {   
            min = sampledData[i];              //Save that array value as minimum
            saveLow = i;                      //Save the postion of the minimum array value
        }   
    }
    return saveLow;                         //Return min value position
}

int PersonId()                              //Function to determine if person entered or exited room
{
    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
    {
        return 2;                         //Return 2 to main to let know person exited
    }
    else if(lowest > (numDataSamples/2))  // If Lowest(min value position) > midpoint, person entered
    {
        return 1;                       //Return 1 to main to let know person entered
    }
    else
    {
        return 0;                       //Return 0 if error
    }
}

void Counter(int count)                 //Function to print count to LCD
{
    lcd.Clear();
    uint8_t DisplayedString[7] = {0};
    sprintf((char *)DisplayedString, "%d\n", count);
    lcd.DisplayString(DisplayedString);
}