Air_Quality_Sensor.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Air_Quality_Sensor.cpp
  3. * Source file for Grove - Air Quality Sensor.
  4. *
  5. * Copyright (c) 2019 seeed technology inc.
  6. * Author : Lets Blu
  7. * Created Time : Jan 2019
  8. * Modified Time:
  9. *
  10. * The MIT License (MIT)
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a copy
  13. * of this software and associated documentation files (the "Software"), to deal
  14. * in the Software without restriction, including without limitation the rights
  15. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. * copies of the Software, and to permit persons to whom the Software is
  17. * furnished to do so, subject to the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included in
  20. * all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. * THE SOFTWARE.
  29. */
  30. #include "Air_Quality_Sensor.h"
  31. const int AirQualitySensor::FORCE_SIGNAL = 0;
  32. const int AirQualitySensor::HIGH_POLLUTION = 1;
  33. const int AirQualitySensor::LOW_POLLUTION = 2;
  34. const int AirQualitySensor::FRESH_AIR = 3;
  35. AirQualitySensor::AirQualitySensor(int pin)
  36. : _pin(pin), _voltageSum(0), _volSumCount(0) {
  37. // do nothing
  38. }
  39. bool AirQualitySensor::init(void) {
  40. int initVoltage = analogRead(_pin);
  41. if (10 < initVoltage && initVoltage < 798) {
  42. _currentVoltage = initVoltage;
  43. _lastVoltage = _currentVoltage;
  44. _standardVoltage = initVoltage;
  45. _lastStdVolUpdated = millis();
  46. return true;
  47. }
  48. else {
  49. return false;
  50. }
  51. }
  52. int AirQualitySensor::slope(void) {
  53. _lastVoltage = _currentVoltage;
  54. _currentVoltage = analogRead(_pin);
  55. _voltageSum += _currentVoltage;
  56. _volSumCount += 1;
  57. updateStandardVoltage();
  58. if (_currentVoltage - _lastVoltage > 400 || _currentVoltage > 700) {
  59. return AirQualitySensor::FORCE_SIGNAL;
  60. }
  61. else if ((_currentVoltage - _lastVoltage > 400 && _currentVoltage < 700)
  62. || _currentVoltage - _standardVoltage > 150) {
  63. return AirQualitySensor::HIGH_POLLUTION;
  64. }
  65. else if ((_currentVoltage - _lastVoltage > 200 && _currentVoltage < 700)
  66. || _currentVoltage - _standardVoltage > 50) {
  67. return AirQualitySensor::LOW_POLLUTION;
  68. }
  69. else {
  70. return AirQualitySensor::FRESH_AIR;
  71. }
  72. return -1;
  73. }
  74. int AirQualitySensor::getValue(void) {
  75. return _currentVoltage;
  76. }
  77. void AirQualitySensor::updateStandardVoltage(void) {
  78. if (millis() - _lastStdVolUpdated > 500000) {
  79. _standardVoltage = _voltageSum / _volSumCount;
  80. _lastStdVolUpdated = millis();
  81. _voltageSum = 0;
  82. _volSumCount = 0;
  83. }
  84. }