AirQuality.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. AirQuality library v1.0
  3. 2010 Copyright (c) Seeed Technology Inc. All right reserved.
  4. Original Author: Bruce.Qin
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #include"Arduino.h"
  18. #include"AirQuality.h"
  19. //Get the avg voltage in 5 minutes.
  20. void AirQuality::avgVoltage()
  21. {
  22. if(i==150)//sum 5 minutes
  23. {
  24. vol_standard=temp/150;
  25. temp=0;
  26. Serial.print("Vol_standard in 5 minutes:");
  27. Serial.println(vol_standard);
  28. i=0;
  29. }
  30. else
  31. {
  32. temp+=first_vol;
  33. i++;
  34. }
  35. }
  36. void AirQuality::init(int pin)
  37. {
  38. _pin=pin;
  39. pinMode(_pin,INPUT);
  40. unsigned char i=0;
  41. Serial.println("sys_starting...");
  42. delay(20000);//200000
  43. init_voltage=analogRead(_pin);
  44. Serial.println("The init voltage is ...");
  45. Serial.println(init_voltage);
  46. while(init_voltage)
  47. {
  48. if(init_voltage<798 && init_voltage>10)// the init voltage is ok
  49. {
  50. first_vol=analogRead(_pin);//initialize first value
  51. last_vol=first_vol;
  52. vol_standard=last_vol;
  53. Serial.println("Sensor ready.");
  54. error=false;;
  55. break;
  56. }
  57. else if(init_voltage>798||init_voltage<=10)
  58. {
  59. i++;
  60. delay(60000);//60000
  61. Serial.println("waitting sensor init..");
  62. init_voltage=analogRead(_pin);
  63. if(i==5)
  64. {
  65. i=0;
  66. error=true;
  67. Serial.println("Sensor Error!");
  68. }
  69. }
  70. else
  71. break;
  72. }
  73. //init the timer
  74. TCCR2A=0;//normal model
  75. TCCR2B=0x07;//set clock as 1024*(1/16M)
  76. TIMSK2=0x01;//enable overflow interrupt
  77. Serial.println("Test begin...");
  78. sei();
  79. }
  80. int AirQuality::slope(void)
  81. {
  82. while(timer_index)
  83. {
  84. if(first_vol-last_vol>400||first_vol>700)
  85. {
  86. Serial.println("High pollution! Force signal active.");
  87. timer_index=0;
  88. avgVoltage();
  89. return 0;
  90. }
  91. else if((first_vol-last_vol>400&&first_vol<700)||first_vol-vol_standard>150)
  92. {
  93. Serial.print("sensor_value:");
  94. Serial.print(first_vol);
  95. Serial.println("\t High pollution!");
  96. timer_index=0;
  97. avgVoltage();
  98. return 1;
  99. }
  100. else if((first_vol-last_vol>200&&first_vol<700)||first_vol-vol_standard>50)
  101. {
  102. //Serial.println(first_vol-last_vol);
  103. Serial.print("sensor_value:");
  104. Serial.print(first_vol);
  105. Serial.println("\t Low pollution!");
  106. timer_index=0;
  107. avgVoltage();
  108. return 2;
  109. }
  110. else
  111. {
  112. avgVoltage();
  113. Serial.print("sensor_value:");
  114. Serial.print(first_vol);
  115. Serial.println("\t Air fresh");
  116. timer_index=0;
  117. return 3;
  118. }
  119. }
  120. return -1;
  121. }