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