arduinoprojects

git clone https://git.tarina.org/arduinoprojects
Log | Files | Refs

dhtnew_setReadDelay.ino (2948B)


      1 //
      2 //    FILE: dhtnew_setReadDelay.ino
      3 //  AUTHOR: Rob Tillaart
      4 // VERSION: 0.1.2
      5 // PURPOSE: DHTNEW library waitForRead example sketch for Arduino
      6 //     URL: https://github.com/RobTillaart/DHTNew
      7 //
      8 // HISTORY:
      9 // 0.1.0    2020-06-12 initial version
     10 // 0.1.1    2020-06-15 match 0.3.0 error handling
     11 // 0.1.2    2020-11-09 fix + wait for read handling
     12 //
     13 // DHT PIN layout from left to right
     14 // =================================
     15 // FRONT : DESCRIPTION
     16 // pin 1 : VCC
     17 // pin 2 : DATA
     18 // pin 3 : Not Connected
     19 // pin 4 : GND
     20 
     21 #include <dhtnew.h>
     22 
     23 DHTNEW mySensor(16);
     24 
     25 void setup()
     26 {
     27   Serial.begin(115200);
     28   Serial.println(__FILE__);
     29   Serial.print("LIBRARY VERSION: ");
     30   Serial.println(DHTNEW_LIB_VERSION);
     31   Serial.println();
     32 
     33   delay(2000);  // boot time
     34 
     35   mySensor.setWaitForReading(true);
     36 
     37   uint16_t rd = 2000;
     38   uint16_t step = 2000;
     39 
     40   while (step)
     41   {
     42     step /= 2;
     43     mySensor.setReadDelay(rd);
     44     int chk = mySensor.read();
     45 
     46     Serial.print("ReadDelay (ms): ");
     47     Serial.print(mySensor.getReadDelay());
     48     Serial.print("\t T: ");
     49     Serial.print(mySensor.getTemperature(), 1);
     50     Serial.print("\t H: ");
     51     Serial.print(mySensor.getHumidity(), 1);
     52     Serial.print("\t");
     53     printStatus(chk);
     54 
     55     if (chk == DHTLIB_OK) rd -= step;
     56     else {
     57       rd += step;
     58       mySensor.read();
     59     }
     60   }
     61 
     62   // safety margin of 100 uSec
     63   rd += 100;
     64   mySensor.setReadDelay(rd);
     65   Serial.print("\nreadDelay set to (ms) : ");
     66   Serial.print(mySensor.getReadDelay());
     67   Serial.println("\n\nDuration test started");
     68 }
     69 
     70 void loop()
     71 {
     72   // Note: the library prevents reads faster than readDelay...
     73   //       it will return previous values for T & H
     74   int chk = mySensor.read();
     75   Serial.print(millis());
     76   Serial.print("\t");
     77   Serial.print(mySensor.getReadDelay());
     78   Serial.print("\t T: ");
     79   Serial.print(mySensor.getTemperature(), 1);
     80   Serial.print("\t H: ");
     81   Serial.print(mySensor.getHumidity(), 1);
     82   Serial.print("\t");
     83   printStatus(chk);
     84 }
     85 
     86 void printStatus(int chk)
     87 {
     88   switch (chk)
     89   {
     90     case DHTLIB_OK:
     91       Serial.print("OK,\t");
     92       break;
     93     case DHTLIB_ERROR_CHECKSUM:
     94       Serial.print("Checksum error,\t");
     95       break;
     96     case DHTLIB_ERROR_TIMEOUT_A:
     97       Serial.print("Time out A error,\t");
     98       break;
     99     case DHTLIB_ERROR_TIMEOUT_B:
    100       Serial.print("Time out B error,\t");
    101       break;
    102     case DHTLIB_ERROR_TIMEOUT_C:
    103       Serial.print("Time out C error,\t");
    104       break;
    105     case DHTLIB_ERROR_TIMEOUT_D:
    106       Serial.print("Time out D error,\t");
    107       break;
    108     case DHTLIB_ERROR_SENSOR_NOT_READY:
    109       Serial.print("Sensor not ready,\t");
    110       break;
    111     case DHTLIB_ERROR_BIT_SHIFT:
    112       Serial.print("Bit shift error,\t");
    113       break;
    114     case DHTLIB_WAITING_FOR_READ:
    115       Serial.print("Waiting for read,\t");
    116       break;
    117     default:
    118       Serial.print("Unknown: ");
    119       Serial.print(chk);
    120       Serial.print(",\t");
    121       break;
    122   }
    123   Serial.println();
    124 }
    125 
    126 // -- END OF FILE --