Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. //http://www.circuitbasics.com/how-to-set-up-the-dht11-humidity-sensor-on-an-arduino/
  2. #include <SoftwareSerial.h>
  3.  
  4. #include <dht11.h>
  5.  
  6. dht11 DHT11;
  7.  
  8. #define DHT11PIN 2
  9.  
  10. SoftwareSerial mySerial(0, 1); // RX, TX
  11.  
  12. const int trigPin = 9;
  13. const int echoPin = 10;
  14.  
  15. int mean(int* tab){
  16. int res=0;
  17. for(int i=0;i<16;++i){
  18. res+=tab[i];
  19. }
  20. return res/16;
  21. }
  22.  
  23. int getDistance(){
  24.  
  25. int distance;
  26.  
  27. int tab1[18];
  28. int tab2[16];
  29. int duration;
  30. for(int i=0;i<18;++i){
  31.  
  32. digitalWrite(trigPin, LOW);
  33. delayMicroseconds(2);
  34. // Sets the trigPin on HIGH state for 10 micro seconds
  35. digitalWrite(trigPin, HIGH);
  36. delayMicroseconds(10);
  37. digitalWrite(trigPin, LOW);
  38. // Reads the echoPin, returns the sound wave travel time in microseconds
  39. duration = pulseIn(echoPin, HIGH);
  40. // Calculating the distance
  41. distance= duration*0.034/2;
  42. tab1[i] = distance;
  43.  
  44. }
  45.  
  46.  
  47. int minpoz = 0;
  48. for(int i=0;i<18;++i)
  49. if(tab1[minpoz]>tab1[i])minpoz=i;
  50. int maxpoz = 0;
  51. for(int i=0;i<18;++i)
  52. if(tab1[maxpoz]<tab1[i])maxpoz=i;
  53. if(maxpoz==minpoz)maxpoz++;
  54. int j=0;
  55. for(int i=0;i<18;++i)
  56. if(i!=maxpoz && i!=minpoz)tab2[j++]=tab1[i];
  57.  
  58. int mean_value = mean(tab2);
  59.  
  60. return mean_value;
  61.  
  62.  
  63. }
  64.  
  65. int getTemperature(){
  66. int chk = DHT11.read(DHT11PIN); //sprawdzenie stanu sensora, a następnie wyświetlenie komunikatu na monitorze szeregowym
  67.  
  68.  
  69. switch (chk)
  70. {
  71. case DHTLIB_OK:
  72.  
  73. return (float)DHT11.temperature;
  74.  
  75. break;
  76. case DHTLIB_ERROR_CHECKSUM:
  77.  
  78. return -100;
  79. break;
  80. case DHTLIB_ERROR_TIMEOUT:
  81.  
  82. return -100;
  83. break;
  84. default:
  85.  
  86. return -100;
  87. break;
  88. }
  89. }
  90.  
  91. int getHumidity(){
  92. int chk = DHT11.read(DHT11PIN); //sprawdzenie stanu sensora, a następnie wyświetlenie komunikatu na monitorze szeregowym
  93.  
  94.  
  95. switch (chk)
  96. {
  97. case DHTLIB_OK:
  98.  
  99. return (float)DHT11.humidity;
  100.  
  101. break;
  102. case DHTLIB_ERROR_CHECKSUM:
  103.  
  104. return -100;
  105. break;
  106. case DHTLIB_ERROR_TIMEOUT:
  107.  
  108. return -100;
  109. break;
  110. default:
  111.  
  112. return -100;
  113. break;
  114. }
  115. }
  116.  
  117.  
  118. char inChar;
  119.  
  120.  
  121.  
  122. void setup() {
  123. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  124. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  125. Serial.begin(9600); // Starts the serial communication
  126. mySerial.begin(9600);
  127. }
  128.  
  129. void printToSerial(char* name, int value){
  130. Serial.print(name);
  131. Serial.print(int(value));
  132. Serial.print("\n");
  133. }
  134.  
  135. void loop() {
  136.  
  137. //int mean_value = getDistance();
  138. //mySerial.write("Distance ");
  139. //mySerial.write(mean_value);
  140. //mySerial.write("\n");
  141. //Serial.println(mean_value);
  142. //Serial.write("Distance ");
  143. //Serial.write(mean_value);
  144. //Serial.write("\n");
  145.  
  146. //printToSerial("Distance",mean_value);
  147. int temperature = getTemperature();
  148. delay(1000);
  149. int humidity = getHumidity();
  150. printToSerial("Temperature ", temperature);
  151. printToSerial("humidity ", humidity);
  152. delay(1000);
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement