Grove Air Quality Sensor.
Fork of Grove_Air_Quality_Sensor_Library by
Diff: Air_Quality.cpp
- Revision:
- 4:0ca4a9fd1b5d
- Parent:
- 3:c25365a52d78
- Child:
- 7:38425a51906f
--- a/Air_Quality.cpp Fri Dec 23 22:31:17 2016 +0200 +++ b/Air_Quality.cpp Tue Dec 27 12:34:54 2016 +0200 @@ -20,46 +20,60 @@ */ #include "Air_Quality.h" -//Get the avg voltage in 5 minutes. -void AirQuality::avgVoltage() { - if (i == 150) { //sum 5 minutes - standard_vol = temp / 150; - temp = 0; - debug_if(DEBUG_AIR_QUALITY, "Vol_standard in 5 minutes: %d", standard_vol); - i = 0; +AirQuality::AirQuality() { + _s = 0; + _sum_vol = 0; +} + +void AirQuality::set_calc_avg_volt_period(uint16_t seconds) { + _calc_avg_volt_period_s = seconds; +} + +void AirQuality::set_sampling_period(uint16_t seconds) { + _sampling_period_s = seconds; +} + +void AirQuality::calcAvgVoltageFor(uint16_t seconds) { + uint16_t s = (uint16_t) (seconds / _sampling_period_s); + if (_s == s) { + standard_vol = _sum_vol / s; + _sum_vol = 0; + _s = 0; + debug_if(DEBUG_AIR_QUALITY, "Vol standard in %d seconds: %d\r\n", seconds, standard_vol); } else { - temp += first_vol; - i++; + _sum_vol += first_vol; + _s++; } } void AirQuality::init(PinName pin) { _pin = pin; AnalogIn sensor(_pin); - unsigned char i = 0; + unsigned char minutes = 0; debug_if(DEBUG_AIR_QUALITY, "Air Quality Sensor Starting Up...(20s)"); - wait(20); //20s + wait(20); 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)) { + if ((init_voltage > 10) && (init_voltage < 798)) { // the init voltage is ok first_vol = (uint16_t) (sensor.read() * 1000); //initialize first value last_vol = first_vol; standard_vol = last_vol; debug_if(DEBUG_AIR_QUALITY, "Sensor ready."); - error = false;; + _has_error = false; break; - } else if (init_voltage > 798 || init_voltage <= 10) { + } else if (init_voltage <= 10 || init_voltage > 798) { // The sensor is not ready, wait a bit for it to cool off - i++; - debug_if(DEBUG_AIR_QUALITY, "Sensor not ready (%d), try %d/5, waiting 60 seconds...", init_voltage, i); - wait(60);//60s + minutes++; + debug_if(DEBUG_AIR_QUALITY, "Sensor not ready (%d), try %d/5, waiting 60 seconds...", init_voltage, + minutes); + wait(60); init_voltage = (uint16_t) (sensor.read() * 1000); - if (i == 5) { + if (minutes == 5) { // After 5 minutes warn user that the sensor may be broken - i = 0; - error = true; + minutes = 0; + _has_error = true; debug_if(DEBUG_AIR_QUALITY, "Sensor Error! You may have a bad sensor. :-("); } } else @@ -68,33 +82,30 @@ } air_quality_values AirQuality::slope(void) { - while (timer_index) { + air_quality_values ret = UNKNOWN; + + while (_was_sampled) { + debug_if(DEBUG_AIR_QUALITY, "sensor_value: %d\r\n", first_vol); + if (first_vol - last_vol > 400 || first_vol > 700) { - debug_if(DEBUG_AIR_QUALITY, "Very high pollution! Force signal active."); - timer_index = 0; - avgVoltage(); - return VERY_HIGH_POLLUTION; + debug_if(DEBUG_AIR_QUALITY, "Very high pollution! Force signal active.\r\n"); + ret = VERY_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; + debug_if(DEBUG_AIR_QUALITY, "High pollution!\r\n"); + ret = HIGH_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; + debug_if(DEBUG_AIR_QUALITY, "Low pollution!\r\n"); + ret = LOW_POLLUTION; } else { - avgVoltage(); - debug_if(DEBUG_AIR_QUALITY, "sensor_value:%d", first_vol); - debug_if(DEBUG_AIR_QUALITY, "No pollution"); - timer_index = 0; - return NO_POLLUTION; + debug_if(DEBUG_AIR_QUALITY, "No pollution\r\n"); + ret = NO_POLLUTION; } + + _was_sampled = false; + calcAvgVoltageFor(_calc_avg_volt_period_s); + return ret; } - return UNKNOWN; + return ret; } +