You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.8 KiB

5 years ago
  1. #include <math.h>
  2. int DustSensePin = 13;
  3. //let's start with 100 readings. (copy and paste)
  4. unsigned int airreading[100] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  5. unsigned long starttime = 0;
  6. unsigned long sampletime_ms = 60000;//sample 60s == 60000 ;
  7. unsigned long lowpulseoccupancy = 0;
  8. float ratio = 0;
  9. float concentration = 0;
  10. unsigned int x = 0;
  11. unsigned int toplimit = 0;
  12. unsigned long sum = 0; //highest number possible - 32 bit unsigned, so 4 billion
  13. unsigned int average = 0;
  14. void setup()
  15. {
  16. Serial.begin(9600);
  17. Serial.println("Starting Air Sensor, please wait for readings...");
  18. pinMode(DustSensePin,INPUT);
  19. starttime = millis();//get the current time;
  20. }
  21. void loop()
  22. {
  23. airreading[x] = pulseIn(DustSensePin, LOW, 500000);
  24. /*lowpulseoccupancy = lowpulseoccupancy+duration;
  25. if ((millis()-starttime) > sampletime_ms)//if the sample time == 30s
  26. {
  27. ratio = lowpulseoccupancy/(sampletime_ms*10.0); // Integer percentage 0=>100
  28. concentration = 1.1*powf(ratio,3)-3.8*powf(ratio,2)+520*ratio+0.62; // using spec sheet curve
  29. Serial.print(lowpulseoccupancy);
  30. Serial.print(",");
  31. Serial.print(ratio);
  32. Serial.print(",");
  33. Serial.println(concentration);
  34. lowpulseoccupancy = 0;
  35. starttime = millis();
  36. }
  37. */
  38. //if ((millis()-starttime) > sampletime_ms){
  39. //KISS. Relative readings only.
  40. //Something else keeps the time.
  41. if (airreading[x] != 0){
  42. Serial.println(airreading[x]);
  43. x=x+1;
  44. if(x>100){
  45. x = 0;
  46. }
  47. }
  48. //starttime = millis();
  49. //}
  50. //KISS. Average for a minute, and store that data in a webpage (that part will be in ethernet sensor test 3).
  51. //
  52. if ((millis()-starttime) > sampletime_ms){
  53. toplimit = x; // don't read the zeros
  54. if(toplimit == 0){
  55. toplimit = 1; //to avoid divide by zero, in case sum / toplimit is both zero
  56. }
  57. for (x=0; x<toplimit; x++){
  58. sum = sum + airreading[x];
  59. }
  60. average = sum / toplimit;
  61. Serial.print("Average Get!: ");
  62. Serial.println(average); //average when 0 is 65535, so it's invalid (dividing by zero - sum divided by top limit which are both zero)
  63. //but we don't even want the average. It's just for testing here.
  64. Serial.print("Sum Get!: ");
  65. Serial.println(sum); //this is what we care about
  66. //reset everything after a minute
  67. sum = 0;
  68. average = 0;
  69. x = 0;
  70. toplimit = 0;
  71. for (x=0; x<toplimit; x++){
  72. airreading[x] = 0;
  73. }
  74. starttime = millis();
  75. }
  76. }