Code IR sensor counts and controls relay. Erkle grue pizza is good.
Dependencies: BSP_DISCO_L476VG LCD_DISCO_L476VG
main.cpp@6:fa417472c812, 2020-10-24 (annotated)
- Committer:
- mygore
- Date:
- Sat Oct 24 16:45:52 2020 +0000
- Revision:
- 6:fa417472c812
- Parent:
- 5:f67b64d82cf3
- Child:
- 7:3570853380bf
Added comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mygore | 0:0949c00010ff | 1 | /* Project: Data Fitting Code |
mygore | 0:0949c00010ff | 2 | Description: Base functionality for lighting controller. |
mygore | 0:0949c00010ff | 3 | |
mygore | 0:0949c00010ff | 4 | Author: Alex Mueller and Marissa Gore |
mygore | 6:fa417472c812 | 5 | Date: 10/24/2020 |
mygore | 0:0949c00010ff | 6 | |
mygore | 0:0949c00010ff | 7 | */ |
mygore | 0:0949c00010ff | 8 | |
mygore | 0:0949c00010ff | 9 | //Includes |
mygore | 6:fa417472c812 | 10 | #include "mbed.h" //Includes mbed header file |
mygore | 6:fa417472c812 | 11 | #include "LCD_DISCO_L476VG.h" //Includes LCD libraries |
mygore | 6:fa417472c812 | 12 | #include <stdio.h> //Includes studio header file |
mygore | 6:fa417472c812 | 13 | #include <fstream> //Include file handing library |
mygore | 0:0949c00010ff | 14 | |
mygore | 0:0949c00010ff | 15 | |
mygore | 0:0949c00010ff | 16 | DigitalOut Ctrl(PA_2); //Controls relay |
mygore | 0:0949c00010ff | 17 | AnalogIn ain(PA_1); //Sensor |
mygore | 0:0949c00010ff | 18 | |
mygore | 0:0949c00010ff | 19 | LCD_DISCO_L476VG lcd; |
mygore | 0:0949c00010ff | 20 | |
mygore | 0:0949c00010ff | 21 | //Global variables |
mygore | 0:0949c00010ff | 22 | float sampledData[10000]; |
mygore | 0:0949c00010ff | 23 | int numDataSamples = 0; |
mygore | 0:0949c00010ff | 24 | |
mygore | 0:0949c00010ff | 25 | //Initalize functions |
mygore | 0:0949c00010ff | 26 | void SensorValues(float value); |
mygore | 0:0949c00010ff | 27 | int PersonId(); |
mygore | 0:0949c00010ff | 28 | int PeakLocation(); |
mygore | 0:0949c00010ff | 29 | void Counter(int count); |
mygore | 0:0949c00010ff | 30 | void Status(); |
mygore | 0:0949c00010ff | 31 | |
mygore | 0:0949c00010ff | 32 | int main() |
mygore | 0:0949c00010ff | 33 | { |
mygore | 0:0949c00010ff | 34 | float value; |
mygore | 6:fa417472c812 | 35 | bool incident = false; //Setting incident intially to false. Incident = when someone has waked through doorway |
mygore | 6:fa417472c812 | 36 | float baseline = 65; //Initalize baseline to 65cm |
mygore | 6:fa417472c812 | 37 | int count = 0; //Initalize count to zero |
mygore | 0:0949c00010ff | 38 | int person; |
mygore | 0:0949c00010ff | 39 | uint8_t DisplayedString[7] = {0}; |
mygore | 6:fa417472c812 | 40 | Ctrl = 1; //Start relay control off so doesn't flicker light |
mygore | 0:0949c00010ff | 41 | |
mygore | 0:0949c00010ff | 42 | while(1) |
mygore | 0:0949c00010ff | 43 | { |
mygore | 6:fa417472c812 | 44 | value = 30 * pow(ain.read(), -1.173); //Reads in values from sensor - AnalogIn |
mygore | 6:fa417472c812 | 45 | printf("%f \n", value); //Prints sensor values to serial monitor |
mygore | 6:fa417472c812 | 46 | if(value < baseline) //If a sensor value drops below the baseline(65cm) |
mygore | 0:0949c00010ff | 47 | { |
mygore | 6:fa417472c812 | 48 | incident = true; //Incidient is set to true (a person walked through doorway) |
mygore | 6:fa417472c812 | 49 | SensorValues(value); //Read in sensor values (of the incident) into an array |
mygore | 0:0949c00010ff | 50 | } |
mygore | 6:fa417472c812 | 51 | else if(incident == true && value >= baseline) //If incident was previously set to true and the sensor values go back up to the baseline |
mygore | 0:0949c00010ff | 52 | { |
mygore | 6:fa417472c812 | 53 | person = PersonId(); //Send to person Id funciton - determine if a person entered or exited |
mygore | 6:fa417472c812 | 54 | if(person == 1) //If person entered room |
mygore | 0:0949c00010ff | 55 | { |
mygore | 6:fa417472c812 | 56 | Ctrl = 0; //Set low to turn on relay |
mygore | 6:fa417472c812 | 57 | count++; //Increase count by 1 |
mygore | 6:fa417472c812 | 58 | Counter(count); //Display count to LCD |
mygore | 0:0949c00010ff | 59 | } |
mygore | 6:fa417472c812 | 60 | else if(person == 2) //If person exited room |
mygore | 0:0949c00010ff | 61 | { |
mygore | 6:fa417472c812 | 62 | count--; //Decrease count by 1 |
mygore | 6:fa417472c812 | 63 | Counter(count); //Display count to LCD |
mygore | 6:fa417472c812 | 64 | if(count == 0) //If the count is equal to zero, turn off light |
mygore | 0:0949c00010ff | 65 | { |
mygore | 6:fa417472c812 | 66 | Ctrl = 1; //Set high to turn off relay |
mygore | 0:0949c00010ff | 67 | } |
mygore | 0:0949c00010ff | 68 | } |
mygore | 6:fa417472c812 | 69 | incident = false; //Incident is set back to false |
mygore | 6:fa417472c812 | 70 | numDataSamples = 0; //Number of data samples are cleared to zero |
mygore | 0:0949c00010ff | 71 | } |
mygore | 6:fa417472c812 | 72 | ThisThread::sleep_for(10); //10 second delay |
mygore | 0:0949c00010ff | 73 | } |
mygore | 0:0949c00010ff | 74 | } |
mygore | 0:0949c00010ff | 75 | |
mygore | 6:fa417472c812 | 76 | void SensorValues(float value) //Function to read sensor values into array |
mygore | 0:0949c00010ff | 77 | { |
mygore | 6:fa417472c812 | 78 | sampledData[numDataSamples] = value; //Reads sensor values into array sampledData |
mygore | 6:fa417472c812 | 79 | numDataSamples++; //Increase for number of data samples in incident |
mygore | 0:0949c00010ff | 80 | } |
mygore | 0:0949c00010ff | 81 | |
mygore | 6:fa417472c812 | 82 | int PeakLocation() //Function to find minimum value of array |
mygore | 0:0949c00010ff | 83 | { |
mygore | 6:fa417472c812 | 84 | int min = sampledData[0]; //Initalize minimum value in array to first postion |
mygore | 3:b6bfb5955a08 | 85 | int saveLow = 0; |
mygore | 6:fa417472c812 | 86 | for (int i = 1; i < numDataSamples; ++i) //For loop through array |
mygore | 0:0949c00010ff | 87 | { |
mygore | 6:fa417472c812 | 88 | if(sampledData[i] < min) //If the array value is less than minimum |
mygore | 0:0949c00010ff | 89 | { |
mygore | 6:fa417472c812 | 90 | min = sampledData[i]; //Save that array value as minimum |
mygore | 6:fa417472c812 | 91 | saveLow = i; //Save the postion of the minimum array value |
mygore | 0:0949c00010ff | 92 | } |
mygore | 0:0949c00010ff | 93 | } |
mygore | 6:fa417472c812 | 94 | return saveLow; //Return min value position |
mygore | 0:0949c00010ff | 95 | } |
mygore | 0:0949c00010ff | 96 | |
mygore | 6:fa417472c812 | 97 | int PersonId() //Function to determine if person entered or exited room |
mygore | 0:0949c00010ff | 98 | { |
mygore | 6:fa417472c812 | 99 | int lowest = PeakLocation(); //Send to function to find min value position in array |
mygore | 0:0949c00010ff | 100 | |
mygore | 6:fa417472c812 | 101 | if(lowest < (numDataSamples/2)) // If Lowest(min value position) < midpoint, person exited |
mygore | 0:0949c00010ff | 102 | { |
mygore | 6:fa417472c812 | 103 | return 2; //Return 2 to main to let know person exited |
mygore | 0:0949c00010ff | 104 | } |
mygore | 6:fa417472c812 | 105 | else if(lowest > (numDataSamples/2)) // If Lowest(min value position) > midpoint, person entered |
mygore | 0:0949c00010ff | 106 | { |
mygore | 6:fa417472c812 | 107 | return 1; //Return 1 to main to let know person entered |
mygore | 0:0949c00010ff | 108 | } |
mygore | 0:0949c00010ff | 109 | else |
mygore | 0:0949c00010ff | 110 | { |
mygore | 6:fa417472c812 | 111 | return 0; //Return 0 if error |
mygore | 0:0949c00010ff | 112 | } |
mygore | 0:0949c00010ff | 113 | } |
mygore | 0:0949c00010ff | 114 | |
mygore | 6:fa417472c812 | 115 | void Counter(int count) //Function to print count to LCD |
mygore | 0:0949c00010ff | 116 | { |
mygore | 0:0949c00010ff | 117 | lcd.Clear(); |
mygore | 0:0949c00010ff | 118 | uint8_t DisplayedString[7] = {0}; |
mygore | 0:0949c00010ff | 119 | sprintf((char *)DisplayedString, "%d\n", count); |
mygore | 0:0949c00010ff | 120 | lcd.DisplayString(DisplayedString); |
mygore | 2:f8e9daaf48b5 | 121 | } |