dhtnew_waitForRead.ino (3059B)
1 // 2 // FILE: dhtnew_waitForRead.ino 3 // AUTHOR: Mr-HaleYa 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-05-03 initial version 10 // 0.1.1 2020-06-08 updated error handling 11 // 0.1.2 2020-06-15 match 0.3.0 error 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("\n"); 29 Serial.println("dhtnew_waitForRead.ino"); 30 Serial.print("LIBRARY VERSION: "); 31 Serial.println(DHTNEW_LIB_VERSION); 32 Serial.println(); 33 Serial.println("This sketch shows the use of the setWaitForReading() flag (default value is false)."); 34 Serial.println("Setting the flag to true will make the sketch wait until the sensor is ready to take another reading."); 35 Serial.println("Otherwise, if the sensor was not ready to take a new reading the previous data will be returned."); 36 Serial.println("Note: Sensor reading times vary with sensor type"); 37 38 Serial.println("\n1. Test with WaitForReading not set"); 39 Serial.println("AGE\tHUMI\tTEMP\tTIME\tTYPE\tSTAT"); 40 test(); 41 test(); 42 test(); 43 test(); 44 test(); 45 46 Serial.print("\n2. Test with WaitForReading set to "); 47 mySensor.setWaitForReading(true); 48 Serial.println(mySensor.getWaitForReading() ? "true" : "false"); 49 Serial.println("AGE\tHUMI\tTEMP\tTIME\tTYPE\tSTAT"); 50 test(); 51 test(); 52 test(); 53 test(); 54 test(); 55 56 Serial.println("\nDone..."); 57 } 58 59 void loop() 60 { 61 62 } 63 64 void test() 65 { 66 // READ DATA 67 uint32_t start = micros(); 68 int chk = mySensor.read(); 69 uint32_t stop = micros(); 70 uint32_t duration = stop - start; 71 72 // DISPLAY IF OLD OR NEW DATA 73 if (duration > 50){Serial.print("NEW\t");}else{Serial.print("OLD\t");} 74 75 // DISPLAY DATA 76 Serial.print(mySensor.getHumidity(), 1); 77 Serial.print(",\t"); 78 Serial.print(mySensor.getTemperature(), 1); 79 Serial.print(",\t"); 80 Serial.print(duration); 81 Serial.print(",\t"); 82 Serial.print(mySensor.getType()); 83 Serial.print(",\t"); 84 85 switch (chk) 86 { 87 case DHTLIB_OK: 88 Serial.print("OK,\t"); 89 break; 90 case DHTLIB_ERROR_CHECKSUM: 91 Serial.print("Checksum error,\t"); 92 break; 93 case DHTLIB_ERROR_TIMEOUT_A: 94 Serial.print("Time out A error,\t"); 95 break; 96 case DHTLIB_ERROR_TIMEOUT_B: 97 Serial.print("Time out B error,\t"); 98 break; 99 case DHTLIB_ERROR_TIMEOUT_C: 100 Serial.print("Time out C error,\t"); 101 break; 102 case DHTLIB_ERROR_TIMEOUT_D: 103 Serial.print("Time out D error,\t"); 104 break; 105 case DHTLIB_ERROR_SENSOR_NOT_READY: 106 Serial.print("Sensor not ready,\t"); 107 break; 108 case DHTLIB_ERROR_BIT_SHIFT: 109 Serial.print("Bit shift error,\t"); 110 break; 111 default: 112 Serial.print("Unknown: "); 113 Serial.print(chk); 114 Serial.print(",\t"); 115 break; 116 } 117 Serial.println(); 118 delay(100); 119 } 120 121 // END OF FILE