Grove Air Quality Sensor.

Fork of Grove_Air_Quality_Sensor_Library by Seeed

Revision:
3:c25365a52d78
Parent:
2:95009177a860
Child:
4:0ca4a9fd1b5d
--- a/Air_Quality.cpp	Fri Dec 23 20:19:35 2016 +0200
+++ b/Air_Quality.cpp	Fri Dec 23 22:31:17 2016 +0200
@@ -20,15 +20,12 @@
 */
 #include "Air_Quality.h"
 
-// Interrupt Handler Assignment
-Ticker IntHandler;
-
 //Get the avg voltage in 5 minutes.
 void AirQuality::avgVoltage() {
     if (i == 150) { //sum 5 minutes
-        vol_standard = temp / 150;
+        standard_vol = temp / 150;
         temp = 0;
-        debug("Vol_standard in 5 minutes: %d", vol_standard);
+        debug_if(DEBUG_AIR_QUALITY, "Vol_standard in 5 minutes: %d", standard_vol);
         i = 0;
     } else {
         temp += first_vol;
@@ -36,68 +33,65 @@
     }
 }
 
-void AirQuality::init(PinName pin, void(*IRQ)(void)) {
+void AirQuality::init(PinName pin) {
     _pin = pin;
     AnalogIn sensor(_pin);
     unsigned char i = 0;
-    debug("Air Quality Sensor Starting Up...(20s)");
+    debug_if(DEBUG_AIR_QUALITY, "Air Quality Sensor Starting Up...(20s)");
     wait(20); //20s
-    init_voltage = (int) (sensor.read() * 1000); // boost the value to be on a 0 -> 1000 scale for compatibility
-    debug("The initial voltage is %d%% of source ", init_voltage / 10);
+    init_voltage = (uint16_t) (sensor.read() * 1000); // boost the value to be on a 0 -> 1000 scale for compatibility
+    debug_if(DEBUG_AIR_QUALITY, "The initial voltage is %d%% of source ", init_voltage / 10);
     while (init_voltage) {
         if ((init_voltage < 798) && (init_voltage > 10)) {
             // the init voltage is ok
-            first_vol = (int) (sensor.read() * 1000);//initialize first value
+            first_vol = (uint16_t) (sensor.read() * 1000); //initialize first value
             last_vol = first_vol;
-            vol_standard = last_vol;
-            debug("Sensor ready.");
+            standard_vol = last_vol;
+            debug_if(DEBUG_AIR_QUALITY, "Sensor ready.");
             error = false;;
             break;
         } else if (init_voltage > 798 || init_voltage <= 10) {
             // The sensor is not ready, wait a bit for it to cool off
             i++;
-            debug("Sensor not ready (%d), try %d/5, waiting 60 seconds...", init_voltage, i);
+            debug_if(DEBUG_AIR_QUALITY, "Sensor not ready (%d), try %d/5, waiting 60 seconds...", init_voltage, i);
             wait(60);//60s
-            init_voltage = (int) (sensor.read() * 1000);
+            init_voltage = (uint16_t) (sensor.read() * 1000);
             if (i == 5) {
                 // After 5 minutes warn user that the sensor may be broken
                 i = 0;
                 error = true;
-                debug("Sensor Error! You may have a bad sensor. :-(");
+                debug_if(DEBUG_AIR_QUALITY, "Sensor Error! You may have a bad sensor. :-(");
             }
         } else
             break;
     }
-    // Call AirQualityInterrupt every 2 seconds
-    IntHandler.attach(IRQ, 2.0);
-    debug("Test begin...");
 }
 
 air_quality_values AirQuality::slope(void) {
     while (timer_index) {
         if (first_vol - last_vol > 400 || first_vol > 700) {
-            debug("Very high pollution! Force signal active.");
+            debug_if(DEBUG_AIR_QUALITY, "Very high pollution! Force signal active.");
             timer_index = 0;
             avgVoltage();
             return VERY_HIGH_POLLUTION;
-        } else if ((first_vol - last_vol > 400 && first_vol < 700) || first_vol - vol_standard > 150) {
-            debug("sensor_value:%d", first_vol);
-            debug("High pollution!");
+        } else if ((first_vol - last_vol > 400 && first_vol < 700) || first_vol - standard_vol > 150) {
+            debug_if(DEBUG_AIR_QUALITY, "sensor_value:%d", first_vol);
+            debug_if(DEBUG_AIR_QUALITY, "High pollution!");
             timer_index = 0;
             avgVoltage();
             return HIGH_POLLUTION;
 
-        } else if ((first_vol - last_vol > 200 && first_vol < 700) || first_vol - vol_standard > 50) {
-            //debug(first_vol-last_vol);
-            debug("sensor_value:%d", first_vol);
-            debug("Low pollution!");
+        } else if ((first_vol - last_vol > 200 && first_vol < 700) || first_vol - standard_vol > 50) {
+//            debug_if( DEBUG_AIR_QUALITY, first_vol - last_vol);
+            debug_if(DEBUG_AIR_QUALITY, "sensor_value:%d", first_vol);
+            debug_if(DEBUG_AIR_QUALITY, "Low pollution!");
             timer_index = 0;
             avgVoltage();
             return LOW_POLLUTION;
         } else {
             avgVoltage();
-            debug("sensor_value:%d", first_vol);
-            debug("No pollution");
+            debug_if(DEBUG_AIR_QUALITY, "sensor_value:%d", first_vol);
+            debug_if(DEBUG_AIR_QUALITY, "No pollution");
             timer_index = 0;
             return NO_POLLUTION;
         }