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

Dependencies:   BSP_DISCO_L476VG LCD_DISCO_L476VG

Committer:
mygore
Date:
Mon Oct 19 21:05:49 2020 +0000
Revision:
1:28d1cf3f31f7
Parent:
0:0949c00010ff
Child:
2:f8e9daaf48b5
Works better than before. IR counts and controls relay. Pizza

Who changed what in which revision?

UserRevisionLine numberNew 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 0:0949c00010ff 5 Date: 10/18/2020
mygore 0:0949c00010ff 6
mygore 0:0949c00010ff 7 */
mygore 0:0949c00010ff 8
mygore 0:0949c00010ff 9 //Includes
mygore 0:0949c00010ff 10 #include "mbed.h" //includes mbed header file
mygore 0:0949c00010ff 11 #include "LCD_DISCO_L476VG.h" //includes LCD libraries
mygore 0:0949c00010ff 12 #include <stdio.h> //includes studio header file
mygore 0:0949c00010ff 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 DigitalIn button(PA_0); //Push button changes display mode
mygore 0:0949c00010ff 18 AnalogIn ain(PA_1); //Sensor
mygore 0:0949c00010ff 19
mygore 0:0949c00010ff 20 LCD_DISCO_L476VG lcd;
mygore 0:0949c00010ff 21
mygore 0:0949c00010ff 22 //Global variables
mygore 0:0949c00010ff 23 float sampledData[10000];
mygore 0:0949c00010ff 24 int numDataSamples = 0;
mygore 0:0949c00010ff 25 //int StateEx = 1;
mygore 0:0949c00010ff 26
mygore 0:0949c00010ff 27 //Initalize functions
mygore 0:0949c00010ff 28 void SensorValues(float value);
mygore 0:0949c00010ff 29 void Timer(); //not done
mygore 0:0949c00010ff 30 int PersonId();
mygore 0:0949c00010ff 31 int PeakLocation();
mygore 0:0949c00010ff 32 void Counter(int count);
mygore 0:0949c00010ff 33 void Status();
mygore 0:0949c00010ff 34 //void DisplayHandler(int count);
mygore 0:0949c00010ff 35
mygore 0:0949c00010ff 36 int main()
mygore 0:0949c00010ff 37 {
mygore 0:0949c00010ff 38 float value;
mygore 0:0949c00010ff 39 bool incident = false;
mygore 0:0949c00010ff 40 float baseline = 65; //75cm
mygore 0:0949c00010ff 41 int count = 0;
mygore 0:0949c00010ff 42 int person;
mygore 0:0949c00010ff 43 uint8_t DisplayedString[7] = {0};
mygore 0:0949c00010ff 44
mygore 0:0949c00010ff 45 while(1)
mygore 0:0949c00010ff 46 {
mygore 0:0949c00010ff 47 // printf("%i\n", count*50);
mygore 0:0949c00010ff 48 // DisplayHandler(count);
mygore 0:0949c00010ff 49 value = 30 * pow(ain.read(), -1.173);
mygore 0:0949c00010ff 50 printf("%f \n", value);
mygore 0:0949c00010ff 51 if(value < baseline)
mygore 0:0949c00010ff 52 {
mygore 0:0949c00010ff 53 incident = true;
mygore 0:0949c00010ff 54 SensorValues(value);
mygore 0:0949c00010ff 55 }
mygore 0:0949c00010ff 56 else if(incident == true && value >= baseline)
mygore 0:0949c00010ff 57 {
mygore 0:0949c00010ff 58 person = PersonId();
mygore 0:0949c00010ff 59 if(person == 1)
mygore 0:0949c00010ff 60 {
mygore 0:0949c00010ff 61 Ctrl = 1; //change to set low to turn on relay
mygore 0:0949c00010ff 62 count++;
mygore 0:0949c00010ff 63 Counter(count);
mygore 0:0949c00010ff 64 }
mygore 1:28d1cf3f31f7 65 else if(person == -1)
mygore 0:0949c00010ff 66 {
mygore 0:0949c00010ff 67 count--;
mygore 0:0949c00010ff 68 Counter(count);
mygore 0:0949c00010ff 69 if(count == 0)
mygore 0:0949c00010ff 70 {
mygore 0:0949c00010ff 71 Ctrl = 0; //change to set high to turn off relay
mygore 0:0949c00010ff 72 }
mygore 0:0949c00010ff 73 }
mygore 0:0949c00010ff 74 incident = false;
mygore 0:0949c00010ff 75 numDataSamples = 0;
mygore 0:0949c00010ff 76 }
mygore 0:0949c00010ff 77 ThisThread::sleep_for(5);
mygore 0:0949c00010ff 78 }
mygore 0:0949c00010ff 79 }
mygore 0:0949c00010ff 80
mygore 0:0949c00010ff 81 void SensorValues(float value)
mygore 0:0949c00010ff 82 {
mygore 0:0949c00010ff 83 sampledData[numDataSamples] = value;
mygore 0:0949c00010ff 84 numDataSamples++;
mygore 0:0949c00010ff 85 }
mygore 0:0949c00010ff 86
mygore 0:0949c00010ff 87 ////void Timer()
mygore 0:0949c00010ff 88 ////{
mygore 0:0949c00010ff 89 //// pizza
mygore 0:0949c00010ff 90 ////}
mygore 0:0949c00010ff 91
mygore 0:0949c00010ff 92 int PeakLocation()
mygore 0:0949c00010ff 93 {
mygore 0:0949c00010ff 94 int min = sampledData[0];
mygore 0:0949c00010ff 95 int saveLow;
mygore 0:0949c00010ff 96 for (int i = 1; i < numDataSamples; ++i)
mygore 0:0949c00010ff 97 {
mygore 0:0949c00010ff 98 if(sampledData[i] < min)
mygore 0:0949c00010ff 99 {
mygore 0:0949c00010ff 100 min = sampledData[i];
mygore 0:0949c00010ff 101 saveLow = i;
mygore 0:0949c00010ff 102 }
mygore 0:0949c00010ff 103 }
mygore 0:0949c00010ff 104 return saveLow;
mygore 0:0949c00010ff 105 }
mygore 0:0949c00010ff 106
mygore 0:0949c00010ff 107 int PersonId()
mygore 0:0949c00010ff 108 {
mygore 0:0949c00010ff 109 int lowest = PeakLocation();
mygore 0:0949c00010ff 110
mygore 1:28d1cf3f31f7 111 if(lowest < (numDataSamples*(1/2))) // Lowest < midpoint - 10%
mygore 0:0949c00010ff 112 {
mygore 0:0949c00010ff 113 return -1;
mygore 0:0949c00010ff 114 }
mygore 1:28d1cf3f31f7 115 else if(lowest > (numDataSamples*(1/2))) // Lowest > midpoint + 10%
mygore 0:0949c00010ff 116 {
mygore 0:0949c00010ff 117 return 1;
mygore 0:0949c00010ff 118 }
mygore 0:0949c00010ff 119 else
mygore 0:0949c00010ff 120 {
mygore 0:0949c00010ff 121 return 0;
mygore 0:0949c00010ff 122 }
mygore 0:0949c00010ff 123 }
mygore 0:0949c00010ff 124
mygore 0:0949c00010ff 125 void Counter(int count)
mygore 0:0949c00010ff 126 {
mygore 0:0949c00010ff 127 lcd.Clear();
mygore 0:0949c00010ff 128 uint8_t DisplayedString[7] = {0};
mygore 0:0949c00010ff 129 sprintf((char *)DisplayedString, "%d\n", count);
mygore 0:0949c00010ff 130 lcd.DisplayString(DisplayedString);
mygore 0:0949c00010ff 131
mygore 0:0949c00010ff 132 // if(button == 1)
mygore 0:0949c00010ff 133 // {
mygore 0:0949c00010ff 134 // StateEx =2;
mygore 0:0949c00010ff 135 // }
mygore 0:0949c00010ff 136 }
mygore 0:0949c00010ff 137
mygore 0:0949c00010ff 138 //void Status()
mygore 0:0949c00010ff 139 //{
mygore 0:0949c00010ff 140 // lcd.Clear();
mygore 0:0949c00010ff 141 // if(Ctrl == 1)
mygore 0:0949c00010ff 142 // {
mygore 0:0949c00010ff 143 // lcd.DisplayString((uint8_t *)"OFF");
mygore 0:0949c00010ff 144 // }
mygore 0:0949c00010ff 145 // if(Ctrl == 0)
mygore 0:0949c00010ff 146 // {
mygore 0:0949c00010ff 147 // lcd.DisplayString((uint8_t *)"ON");
mygore 0:0949c00010ff 148 // }
mygore 0:0949c00010ff 149 // if(button == 1)
mygore 0:0949c00010ff 150 // {
mygore 0:0949c00010ff 151 // StateEx =1;
mygore 0:0949c00010ff 152 // }
mygore 0:0949c00010ff 153 //}
mygore 0:0949c00010ff 154
mygore 0:0949c00010ff 155 //void DisplayHandler(int count)
mygore 0:0949c00010ff 156 //{
mygore 0:0949c00010ff 157 // switch(StateEx)
mygore 0:0949c00010ff 158 // {
mygore 0:0949c00010ff 159 // case PersonCount: Counter(count);
mygore 0:0949c00010ff 160 // break;
mygore 0:0949c00010ff 161 // case LEDstatus: Status();
mygore 0:0949c00010ff 162 // break;
mygore 0:0949c00010ff 163 // case ErrorState: printf("Error the cookie monster is going to eat you");
mygore 0:0949c00010ff 164 // break;
mygore 0:0949c00010ff 165 // }
mygore 0:0949c00010ff 166 // wait(0.1);
mygore 0:0949c00010ff 167 //}