1602的溫濕度程序
- #include <LiquidCrystal.h>
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
- int temp;//溫度
- int humi;//濕度
- int tol;//校對碼
- int j;
- unsigned int loopCnt;
- int chr[40] = {0};//創(chuàng)建數(shù)字數(shù)組,用來存放40個bit
- unsigned long time;
- #define pin 7
- int Aout=A0 ;//MQ-135 AO 接 Arduino Uno A0
- int temp1 = 0; //臨時變量,存儲A0讀取的數(shù)據(jù)
- void setup() {
- Serial.begin(9600);//定義波特率
- pinMode(A0, INPUT);//定義A0為INPUT模式
- lcd.begin(16, 2);
- }
-
- void loop() {
- bgn:
- delay(2000);
- //設(shè)置2號接口模式為:輸出
- //輸出低電平20ms(>18ms)
- //輸出高電平40μs
- pinMode(pin,OUTPUT);
- digitalWrite(pin,LOW);
- delay(20);
- digitalWrite(pin,HIGH);
- delayMicroseconds(40);
- digitalWrite(pin,LOW);
- //設(shè)置2號接口模式:輸入
- pinMode(pin,INPUT);
- //高電平響應(yīng)信號
- loopCnt=10000;
- while(digitalRead(pin) != HIGH)
- {
- if(loopCnt-- == 0)
- {
- //如果長時間不返回高電平,輸出個提示,重頭開始。
- Serial.println("HIGH");
- goto bgn;
- }
- }
- //低電平響應(yīng)信號
- loopCnt=30000;
- while(digitalRead(pin) != LOW)
- {
- if(loopCnt-- == 0)
- {
- //如果長時間不返回低電平,輸出個提示,重頭開始。
- Serial.println("LOW");
- goto bgn;
- }
- }
- //開始讀取bit1-40的數(shù)值
- for(int i=0;i<40;i++)
- {
- while(digitalRead(pin) == LOW)
- {}
- //當出現(xiàn)高電平時,記下時間“time”
- time = micros();
- while(digitalRead(pin) == HIGH)
- {}
- //當出現(xiàn)低電平,記下時間,再減去剛才儲存的time
- //得出的值若大于50μs,則為‘1’,否則為‘0’
- //并儲存到數(shù)組里去
- if (micros() - time >50)
- {
- chr[i]=1;
- }
- else{
- chr[i]=0;
- }
- }
-
- //濕度,8位的bit,轉(zhuǎn)換為數(shù)值
- humi=chr[0]*128+chr[1]*64+chr[2]*32+chr[3]*16+chr[4]*8+chr[5]*4+chr[6]*2+chr[7];
-
- //溫度,8位的bit,轉(zhuǎn)換為數(shù)值
- temp=chr[16]*128+chr[17]*64+chr[18]*32+chr[19]*16+chr[20]*8+chr[21]*4+chr[22]*2+chr[23];
- //校對碼,8位的bit,轉(zhuǎn)換為數(shù)值
- tol=chr[32]*128+chr[33]*64+chr[34]*32+chr[35]*16+chr[36]*8+chr[37]*4+chr[38]*2+chr[39];
- //輸出:溫度、濕度、校對碼
- lcd.clear ();
- lcd.setCursor(0,0);
- lcd.print("temp:");
- lcd.print(temp);
- lcd.print((char) 0xDF);
- lcd.print("C");
- lcd.setCursor(0,1);
- lcd.print("humidity:");
- lcd.print(humi);
- lcd.print("%");
- delay (5000);
-
-
-
- temp1 = analogRead(Aout); //讀取A0的模擬數(shù)據(jù)
-
- if (temp<500 )
- {
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("status:normal");
- lcd.setCursor(0,1);
- lcd.print("potency:");
- lcd.print(temp1);
-
- }
- if (temp1>500)
- {
-
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
完整代碼下載:
1602 DTH11 MQ-5.doc
(19 KB, 下載次數(shù): 102)
2017-4-6 23:20 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|