arduinoprojects

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

dhtnew_waitForRead_nonBlocking.ino (2495B)


      1 //
      2 //    FILE: dhtnew_waitForRead_nonBlocking.ino
      3 //  AUTHOR: Budulinek
      4 // VERSION: 0.1.0
      5 // PURPOSE: DHTNEW non-blocking wait for read example sketch for Arduino
      6 //     URL: https://github.com/RobTillaart/DHTNew
      7 //
      8 // HISTORY:
      9 // 0.1.0    2020-11-09 initial version
     10 //
     11 // DHT PIN layout from left to right
     12 // =================================
     13 // FRONT : DESCRIPTION
     14 // pin 1 : VCC
     15 // pin 2 : DATA
     16 // pin 3 : Not Connected
     17 // pin 4 : GND
     18 
     19 #include <dhtnew.h>
     20 
     21 DHTNEW mySensor(16);
     22 
     23 void setup()
     24 {
     25   Serial.begin(115200);
     26   Serial.println("\n");
     27   Serial.println("dhtnew_waitForRead_nonBlocking.ino");
     28   Serial.print("LIBRARY VERSION: ");
     29   Serial.println(DHTNEW_LIB_VERSION);
     30   Serial.println();
     31   Serial.println("This example shows how you can use the output of the read() function to implement non-blocking waiting for read.");
     32   Serial.println("In this example, Arduino continuously polls the read() function and returns fresh data (or an error) only when the read delay is over.");
     33   Serial.println("Infinite loop.");
     34   Serial.println();
     35   Serial.println("STAT\tHUMI\tTEMP\tTIME\tTYPE");
     36 }
     37 
     38 void loop()
     39 {
     40   // READ DATA
     41   uint32_t start = micros();
     42   int chk = mySensor.read();
     43   uint32_t stop = micros();
     44 
     45   if (chk != DHTLIB_WAITING_FOR_READ) {
     46     switch (chk)
     47     {
     48       case DHTLIB_OK:
     49         Serial.print("OK,\t");
     50         break;
     51       case DHTLIB_ERROR_CHECKSUM:
     52         Serial.print("Checksum error,\t");
     53         break;
     54       case DHTLIB_ERROR_TIMEOUT_A:
     55         Serial.print("Time out A error,\t");
     56         break;
     57       case DHTLIB_ERROR_TIMEOUT_B:
     58         Serial.print("Time out B error,\t");
     59         break;
     60       case DHTLIB_ERROR_TIMEOUT_C:
     61         Serial.print("Time out C error,\t");
     62         break;
     63       case DHTLIB_ERROR_TIMEOUT_D:
     64         Serial.print("Time out D error,\t");
     65         break;
     66       case DHTLIB_ERROR_SENSOR_NOT_READY:
     67         Serial.print("Sensor not ready,\t");
     68         break;
     69       case DHTLIB_ERROR_BIT_SHIFT:
     70         Serial.print("Bit shift error,\t");
     71         break;
     72       default:
     73         Serial.print("Unknown: ");
     74         Serial.print(chk);
     75         Serial.print(",\t");
     76         break;
     77     }
     78 
     79     // DISPLAY DATA
     80     Serial.print(mySensor.getHumidity(), 1);
     81     Serial.print(",\t");
     82     Serial.print(mySensor.getTemperature(), 1);
     83     Serial.print(",\t");
     84     uint32_t duration = stop - start;
     85     Serial.print(duration);
     86     Serial.print(",\t");
     87     Serial.println(mySensor.getType());
     88   }
     89 
     90   // do some other stuff
     91   delay(100);
     92 }
     93 
     94 
     95 // END OF FILE