Seo-young Yoon / Air_Quality

Dependents:   mbed_Wiznet_W7500

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Air_Quality.cpp Source File

Air_Quality.cpp

00001 //
00002 // modified by mbed group for use with the mbed platform
00003 // modification date 9/4/2014
00004 //
00005 
00006 /*
00007   AirQuality library v1.0
00008   2010 Copyright (c) Seeed Technology Inc.  All right reserved.
00009 
00010   Original Author: Bruce.Qin
00011 
00012   This library is free software; you can redistribute it and/or
00013   modify it under the terms of the GNU Lesser General Public
00014   License as published by the Free Software Foundation; either
00015   version 2.1 of the License, or (at your option) any later version.
00016 
00017   This library is distributed in the hope that it will be useful,
00018   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020   Lesser General Public License for more details.
00021 
00022   You should have received a copy of the GNU Lesser General Public
00023   License along with this library; if not, write to the Free Software
00024   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00025 */
00026 #include"Air_Quality.h"
00027 
00028 // Interrupt Handler Assignment
00029 Ticker IntHandler;
00030 
00031 //Get the avg voltage in 5 minutes.
00032 void AirQuality::avgVoltage()
00033 {
00034     if(i==150) { //sum 5 minutes
00035         vol_standard=temp/150;
00036         temp=0;
00037         printf("Vol_standard in 5 minutes: %d\n\r",vol_standard);
00038         i=0;
00039     } else {
00040         temp+=first_vol;
00041         i++;
00042     }
00043 }
00044 
00045 void AirQuality::init(PinName pin, void(*IRQ)(void))
00046 {
00047     _pin=pin;
00048     AnalogIn sensor(_pin);
00049     unsigned char i = 0;
00050     wait(2); //2s
00051     init_voltage = sensor.read() * 1000; // boost the value to be on a 0 -> 1000 scale for compatibility
00052      while(init_voltage) {
00053         if((init_voltage < 798) && (init_voltage > 10)) {
00054             // the init voltage is ok
00055             first_vol = sensor.read() * 1000;//initialize first value
00056             last_vol = first_vol;
00057             vol_standard = last_vol;
00058             error = false;;
00059             break;
00060         } else if(init_voltage > 798 || init_voltage <= 10) {
00061             // The sensor is not ready, wait a bit for it to cool off
00062             i++;
00063             wait(60);//60s
00064             init_voltage = sensor.read() * 1000;
00065             if(i==5) {
00066                 // After 5 minutes warn user that the sensor may be broken
00067                 i = 0;
00068                 error = true;
00069                 printf("Sensor Error! You may have a bad sensor. :-(\n\r");
00070             }
00071         } else
00072             break;
00073     }
00074     // Call AirQualityInterrupt every 2seconds
00075     IntHandler.attach(IRQ, 2.0);
00076 }
00077 
00078 int AirQuality::slope(void)
00079 {
00080     while(timer_index) {
00081         if(first_vol-last_vol > 400 || first_vol > 700) {
00082             printf("High pollution! Force signal active.\n\r");
00083             timer_index = 0;
00084             avgVoltage(); 
00085             return 0;
00086         } else if((first_vol - last_vol > 400 && first_vol < 700) || first_vol - vol_standard > 150) {
00087             printf("Air_quality:%d",first_vol);
00088             printf("\t High pollution!\n\r");
00089             timer_index = 0;
00090             avgVoltage();
00091             return 1;
00092 
00093         } else if((first_vol-last_vol > 200 && first_vol < 700) || first_vol - vol_standard > 50) {
00094             //printf(first_vol-last_vol);
00095             printf("Air_quality:%d",first_vol);
00096             printf("\t Low pollution!\n\r");
00097             timer_index = 0;
00098             avgVoltage();
00099             return 2;
00100         } else {
00101             avgVoltage();
00102             printf("Air_quality:%d",first_vol);
00103             printf("\t Air fresh\n\r");
00104             timer_index = 0;
00105             return 3;
00106         }
00107     }
00108     return -1;
00109 }