|
用温湿度传感器控制继电器,为什么点了串口助手才能运行,拔掉usb线,直接外界9V电源却用不了,以下是程序,目的是在湿度达到40%的时候继电器吸合,45%后,继电器断开,求各位大神指教问题出在哪了,温湿度传感器型号DHT11。本人Arduino新手,大家多多包涵。
#define DHT11_PIN 0
int relay=7;
byte read_dht11_dat()
{
byte i = 0;
byte result=0;
for(i=0; i< 8; i++){
while(!(PINC & _BV(DHT11_PIN))); // wait for 50us
delayMicroseconds(30);
if(PINC & _BV(DHT11_PIN))
result |=(1<<(7-i));
while((PINC & _BV(DHT11_PIN)));
}
return result;
}
void setup()
{
DDRC |= _BV(DHT11_PIN);
PORTC |= _BV(DHT11_PIN);
pinMode(relay,OUTPUT);
}
void loop()
{
byte dht11_dat[5];
byte dht11_in;
byte i;
PORTC &= ~_BV(DHT11_PIN);
delay(18);
PORTC |= _BV(DHT11_PIN);
delayMicroseconds(40);
DDRC &= ~_BV(DHT11_PIN);
delayMicroseconds(40);
dht11_in = PINC & _BV(DHT11_PIN);
delayMicroseconds(80);
dht11_in = PINC & _BV(DHT11_PIN);
delayMicroseconds(80);
for (i=0; i<5; i++)
dht11_dat[i] = read_dht11_dat();
DDRC |= _BV(DHT11_PIN);
PORTC |= _BV(DHT11_PIN);
if(dht11_dat[0]<=40)
{
digitalWrite(relay,HIGH);
}
else if(dht11_dat[0]>=45)
{
digitalWrite(relay,LOW);
}
delay(2000);
} |
|