Advertisement
Guest User

Untitled

a guest
Sep 14th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2. #include <pms.h>
  3.  
  4. ////////////////////////////////////////
  5.  
  6. #define RX_PIN 5
  7. #define TX_PIN 4
  8.  
  9. #if defined PMS_DYNAMIC
  10. Pmsx003 *pms_ = nullptr;
  11. #define pms (*pms_)
  12. #else
  13. // RX, TX
  14. // Defining the pins connected
  15. Pmsx003 pms(RX_PIN, TX_PIN);
  16. #endif
  17.  
  18. ////////////////////////////////////////
  19.  
  20. void setup(void) {
  21.     Serial.begin(115200);
  22.     while (!Serial) {};
  23.     Serial.println("Pmsx003");
  24.  
  25. #if defined PMS_DYNAMIC
  26.     pms_ = new Pmsx003(RX_PIN, TX_PIN);
  27. #else
  28.     pms.begin();
  29. #endif
  30.  
  31.   // Waiting to receive data until one is send
  32.     pms.waitForData(Pmsx003::wakeupTime);
  33.   Serial.println("Wait for data");
  34.     pms.write(Pmsx003::cmdModeActive);
  35.   Serial.println("Read data");
  36. }
  37.  
  38. ////////////////////////////////////////
  39.  
  40. auto lastRead = millis();
  41.  
  42. void loop(void) {
  43.   //Serial.println("Entered loop");
  44.  
  45.     const Pmsx003::pmsIdx n = Pmsx003::nValues_PmsDataNames;
  46.     Pmsx003::pmsData data[n];
  47.  
  48.   //Serial.println("Print Data");
  49.  
  50.     auto t0Read = millis();
  51.     Pmsx003::PmsStatus status = pms.read(data, n);
  52.     auto t1Read = millis();
  53.  
  54.     switch (status) {
  55.         case Pmsx003::OK:
  56.         {
  57.     //calculating how much time needed for reading
  58.             Serial.print("_________________ time of read(): ");
  59.             Serial.print(t1Read - t0Read);
  60.             Serial.println(" msec");
  61.             auto newRead = millis();
  62.       //calculating how much time needed for waiting
  63.             Serial.print("Wait time ");
  64.             Serial.println(newRead - lastRead);
  65.             lastRead = newRead;
  66.  
  67.             for (Pmsx003::pmsIdx i = 0; i < n; ++i) {
  68.                 Serial.print(data[i]);
  69.                 Serial.print("\t");
  70.                 Serial.print(Pmsx003::getDataNames(i));
  71.                 Serial.print(" [");
  72.                 Serial.print(Pmsx003::getMetrics(i));
  73.                 Serial.print("]");
  74.                 Serial.println();
  75.             }
  76.             break;
  77.         }
  78.         case Pmsx003::noData:
  79.             break;
  80.         default:
  81.             Serial.println("_________________");
  82.             Serial.println(Pmsx003::errorMsg[status]);
  83.     };
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement