//DHT22-3
//LED-9-10
//电位器-A0
//舵机-5
//按钮-11
#include <Wire.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#define DHTPIN 3
#define DHTTYPE DHT22
DHT dht(DHTPIN,DHTTYPE);
Servo myServo;
int potPin = A0;
int Servopin = 5;
int buttonPin = 2;
LiquidCrystal_I2C lcd(0x27,16,2);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
myServo.attach(Servopin);
dht.begin();
lcd.init();
lcd.backlight();
pinMode(buttonPin, INPUT_PULLUP);
pinMode(12, OUTPUT);//按钮的另一个引脚不能直接接正极,而是要接一个输出模式的引脚
}
void loop() {
// put your main code here, to run repeatedly:
int buttonstate = digitalRead(buttonPin);
int potvalue = analogRead(potPin);
int angle = map(potvalue,0,1023,0,180);
myServo.write(angle);
delay(1000);
float temC = dht.readTemperature();
float temF = temC+273.15;
Serial.println(potvalue);
Serial.println(angle);
lcd.setCursor(0,0);
lcd.print("Temperature:");
if(buttonstate==LOW)
{
Serial.println(temC);
lcd.setCursor(0,1);
lcd.print(temC);
lcd.print(" C");
}
else if(buttonstate==HIGH)
{
Serial.println(temF);
lcd.setCursor(0,1);
lcd.println(temF);
lcd.print(" F");
}
delay(2000);
lcd.clear();
}