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.

130 lines
3.2 KiB

  1. #include <Adafruit_GFX.h> // Core graphics library
  2. #include <MCUFRIEND_kbv.h> // Hardware-specific library
  3. MCUFRIEND_kbv tft;
  4. #include <Fonts/FreeSans9pt7b.h>
  5. #include <Fonts/FreeSans12pt7b.h>
  6. #include <Fonts/FreeSerif12pt7b.h>
  7. #include <FreeDefaultFonts.h>
  8. #define BLACK 0x0000
  9. #define RED 0xF800
  10. #define GREEN 0x07E0
  11. #define WHITE 0xFFFF
  12. #define GREY 0x8410
  13. #include <math.h>
  14. int DustSensePin = 31;
  15. //globals for ppd42 dust sensor
  16. //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};
  17. unsigned long starttime = 0;
  18. unsigned long sampletime_ms = 10000;//sample 60s == 60000 ;
  19. unsigned long lowpulseoccupancy = 0;
  20. unsigned long duration = 0;
  21. float ratio = 0;
  22. float concentration = 0;
  23. unsigned int x = 0;
  24. unsigned int toplimit = 0;
  25. unsigned long sum = 0; //highest number possible - 32 bit unsigned, so 4 billion
  26. unsigned int average = 0;
  27. //end ppd42
  28. int brightnessread = 0;
  29. int cat = 0;
  30. int resultsread1 = 0;
  31. int screenreadyforprint = 0;
  32. double count = 0;
  33. void setup(void)
  34. {
  35. Serial.begin(9600);
  36. uint16_t ID = tft.readID();
  37. if (ID == 0xD3) ID = 0x9481;
  38. tft.begin(ID);
  39. tft.setRotation(1);
  40. tft.WriteCmdData(0x51, 0x00);
  41. //resultsread1 = tft.readReg(0x52);
  42. //writeData(0x51);
  43. pinMode(DustSensePin,INPUT);
  44. starttime = millis();//get the current time;
  45. }
  46. void loop(void)
  47. {
  48. if(screenreadyforprint == 0){
  49. tft.fillScreen(BLACK);
  50. tft.WriteCmdData(0x51, 0x0000);
  51. tft.setTextColor(0XFFFF);
  52. tft.setTextSize(2);
  53. tft.setCursor(0, 36);
  54. tft.println("Starting Sensor");
  55. screenreadyforprint = 1;
  56. }
  57. duration = pulseIn(DustSensePin, LOW, 500000);
  58. lowpulseoccupancy = lowpulseoccupancy + duration;
  59. //tft.println(airreading)
  60. if ((millis()-starttime) > sampletime_ms)//if the sample time == 30s
  61. {
  62. tft.fillScreen(BLACK);
  63. tft.WriteCmdData(0x51, 0x0000);
  64. tft.setTextColor(0XFFFF);
  65. tft.setTextSize(2);
  66. tft.setCursor(0, 36);
  67. ratio = lowpulseoccupancy/(sampletime_ms*10.0); // Integer percentage 0=>100
  68. concentration = 1.1*powf(ratio,3)-3.8*powf(ratio,2)+520*ratio+0.62; // using spec sheet curve
  69. tft.print("Count: ");
  70. tft.println(count);
  71. tft.println(lowpulseoccupancy);
  72. //tft.print(",");
  73. tft.println(ratio);
  74. //tft.print(",");
  75. tft.print("Concentration is: ");
  76. tft.println(concentration);
  77. lowpulseoccupancy = 0;
  78. starttime = millis();
  79. //tft.println("Results printed.");
  80. count++;
  81. }
  82. }
  83. void showmsgXY(int x, int y, int sz, const GFXfont *f, const char *msg)
  84. {
  85. int16_t x1, y1;
  86. uint16_t wid, ht;
  87. tft.drawFastHLine(0, y, tft.width(), WHITE);
  88. tft.setFont(f);
  89. tft.setCursor(x, y);
  90. tft.setTextColor(GREEN);
  91. tft.setTextSize(sz);
  92. tft.print(msg);
  93. delay(1000);
  94. }
  95. uint32_t readReg32(uint16_t reg)
  96. {
  97. uint16_t h = tft.readReg(reg, 0);
  98. uint16_t l = tft.readReg(reg, 1);
  99. return ((uint32_t) h << 16) | (l);
  100. }