[AppleScript] syntaxhighlighter_viewsource syntaxhighlighter_copycode
/* 添加 DHT11 代码库 */
#include <dht11.h>
/* 配置一个 DHT11 对象 */
dht11 DHT11;
/* 定义 DHT11 数据引脚 */
#define DHT11PIN 2
int key[3]={11,12,13};
int LED[8]={3,4,5,6,7,8,9,10};
float temperature,Humidity;
float Fahrenheit;
float Kelvin;
int i,j,k,keynum;
void setup() {
/* 配置串口 */
Serial.begin(9600);
/* 输出 DHT11 代码库信息 */
Serial.println("DHT11 TEST PROGRAM ");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT11LIB_VERSION);
Serial.println();
for(i=2;i<10;i++)
{
pinMode(LED,OUTPUT);
}
for(j=11;j<14;j++)
{ pinMode(key[j],INPUT_PULLUP); }
}
void loop() {
Serial.println("\n");
int chk = DHT11.read(DHT11PIN); //查看 DHT11 是否在线或数据读取是否成功
Serial.print("Read sensor: "); //输出 DHT11 模块数据
switch (chk)
{
case DHTLIB_OK: //数据读取成功
Serial.println("OK");
break;
case DHTLIB_ERROR_CHECKSUM: //数据读取成功,但检验错误
Serial.println("Checksum error");
break;
case DHTLIB_ERROR_TIMEOUT: //数据读取超时,即模块不在线
Serial.println("Time out error");
break;
default: //其他错误
Serial.println("Unknown error");
break;
}
/* 输出湿度信息 */
Serial.print("Humidity (%): ");
Serial.println((float)DHT11.humidity, 2);
Scan_key();
if(keynum==1)
{ Serial.print("Temperature (oC): ");
Serial.println((float)DHT11.temperature, 2); /*摄氏温度*/
}
if(keynum==2)
{
temperature= 1.8 * (float)DHT11.temperature + 32;
Serial.print("Temperature (oF): ");
Serial.println((float)temperature, 2);/*华氏温度*/
}
if(keynum==3)
{
temperature=(float)DHT11.temperature + 273.15;
Serial.print("Temperature (K): ");
Serial.println((float)temperature, 2);/*开氏温度*/
}
k=(float)DHT11.temperature/10;
for(int i=0;i<9;i=i+k)
{digitalWrite(LED,HIGH);
delay(500);
digitalWrite(LED,LOW);
delay(500);}
}
void Scan_key()
{
keynum=0; //清空变量
if(digitalRead(11)==LOW)
{
delay(10);
if(digitalRead(11)==LOW)
{
keynum=1;
}
}
else if(digitalRead(12)==LOW)
{
delay(10);
if(digitalRead(12)==LOW)
{
keynum=2;
}
}
else if(digitalRead(13)==LOW)
{
delay(10);
if(digitalRead(13)==LOW)
{
keynum=3;
}
}
else
keynum=0;
}