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.

109 lines
2.7 KiB

4 years ago
  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 = 13;
  15. //globals for ppd42 dust sensor
  16. //let's start with 100 readings. (copy and paste)
  17. 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};
  18. unsigned long starttime = 0;
  19. unsigned long sampletime_ms = 60000;//sample 60s == 60000 ;
  20. unsigned long lowpulseoccupancy = 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. void setup(void)
  32. {
  33. Serial.begin(9600);
  34. uint16_t ID = tft.readID();
  35. if (ID == 0xD3) ID = 0x9481;
  36. tft.begin(ID);
  37. tft.setRotation(1);
  38. tft.WriteCmdData(0x51, 0x00);
  39. //resultsread1 = tft.readReg(0x52);
  40. //writeData(0x51);
  41. pinMode(DustSensePin,INPUT);
  42. starttime = millis();//get the current time;
  43. }
  44. void loop(void)
  45. {
  46. tft.fillScreen(BLACK);
  47. tft.WriteCmdData(0x51, 0x0000);
  48. tft.setTextColor(0XFFFF);
  49. tft.setTextSize(2);
  50. tft.setCursor(0, 36);
  51. tft.println("running sensor now");
  52. airreading[x] = pulseIn(DustSensePin, LOW, 500000);
  53. tft.println("Results are in:");
  54. //tft.println(airreading)
  55. if ((millis()-starttime) > sampletime_ms)//if the sample time == 30s
  56. {
  57. ratio = lowpulseoccupancy/(sampletime_ms*10.0); // Integer percentage 0=>100
  58. concentration = 1.1*powf(ratio,3)-3.8*powf(ratio,2)+520*ratio+0.62; // using spec sheet curve
  59. tft.println(lowpulseoccupancy);
  60. //tft.print(",");
  61. tft.println(ratio);
  62. //Serial.print(",");
  63. tft.println(concentration);
  64. lowpulseoccupancy = 0;
  65. starttime = millis();
  66. }
  67. }
  68. void showmsgXY(int x, int y, int sz, const GFXfont *f, const char *msg)
  69. {
  70. int16_t x1, y1;
  71. uint16_t wid, ht;
  72. tft.drawFastHLine(0, y, tft.width(), WHITE);
  73. tft.setFont(f);
  74. tft.setCursor(x, y);
  75. tft.setTextColor(GREEN);
  76. tft.setTextSize(sz);
  77. tft.print(msg);
  78. delay(1000);
  79. }
  80. uint32_t readReg32(uint16_t reg)
  81. {
  82. uint16_t h = tft.readReg(reg, 0);
  83. uint16_t l = tft.readReg(reg, 1);
  84. return ((uint32_t) h << 16) | (l);
  85. }