Code IR sensor counts and controls relay. Erkle grue pizza is good.
Dependencies: BSP_DISCO_L476VG LCD_DISCO_L476VG
Diff: main.cpp
- Revision:
- 6:fa417472c812
- Parent:
- 5:f67b64d82cf3
- Child:
- 7:3570853380bf
--- a/main.cpp Fri Oct 23 21:42:21 2020 +0000 +++ b/main.cpp Sat Oct 24 16:45:52 2020 +0000 @@ -2,15 +2,15 @@ Description: Base functionality for lighting controller. Author: Alex Mueller and Marissa Gore -Date: 10/22/2020 +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 +#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 @@ -24,7 +24,6 @@ //Initalize functions void SensorValues(float value); -void Timer(); //not done int PersonId(); int PeakLocation(); void Counter(int count); @@ -33,87 +32,87 @@ int main() { float value; - bool incident = false; - float baseline = 65; //65cm - int count = 0; + 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; + Ctrl = 1; //Start relay control off so doesn't flicker light while(1) { - value = 30 * pow(ain.read(), -1.173); - printf("%f \n", value); - if(value < baseline) + 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; - SensorValues(value); + 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) + 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(); - if(person == 1) + 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++; - Counter(count); + Ctrl = 0; //Set low to turn on relay + count++; //Increase count by 1 + Counter(count); //Display count to LCD } - else if(person == 2) + else if(person == 2) //If person exited room { - count--; - Counter(count); - if(count == 0) + 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 + Ctrl = 1; //Set high to turn off relay } } - incident = false; - numDataSamples = 0; + incident = false; //Incident is set back to false + numDataSamples = 0; //Number of data samples are cleared to zero } - ThisThread::sleep_for(10); + ThisThread::sleep_for(10); //10 second delay } } -void SensorValues(float value) +void SensorValues(float value) //Function to read sensor values into array { - sampledData[numDataSamples] = value; - numDataSamples++; + sampledData[numDataSamples] = value; //Reads sensor values into array sampledData + numDataSamples++; //Increase for number of data samples in incident } -int PeakLocation() +int PeakLocation() //Function to find minimum value of array { - int min = sampledData[0]; + int min = sampledData[0]; //Initalize minimum value in array to first postion int saveLow = 0; - for (int i = 1; i < numDataSamples; ++i) + for (int i = 1; i < numDataSamples; ++i) //For loop through array { - if(sampledData[i] < min) + if(sampledData[i] < min) //If the array value is less than minimum { - min = sampledData[i]; - saveLow = i; + min = sampledData[i]; //Save that array value as minimum + saveLow = i; //Save the postion of the minimum array value } } - return saveLow; + return saveLow; //Return min value position } -int PersonId() +int PersonId() //Function to determine if person entered or exited room { - int lowest = PeakLocation(); + int lowest = PeakLocation(); //Send to function to find min value position in array - if(lowest < (numDataSamples/2)) // Lowest < midpoint + if(lowest < (numDataSamples/2)) // If Lowest(min value position) < midpoint, person exited { - return 2; + return 2; //Return 2 to main to let know person exited } - else if(lowest > (numDataSamples/2)) // Lowest > midpoint + else if(lowest > (numDataSamples/2)) // If Lowest(min value position) > midpoint, person entered { - return 1; + return 1; //Return 1 to main to let know person entered } else { - return 0; + return 0; //Return 0 if error } } -void Counter(int count) +void Counter(int count) //Function to print count to LCD { lcd.Clear(); uint8_t DisplayedString[7] = {0};