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