dhtnew_adaptive_delay.ino (3906B)
1 // 2 // FILE: dhtnew_adaptive_delay.ino 3 // AUTHOR: Rob Tillaart 4 // VERSION: 0.1.3 5 // PURPOSE: DHTNEW library test sketch for Arduino 6 // URL: https://github.com/RobTillaart/DHTNew 7 // 8 // HISTORY: 9 // 0.1.0 2018-08-06 initial version 10 // 0.1.1 2020-04-30 replaced humidity and temperature with functions 11 // 0.1.2 2020-06-08 redid 0.1.1 testing and fix + improved error handling 12 // 0.1.3 2020-06-15 match 0.3.0 error handling 13 // 14 // DHT PIN layout from left to right 15 // ================================= 16 // FRONT : DESCRIPTION 17 // pin 1 : VCC 18 // pin 2 : DATA 19 // pin 3 : Not Connected 20 // pin 4 : GND 21 22 // Note: 23 // Adaptive delay makes no sense anymore as the DHTNEW lib catches reads that 24 // are done faster than READ_DELAY apart (see dhtnew.cpp file). 25 // that said it is the goal to get this adaptibility into the library one day. 26 // A way to do this is to add a function auto_calibrate() that finds the timing 27 // where reading fails and use that value + safety margin (20%?) 28 29 #include <dhtnew.h> 30 31 DHTNEW mySensor(16); 32 33 void setup() 34 { 35 Serial.begin(115200); 36 Serial.println("dhtnew_adaptive_delay.ino"); 37 Serial.print("LIBRARY VERSION: "); 38 Serial.println(DHTNEW_LIB_VERSION); 39 Serial.println(); 40 41 Serial.println("\n1. Type detection test, first run might take longer to determine type"); 42 Serial.println("STAT\tHUMI\tTEMP\tTIME\tTYPE"); 43 test(); 44 test(); 45 test(); 46 test(); 47 48 Serial.println("\n2. Humidity offset test"); 49 Serial.println("STAT\tHUMI\tTEMP\tTIME\tTYPE"); 50 mySensor.setHumOffset(2.5); 51 test(); 52 mySensor.setHumOffset(5.5); 53 test(); 54 mySensor.setHumOffset(-5.5); 55 test(); 56 mySensor.setHumOffset(0); 57 test(); 58 59 Serial.println("\n3. Temperature offset test"); 60 Serial.println("STAT\tHUMI\tTEMP\tTIME\tTYPE"); 61 mySensor.setTempOffset(2.5); 62 test(); 63 mySensor.setTempOffset(5.5); 64 test(); 65 mySensor.setTempOffset(-5.5); 66 test(); 67 mySensor.setTempOffset(0); 68 test(); 69 70 Serial.println("\n4. LastRead test"); 71 mySensor.read(); 72 for (int i = 0; i < 20; i++) 73 { 74 if (millis() - mySensor.lastRead() > 1000) 75 { 76 mySensor.read(); 77 Serial.println("actual read"); 78 } 79 Serial.print(mySensor.getHumidity(), 1); 80 Serial.print(",\t"); 81 Serial.println(mySensor.getTemperature(), 1); 82 delay(250); 83 } 84 85 Serial.println("\nDone..."); 86 } 87 88 void loop() 89 { 90 test(); 91 } 92 93 void test() 94 { 95 static uint16_t dht_delay = 600; 96 // READ DATA 97 uint32_t start = micros(); 98 int chk = mySensor.read(); 99 uint32_t stop = micros(); 100 101 switch (chk) 102 { 103 case DHTLIB_OK: 104 Serial.print("OK,\t"); 105 dht_delay -= 10; 106 break; 107 case DHTLIB_ERROR_CHECKSUM: 108 Serial.print("Checksum error,\t"); 109 dht_delay += 10; 110 break; 111 case DHTLIB_ERROR_TIMEOUT_A: 112 Serial.print("Time out A error,\t"); 113 dht_delay += 10; 114 break; 115 case DHTLIB_ERROR_TIMEOUT_B: 116 Serial.print("Time out B error,\t"); 117 break; 118 case DHTLIB_ERROR_TIMEOUT_C: 119 Serial.print("Time out C error,\t"); 120 dht_delay += 10; 121 break; 122 case DHTLIB_ERROR_TIMEOUT_D: 123 Serial.print("Time out D error,\t"); 124 dht_delay += 10; 125 break; 126 case DHTLIB_ERROR_SENSOR_NOT_READY: 127 Serial.print("Sensor not ready,\t"); 128 dht_delay += 10; 129 break; 130 case DHTLIB_ERROR_BIT_SHIFT: 131 Serial.print("Bit shift error,\t"); 132 dht_delay += 10; 133 break; 134 default: 135 Serial.print("Unknown: "); 136 Serial.print(chk); 137 Serial.print(",\t"); 138 dht_delay += 100; 139 break; 140 } 141 142 dht_delay = constrain(dht_delay, 100, 5000); 143 // DISPLAY DATA 144 Serial.print(mySensor.getHumidity(), 1); 145 Serial.print(",\t"); 146 Serial.print(mySensor.getTemperature(), 1); 147 Serial.print(",\t"); 148 uint32_t duration = stop - start; 149 Serial.print(duration); 150 Serial.print(",\t"); 151 Serial.print(mySensor.getType()); 152 Serial.print(",\t"); 153 Serial.println(dht_delay); 154 delay(dht_delay); 155 } 156 157 158 // END OF FILE