#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_PCF8574.h>
#define DQ_Pin 2
LiquidCrystal_PCF8574 lcd(0x27); // 設定i2c位址,一般情況就是0x27和0x3F兩種
OneWire oneWire(DQ_Pin);
DallasTemperature sensors(&oneWire);
int Set_H_T=28; //設定溫度高值=24 度 C
int Set_L_T=27; //設定溫度低值=22 度 C
int Fan=2; //定義風扇的接腳 pin 2
void setup(void)
{
Serial.begin(9600);
sensors.begin();
lcd.begin(16, 2); // 初始化LCD
lcd.setBacklight(255);
lcd.clear();
lcd.setCursor(0, 0); //設定游標位置 (字,行)
lcd.print("*~ first line.");
lcd.setCursor(0, 1);
lcd.print("~* second line.");
}
void loop(void)
{
Serial.print("Temperatures --> ");
sensors.requestTemperatures();
Serial.println(sensors.getTempCByIndex(0));
delay(2000);
lcd.begin(16, 2); // 初始化LCD
//lcd.begin(20, 4); // 如果是20x4的LCD就是用這行
lcd.setBacklight(255);
lcd.clear();
lcd.setCursor(0, 0); //設定游標位置 (字,行)
lcd.print(sensors.getTempCByIndex(0));
lcd.setCursor(0, 1);
lcd.print("Temperatures");
if (sensors.getTempCByIndex(0) >=Set_H_T) {
analogWrite(Fan,200); //當溫度>高溫度設定值,風扇運轉
LCD.setCursor(12,1); // 遊標移至第 2 列第 12 個位置
LCD.print("80% ");
}
else if (sensors.getTempCByIndex(0)<=Set_H_T & sensors.getTempCByIndex(0)>=Set_L_T){
analogWrite(Fan,160); //當溫度>高溫度設定值,風扇運轉
LCD.setCursor(12,1); // 遊標移至第 2 列第 12 個位置
LCD.print("60% ");
}
else if (sensors.getTempCByIndex(0)<=Set_L_T){
analogWrite(Fan,120); //當溫度>高溫度設定值,風扇運轉
LCD.setCursor(12,1); // 遊標移至第 2 列第 12 個位置
LCD.print("50% ");
}
} // setup()