|
本帖最後由 jod53421 於 2015-12-17 10:29 編輯
以下是濕度感測器的程式碼:
int DHpin=8;
byte dat[5];
byte read_data()
{
byte data;
for(int i=0;i<8;i++)
{
if(digitalRead(DHpin)==LOW)
{
while(digitalRead(DHpin)==LOW);
delayMicroseconds(30);
if(digitalRead(DHpin)==HIGH)
data|=(1<<(7-i));
while(digitalRead(DHpin)==HIGH);
}
}
return data;
}
void start_test()
{
digitalWrite(DHpin,LOW);
delay(30);
digitalWrite(DHpin,HIGH);
delayMicroseconds(40);
pinMode(DHpin,INPUT);
while(digitalRead(DHpin)==HIGH);
delayMicroseconds(80);
while(digitalRead(DHpin)==LOW);
delayMicroseconds(80);
for(int i=0;i<4;i++)
dat=read_data();
pinMode(DHpin,OUTPUT);
digitalWrite(DHpin,HIGH);
}
void setup()
{
Serial.begin(9600);
pinMode(DHpin,OUTPUT);
}
void loop()
{
start_test();
Serial.print("t1:");
Serial.print(dat[0], DEC); //顯示濕度的整數位;
Serial.print('.');
Serial.print(dat[1],DEC); //顯示濕度的小數位;
Serial.print(';');
Serial.print("t2:");
Serial.print(dat[2], DEC); //顯示溫度的整數位;
Serial.print('.');
Serial.println(dat[3],DEC); //顯示溫度的小數位;
delay(2000);
}
想請教各位大師,要如何在這程式碼裡面多加濕度高於40%可以驅動馬達,到達定位可以停止馬達動作.
拜託各位大師! |
|