Grove Air Quality Sensor library. Simple interrupt driven library that polls the sensor every 2 seconds and reports on the air quality.

Fork of Grove_Air_Quality_Sensor_Library by Austin Blackstone

Committer:
mbedAustin
Date:
Fri Sep 05 19:13:07 2014 +0000
Revision:
0:885417624ec2
Added Air Quality Sensor Library and expanded application to be interrupt driven.

Who changed what in which revision?

UserRevisionLine numberNew 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");
mbedAustin 0:885417624ec2 51 wait(20); //20s
mbedAustin 0:885417624ec2 52 init_voltage = sensor.read() * 1000; // boost the value to be on a 0 -> 1000 scale for compatibility
mbedAustin 0:885417624ec2 53 printf("The initial voltage is %d%% of source \n\r",init_voltage/10);
mbedAustin 0:885417624ec2 54 while(init_voltage) {
mbedAustin 0:885417624ec2 55 if((init_voltage < 798) && (init_voltage > 10)) {
mbedAustin 0:885417624ec2 56 // the init voltage is ok
mbedAustin 0:885417624ec2 57 first_vol = sensor.read() * 1000;//initialize first value
mbedAustin 0:885417624ec2 58 last_vol = first_vol;
mbedAustin 0:885417624ec2 59 vol_standard = last_vol;
mbedAustin 0:885417624ec2 60 printf("Sensor ready.\n\r");
mbedAustin 0:885417624ec2 61 error = false;;
mbedAustin 0:885417624ec2 62 break;
mbedAustin 0:885417624ec2 63 } else if(init_voltage > 798 || init_voltage <= 10) {
mbedAustin 0:885417624ec2 64 // The sensor is not ready, wait a bit for it to cool off
mbedAustin 0:885417624ec2 65 i++;
mbedAustin 0:885417624ec2 66 printf("Sensor not ready (%d), try %d/5, waiting 60 seconds...\n\r",init_voltage,i);
mbedAustin 0:885417624ec2 67 wait(60);//60s
mbedAustin 0:885417624ec2 68 init_voltage = sensor.read() * 1000;
mbedAustin 0:885417624ec2 69 if(i==5) {
mbedAustin 0:885417624ec2 70 // After 5 minutes warn user that the sensor may be broken
mbedAustin 0:885417624ec2 71 i = 0;
mbedAustin 0:885417624ec2 72 error = true;
mbedAustin 0:885417624ec2 73 printf("Sensor Error! You may have a bad sensor. :-(\n\r");
mbedAustin 0:885417624ec2 74 }
mbedAustin 0:885417624ec2 75 } else
mbedAustin 0:885417624ec2 76 break;
mbedAustin 0:885417624ec2 77 }
mbedAustin 0:885417624ec2 78 // Call AirQualityInterrupt every 2seconds
mbedAustin 0:885417624ec2 79 IntHandler.attach(IRQ, 2.0);
mbedAustin 0:885417624ec2 80 printf("Test begin...\n\r");
mbedAustin 0:885417624ec2 81 }
mbedAustin 0:885417624ec2 82
mbedAustin 0:885417624ec2 83 int AirQuality::slope(void)
mbedAustin 0:885417624ec2 84 {
mbedAustin 0:885417624ec2 85 while(timer_index) {
mbedAustin 0:885417624ec2 86 if(first_vol-last_vol > 400 || first_vol > 700) {
mbedAustin 0:885417624ec2 87 printf("High pollution! Force signal active.\n\r");
mbedAustin 0:885417624ec2 88 timer_index = 0;
mbedAustin 0:885417624ec2 89 avgVoltage();
mbedAustin 0:885417624ec2 90 return 0;
mbedAustin 0:885417624ec2 91 } else if((first_vol - last_vol > 400 && first_vol < 700) || first_vol - vol_standard > 150) {
mbedAustin 0:885417624ec2 92 printf("sensor_value:%d",first_vol);
mbedAustin 0:885417624ec2 93 printf("\t High pollution!\n\r");
mbedAustin 0:885417624ec2 94 timer_index = 0;
mbedAustin 0:885417624ec2 95 avgVoltage();
mbedAustin 0:885417624ec2 96 return 1;
mbedAustin 0:885417624ec2 97
mbedAustin 0:885417624ec2 98 } else if((first_vol-last_vol > 200 && first_vol < 700) || first_vol - vol_standard > 50) {
mbedAustin 0:885417624ec2 99 //printf(first_vol-last_vol);
mbedAustin 0:885417624ec2 100 printf("sensor_value:%d",first_vol);
mbedAustin 0:885417624ec2 101 printf("\t Low pollution!\n\r");
mbedAustin 0:885417624ec2 102 timer_index = 0;
mbedAustin 0:885417624ec2 103 avgVoltage();
mbedAustin 0:885417624ec2 104 return 2;
mbedAustin 0:885417624ec2 105 } else {
mbedAustin 0:885417624ec2 106 avgVoltage();
mbedAustin 0:885417624ec2 107 printf("sensor_value:%d",first_vol);
mbedAustin 0:885417624ec2 108 printf("\t Air fresh\n\r");
mbedAustin 0:885417624ec2 109 timer_index = 0;
mbedAustin 0:885417624ec2 110 return 3;
mbedAustin 0:885417624ec2 111 }
mbedAustin 0:885417624ec2 112 }
mbedAustin 0:885417624ec2 113 return -1;
mbedAustin 0:885417624ec2 114 }