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-23
Revision:
5:f67b64d82cf3
Parent:
4:742f4fe97790
Child:
6:fa417472c812

File content as of revision 5:f67b64d82cf3:

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

Author: Alex Mueller and Marissa Gore
Date: 10/22/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);
void Timer();                    //not done
int PersonId();
int PeakLocation();
void Counter(int count);
void Status();

int main()
{
    float value;
    bool incident = false; 
    float baseline = 65;             //65cm
    int count = 0;
    int person;
    uint8_t DisplayedString[7] = {0};
    Ctrl = 1;                       

   while(1)
   {
       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 = 0;           //set low to turn on relay
                count++;
                Counter(count);
            }
            else if(person == 2)
            {
                count--;
                Counter(count);
                if(count == 0)
                {
                    Ctrl = 1;       //set high to turn off relay
                }
            }
            incident = false;
            numDataSamples = 0;
        }
        ThisThread::sleep_for(10);
    }  
}      

void SensorValues(float value)
{
   sampledData[numDataSamples] = value;
   numDataSamples++;
}   

int PeakLocation()
{
    int min = sampledData[0];     
    int saveLow = 0;        
    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/2))        // Lowest < midpoint
    {
        return 2;
    }
    else if(lowest > (numDataSamples/2))  // Lowest > midpoint
    {
        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);
}