#include <dht.h> //เรียกLibrar
#include <Servo.h> //เรียกLibrar
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
dht DHT;
#define DHT22_PIN 5 //กำหนดpinที่ใช้งาน
#define sw1 0 //กำหนดpinที่ใช้งาน
void setup() {
myservo.attach(9); //กำหนดservo PWM
Serial.println("dht22_test.ino"); //แสดงผลพร้อมขึ้นบรรทัดใหม่
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT_LIB_VERSION);
Serial.println();
Serial.print("DHT22, \n");
pinMode(sw1,INPUT);
}
void loop() {
DHT.read22(DHT22_PIN);
if(digitalRead(sw1)==HIGH){
Serial.print("Switch 1 :ON");
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1032, 0, 180); // scale it to use it with the servo (value between 0 and 180)
}
else{
Serial.print("Switch 1 :OFF");
if(DHT.temperature>10){
val = map(DHT.temperature, 10, 80, 0, 180); // scale it to use it with the servo (value between 0 and 180)
}
else{
val=0;
}
}
myservo.write(val); // sets the servo position according to the scaled value
Serial.println("\tHumidity (%),\tTemperature (C),\t Degree");
Serial.print("\t\t\t");
Serial.print(DHT.humidity, 1);
Serial.print(",\t\t");
Serial.print(DHT.temperature, 1);
Serial.print(",\t\t");
Serial.print(val, 1);
Serial.print(",\n");
delay(1000);
}