#include<Wire.h>
#include<LiquidCrystal.h>
#define BUTTON_PIN 2
#define TEMP_SENSOR_PIN A1
LiquidCrystal lcd(12,11,10,9,8,7);
float celsius=0;
float fahrenheit=0;
bool isFahrenheit=false;
void setup() {
// put your setup code here, to run once:
//lcd.init();//初始化LCD
//lcd.backlight();//打开背光
lcd.begin(16,2);
pinMode(BUTTON_PIN,INPUT_PULLUP);//
}
void loop() {
// put your main code here, to run repeatedly:
int buttonState=digitalRead(BUTTON_PIN);//按钮控制显示华氏度,摄氏度
float voltage=analogRead(TEMP_SENSOR_PIN)*5.0/1023.0;
//analogRead函数将电压0-5对应到0-1023
float temperature=(voltage-0.5)*100.0;
celsius=temperature;
fahrenheit=celsius*1.8+32;
lcd.setCursor(0,0);//LCD显示屏上的初始值,将光标设置到第一行第一列
lcd.print("Temp:");//在LCD上打印“Temp”
if(isFahrenheit){//华摄度
lcd.print(fahrenheit);
lcd.print("F");
}else{//摄氏度
lcd.print(celsius);
lcd.print("C");
}
if(buttonState==LOW){
isFahrenheit=!isFahrenheit;
delay(500);
}
delay(100);
}