Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <pms.h>
- ////////////////////////////////////////
- #define RX_PIN 5
- #define TX_PIN 4
- #if defined PMS_DYNAMIC
- Pmsx003 *pms_ = nullptr;
- #define pms (*pms_)
- #else
- // RX, TX
- // Defining the pins connected
- Pmsx003 pms(RX_PIN, TX_PIN);
- #endif
- ////////////////////////////////////////
- void setup(void) {
- Serial.begin(115200);
- while (!Serial) {};
- Serial.println("Pmsx003");
- #if defined PMS_DYNAMIC
- pms_ = new Pmsx003(RX_PIN, TX_PIN);
- #else
- pms.begin();
- #endif
- // Waiting to receive data until one is send
- pms.waitForData(Pmsx003::wakeupTime);
- Serial.println("Wait for data");
- pms.write(Pmsx003::cmdModeActive);
- Serial.println("Read data");
- }
- ////////////////////////////////////////
- auto lastRead = millis();
- void loop(void) {
- //loop used to read the data
- const Pmsx003::pmsIdx n = Pmsx003::nValues_PmsDataNames;
- Pmsx003::pmsData data[n]; // array to store sent data
- auto t0Read = millis(); // reading status of data, whether it is sent correctly
- Pmsx003::PmsStatus status = pms.read(data, n);
- auto t1Read = millis();
- switch (status) {
- case Pmsx003::OK: // if status received was OK
- {
- //calculating how much time needed for reading
- Serial.print("_________________ time of read(): ");
- Serial.print(t1Read - t0Read);
- Serial.println(" msec");
- auto newRead = millis();
- //calculating how much time needed for waiting
- Serial.print("Wait time ");
- Serial.println(newRead - lastRead);
- lastRead = newRead;
- //printing info for reach loop, if data received
- for (Pmsx003::pmsIdx i = 0; i < n; ++i) {
- Serial.print(data[i]);
- Serial.print("\t");
- Serial.print(Pmsx003::getDataNames(i));
- Serial.print(" [");
- Serial.print(Pmsx003::getMetrics(i));
- Serial.print("]");
- Serial.println();
- }
- break;
- }
- case Pmsx003::noData: // break if no data was send
- break;
- default: // In case of bad connection, send error message
- Serial.println("_________________");
- Serial.println(Pmsx003::errorMsg[status]);
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement