Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //http://www.circuitbasics.com/how-to-set-up-the-dht11-humidity-sensor-on-an-arduino/
- #include <SoftwareSerial.h>
- #include <dht11.h>
- dht11 DHT11;
- #define DHT11PIN 2
- SoftwareSerial mySerial(0, 1); // RX, TX
- const int trigPin = 9;
- const int echoPin = 10;
- int mean(int* tab){
- int res=0;
- for(int i=0;i<16;++i){
- res+=tab[i];
- }
- return res/16;
- }
- int getDistance(){
- int distance;
- int tab1[18];
- int tab2[16];
- int duration;
- for(int i=0;i<18;++i){
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- // Sets the trigPin on HIGH state for 10 micro seconds
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- // Reads the echoPin, returns the sound wave travel time in microseconds
- duration = pulseIn(echoPin, HIGH);
- // Calculating the distance
- distance= duration*0.034/2;
- tab1[i] = distance;
- }
- int minpoz = 0;
- for(int i=0;i<18;++i)
- if(tab1[minpoz]>tab1[i])minpoz=i;
- int maxpoz = 0;
- for(int i=0;i<18;++i)
- if(tab1[maxpoz]<tab1[i])maxpoz=i;
- if(maxpoz==minpoz)maxpoz++;
- int j=0;
- for(int i=0;i<18;++i)
- if(i!=maxpoz && i!=minpoz)tab2[j++]=tab1[i];
- int mean_value = mean(tab2);
- return mean_value;
- }
- int getTemperature(){
- int chk = DHT11.read(DHT11PIN); //sprawdzenie stanu sensora, a następnie wyświetlenie komunikatu na monitorze szeregowym
- switch (chk)
- {
- case DHTLIB_OK:
- return (float)DHT11.temperature;
- break;
- case DHTLIB_ERROR_CHECKSUM:
- return -100;
- break;
- case DHTLIB_ERROR_TIMEOUT:
- return -100;
- break;
- default:
- return -100;
- break;
- }
- }
- int getHumidity(){
- int chk = DHT11.read(DHT11PIN); //sprawdzenie stanu sensora, a następnie wyświetlenie komunikatu na monitorze szeregowym
- switch (chk)
- {
- case DHTLIB_OK:
- return (float)DHT11.humidity;
- break;
- case DHTLIB_ERROR_CHECKSUM:
- return -100;
- break;
- case DHTLIB_ERROR_TIMEOUT:
- return -100;
- break;
- default:
- return -100;
- break;
- }
- }
- char inChar;
- void setup() {
- pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
- pinMode(echoPin, INPUT); // Sets the echoPin as an Input
- Serial.begin(9600); // Starts the serial communication
- mySerial.begin(9600);
- }
- void printToSerial(char* name, int value){
- Serial.print(name);
- Serial.print(int(value));
- Serial.print("\n");
- }
- void loop() {
- //int mean_value = getDistance();
- //mySerial.write("Distance ");
- //mySerial.write(mean_value);
- //mySerial.write("\n");
- //Serial.println(mean_value);
- //Serial.write("Distance ");
- //Serial.write(mean_value);
- //Serial.write("\n");
- //printToSerial("Distance",mean_value);
- int temperature = getTemperature();
- delay(1000);
- int humidity = getHumidity();
- printToSerial("Temperature ", temperature);
- printToSerial("humidity ", humidity);
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement