/* How to use the DHT-22 sensor with Arduino uno
Temperature and humidity sensor
*/
/*
//Libraries温度传感器
#include <DHT.h>;
//Constants
#define DHTPIN 6 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
//Variables
int chk;
float hum; //Stores humidity value
float temp; //Stores temperature value
void setup()
{
Serial.begin(9600);
dht.begin();
}
void loop()
{
//读取温度
delay(2000);
//Read data and store it to variables hum and temp
hum = dht.readHumidity();
temp= dht.readTemperature();
//Print temp and humidity values to serial monitor
Serial.print("Humidity: ");
Serial.print(hum);
Serial.print(" %, Temp: ");
Serial.print(temp);
Serial.println(" Celsius");
delay(10000); //Delay 2 sec.
}
*/
/*
LiquidCrystal Library - Hello World
Demonstrates the use a 16x2 LCD display. The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.
This sketch prints "Hello World!" to the LCD
and shows the time.
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 22 Nov 2010
by Tom Igoe
modified 7 Nov 2016
by Arturo Guadalupi
This example code is in the public domain.
https://docs.arduino.cc/learn/electronics/lcd-displays
*/
/*
// include the library code:LCD显示
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis() );
}
/*读取开关
//电阻为必需,不然5V就接地了
int pushButton=7;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);//初始化串行通信,设置波特率(通信速率)。
pinMode(pushButton,INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int buttonState=digitalRead(pushButton);
}
//内部上拉电阻(Pull-up resistor)是一种电阻,它将输入引脚连接到供电电压
//(通常是 5V),从而将引脚的电平拉高到高电平状态。这样做的目的是
//确保输入引脚在未连接到外部信号源时保持稳定的高电平状态。
*/
/*控制舵机
Controlling a servo position using a potentiometer (variable resistor)
by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
modified on 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Knob
*/
#include <Servo.h>
#include <LiquidCrystal.h>
#include <DHT.h>
Servo myservo; // 控制电机
int potpin = 0;
int val;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;//控制显示
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
unsigned long previousTimeis = 0;
long interval = 500;
#define DHTPIN 6 //控制温度计
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
int chk;
float temp;
int pushButton=7;//控制按钮
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
lcd.begin(16, 2);
dht.begin();
pinMode(pushButton,INPUT);
}
void loop() {
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
temp= dht.readTemperature();
int buttonState=digitalRead(pushButton);
unsigned long currentTimeis = millis();
if (buttonState){
if(currentTimeis - previousTimeis >= interval){
previousTimeis = currentTimeis;
lcd.clear();
lcd.print(temp);
}
}else{
if(currentTimeis - previousTimeis >= interval){
previousTimeis = currentTimeis;
temp = (temp * 9 / 5) + 32 ;
lcd.clear();
lcd.print(temp);
}
}
}