Removed the long initial wait
Dependents: Group4_WaterCollector_System
Air_Quality.cpp@1:8468b6ef791a, 2022-08-27 (annotated)
- Committer:
- johnanthonyjose
- Date:
- Sat Aug 27 01:46:45 2022 +0000
- Revision:
- 1:8468b6ef791a
- Parent:
- 0:885417624ec2
Final Version for presentation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedAustin | 0:885417624ec2 | 1 | // |
mbedAustin | 0:885417624ec2 | 2 | // modified by mbed group for use with the mbed platform |
mbedAustin | 0:885417624ec2 | 3 | // modification date 9/4/2014 |
mbedAustin | 0:885417624ec2 | 4 | // |
mbedAustin | 0:885417624ec2 | 5 | |
mbedAustin | 0:885417624ec2 | 6 | /* |
mbedAustin | 0:885417624ec2 | 7 | AirQuality library v1.0 |
mbedAustin | 0:885417624ec2 | 8 | 2010 Copyright (c) Seeed Technology Inc. All right reserved. |
mbedAustin | 0:885417624ec2 | 9 | |
mbedAustin | 0:885417624ec2 | 10 | Original Author: Bruce.Qin |
mbedAustin | 0:885417624ec2 | 11 | |
mbedAustin | 0:885417624ec2 | 12 | This library is free software; you can redistribute it and/or |
mbedAustin | 0:885417624ec2 | 13 | modify it under the terms of the GNU Lesser General Public |
mbedAustin | 0:885417624ec2 | 14 | License as published by the Free Software Foundation; either |
mbedAustin | 0:885417624ec2 | 15 | version 2.1 of the License, or (at your option) any later version. |
mbedAustin | 0:885417624ec2 | 16 | |
mbedAustin | 0:885417624ec2 | 17 | This library is distributed in the hope that it will be useful, |
mbedAustin | 0:885417624ec2 | 18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
mbedAustin | 0:885417624ec2 | 19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
mbedAustin | 0:885417624ec2 | 20 | Lesser General Public License for more details. |
mbedAustin | 0:885417624ec2 | 21 | |
mbedAustin | 0:885417624ec2 | 22 | You should have received a copy of the GNU Lesser General Public |
mbedAustin | 0:885417624ec2 | 23 | License along with this library; if not, write to the Free Software |
mbedAustin | 0:885417624ec2 | 24 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
mbedAustin | 0:885417624ec2 | 25 | */ |
mbedAustin | 0:885417624ec2 | 26 | #include"Air_Quality.h" |
mbedAustin | 0:885417624ec2 | 27 | |
mbedAustin | 0:885417624ec2 | 28 | // Interrupt Handler Assignment |
mbedAustin | 0:885417624ec2 | 29 | Ticker IntHandler; |
mbedAustin | 0:885417624ec2 | 30 | |
mbedAustin | 0:885417624ec2 | 31 | //Get the avg voltage in 5 minutes. |
mbedAustin | 0:885417624ec2 | 32 | void AirQuality::avgVoltage() |
mbedAustin | 0:885417624ec2 | 33 | { |
mbedAustin | 0:885417624ec2 | 34 | if(i==150) { //sum 5 minutes |
mbedAustin | 0:885417624ec2 | 35 | vol_standard=temp/150; |
mbedAustin | 0:885417624ec2 | 36 | temp=0; |
mbedAustin | 0:885417624ec2 | 37 | printf("Vol_standard in 5 minutes: %d\n\r",vol_standard); |
mbedAustin | 0:885417624ec2 | 38 | i=0; |
mbedAustin | 0:885417624ec2 | 39 | } else { |
mbedAustin | 0:885417624ec2 | 40 | temp+=first_vol; |
mbedAustin | 0:885417624ec2 | 41 | i++; |
mbedAustin | 0:885417624ec2 | 42 | } |
mbedAustin | 0:885417624ec2 | 43 | } |
mbedAustin | 0:885417624ec2 | 44 | |
mbedAustin | 0:885417624ec2 | 45 | void AirQuality::init(PinName pin, void(*IRQ)(void)) |
mbedAustin | 0:885417624ec2 | 46 | { |
mbedAustin | 0:885417624ec2 | 47 | _pin=pin; |
mbedAustin | 0:885417624ec2 | 48 | AnalogIn sensor(_pin); |
mbedAustin | 0:885417624ec2 | 49 | unsigned char i = 0; |
mbedAustin | 0:885417624ec2 | 50 | printf("Air Quality Sensor Starting Up...(20s)\n\r"); |
johnanthonyjose | 1:8468b6ef791a | 51 | // TODO: Put it back |
johnanthonyjose | 1:8468b6ef791a | 52 | // wait(20); //20s |
mbedAustin | 0:885417624ec2 | 53 | init_voltage = sensor.read() * 1000; // boost the value to be on a 0 -> 1000 scale for compatibility |
mbedAustin | 0:885417624ec2 | 54 | printf("The initial voltage is %d%% of source \n\r",init_voltage/10); |
mbedAustin | 0:885417624ec2 | 55 | while(init_voltage) { |
mbedAustin | 0:885417624ec2 | 56 | if((init_voltage < 798) && (init_voltage > 10)) { |
mbedAustin | 0:885417624ec2 | 57 | // the init voltage is ok |
mbedAustin | 0:885417624ec2 | 58 | first_vol = sensor.read() * 1000;//initialize first value |
mbedAustin | 0:885417624ec2 | 59 | last_vol = first_vol; |
mbedAustin | 0:885417624ec2 | 60 | vol_standard = last_vol; |
mbedAustin | 0:885417624ec2 | 61 | printf("Sensor ready.\n\r"); |
mbedAustin | 0:885417624ec2 | 62 | error = false;; |
mbedAustin | 0:885417624ec2 | 63 | break; |
mbedAustin | 0:885417624ec2 | 64 | } else if(init_voltage > 798 || init_voltage <= 10) { |
mbedAustin | 0:885417624ec2 | 65 | // The sensor is not ready, wait a bit for it to cool off |
mbedAustin | 0:885417624ec2 | 66 | i++; |
mbedAustin | 0:885417624ec2 | 67 | printf("Sensor not ready (%d), try %d/5, waiting 60 seconds...\n\r",init_voltage,i); |
mbedAustin | 0:885417624ec2 | 68 | wait(60);//60s |
mbedAustin | 0:885417624ec2 | 69 | init_voltage = sensor.read() * 1000; |
mbedAustin | 0:885417624ec2 | 70 | if(i==5) { |
mbedAustin | 0:885417624ec2 | 71 | // After 5 minutes warn user that the sensor may be broken |
mbedAustin | 0:885417624ec2 | 72 | i = 0; |
mbedAustin | 0:885417624ec2 | 73 | error = true; |
mbedAustin | 0:885417624ec2 | 74 | printf("Sensor Error! You may have a bad sensor. :-(\n\r"); |
mbedAustin | 0:885417624ec2 | 75 | } |
mbedAustin | 0:885417624ec2 | 76 | } else |
mbedAustin | 0:885417624ec2 | 77 | break; |
mbedAustin | 0:885417624ec2 | 78 | } |
mbedAustin | 0:885417624ec2 | 79 | // Call AirQualityInterrupt every 2seconds |
mbedAustin | 0:885417624ec2 | 80 | IntHandler.attach(IRQ, 2.0); |
mbedAustin | 0:885417624ec2 | 81 | printf("Test begin...\n\r"); |
mbedAustin | 0:885417624ec2 | 82 | } |
mbedAustin | 0:885417624ec2 | 83 | |
mbedAustin | 0:885417624ec2 | 84 | int AirQuality::slope(void) |
mbedAustin | 0:885417624ec2 | 85 | { |
mbedAustin | 0:885417624ec2 | 86 | while(timer_index) { |
mbedAustin | 0:885417624ec2 | 87 | if(first_vol-last_vol > 400 || first_vol > 700) { |
mbedAustin | 0:885417624ec2 | 88 | printf("High pollution! Force signal active.\n\r"); |
mbedAustin | 0:885417624ec2 | 89 | timer_index = 0; |
mbedAustin | 0:885417624ec2 | 90 | avgVoltage(); |
mbedAustin | 0:885417624ec2 | 91 | return 0; |
mbedAustin | 0:885417624ec2 | 92 | } else if((first_vol - last_vol > 400 && first_vol < 700) || first_vol - vol_standard > 150) { |
mbedAustin | 0:885417624ec2 | 93 | printf("sensor_value:%d",first_vol); |
mbedAustin | 0:885417624ec2 | 94 | printf("\t High pollution!\n\r"); |
mbedAustin | 0:885417624ec2 | 95 | timer_index = 0; |
mbedAustin | 0:885417624ec2 | 96 | avgVoltage(); |
mbedAustin | 0:885417624ec2 | 97 | return 1; |
mbedAustin | 0:885417624ec2 | 98 | |
mbedAustin | 0:885417624ec2 | 99 | } else if((first_vol-last_vol > 200 && first_vol < 700) || first_vol - vol_standard > 50) { |
mbedAustin | 0:885417624ec2 | 100 | //printf(first_vol-last_vol); |
mbedAustin | 0:885417624ec2 | 101 | printf("sensor_value:%d",first_vol); |
mbedAustin | 0:885417624ec2 | 102 | printf("\t Low pollution!\n\r"); |
mbedAustin | 0:885417624ec2 | 103 | timer_index = 0; |
mbedAustin | 0:885417624ec2 | 104 | avgVoltage(); |
mbedAustin | 0:885417624ec2 | 105 | return 2; |
mbedAustin | 0:885417624ec2 | 106 | } else { |
mbedAustin | 0:885417624ec2 | 107 | avgVoltage(); |
mbedAustin | 0:885417624ec2 | 108 | printf("sensor_value:%d",first_vol); |
mbedAustin | 0:885417624ec2 | 109 | printf("\t Air fresh\n\r"); |
mbedAustin | 0:885417624ec2 | 110 | timer_index = 0; |
mbedAustin | 0:885417624ec2 | 111 | return 3; |
mbedAustin | 0:885417624ec2 | 112 | } |
mbedAustin | 0:885417624ec2 | 113 | } |
mbedAustin | 0:885417624ec2 | 114 | return -1; |
mbedAustin | 0:885417624ec2 | 115 | } |