|
このページはEtoJ逐語翻訳フィルタによって翻訳生成されました。 |
Oh, yes! If only everything could be so simple! (Ha! I wrote that five (working) hours ago. Using the module may be 平易な. Understanding everything in the code... not so 平易な.)
There is a place where the code used to say....
delayMicroseconds(40);//DHT22 datasheet says..
(警察などへの)密告,告訴(状) (機の)カム in from a helpful reader that the program 作品 more reliably if you change the 40 to 30. (It now says 30 on this page, but it didn't, before 9 損なう 13). There are more 詳細(に述べる)s at the 関連した line, in the code.
__________________
Anyway....
I plugged my DHT11 sensor into my Arduino, 負担d some デモ ソフトウェア, changed one line (made...
Serial.begin(19200);
... into...
Serial.begin(9600);
And it Just Worked!! A nice stream of 湿度 and 気温 reading appeared on my Arduino serial 監視する as soon as I switched to it after uploading the program. Nothing beyond a "basic" Arduino 体制/機構 is needed.
Credit where 予定: The デモ ソフトウェア, and my 認識/意識性 of the sensor (機の)カム from using the NuElectronics datalogging 保護物,者. There was nothing in what I did with the DHT11 which "needed" the 保護物,者, though.
Remember: To run EXACTLY like this, the sensor needs to be plugged into the input that the NuElectronics 保護物,者 "fed" from its "P4". Change the #define line if you want to use it どこかよそで.
The only 関係s you need to the very reasonably 定価つきの module are: +5v, gnd (無 volts), and a 解放する/自由な analog data line. That line is used in a 数字表示式の 方式, but because of the way the ソフトウェア is written, it is hard to move the sensor to a line other than D14-19, aka analog A0-5. I've done a separate page about signal usage on the nuelectronics datalogging 保護物,者, which you may want to 協議する.
The code すぐに below is not 適切に 実験(する)d, commented, etc, etc... but it may work. This 封鎖する of code was just "dropped into" a better webpage. Later, we will return to that more sober, more carefully done 構成要素. What you see below was edited by 手渡す after 実験(する)ing. Errors may have crept in. It is an 試みる/企てる at a "clean" 見解/翻訳/版 of the code I derived from the nuelectronics 示唆するd code. If it doesn't "just work", just go on to the other two code listings. Both of them SHOULD work. If you find that either of them do work for you, but the code すぐに below doesn't, please let me know? If you want more than just "the answer" to "how do I read the sensor, read the 残り/休憩(する) of this page. But the に引き続いて code SHOULD work....
//ReadHumTturDHT11alternate2-tweaked by 手渡す for webpage
//vers 20Jly10-tweaked by 手渡す for webpage
//See for more (警察などへの)密告,告訴(状)....
//http://sheepdogguides.som/arduino/ar3ne1humDHT11.htm
//N.B. "bit" is used in the 狭くする, computer "1 or 0"
// sense throughout.
//"DHT" from sensor's 指名するs: DHT11, DHT22.
//DHT aka Aosong AM2302, and there's an AM2303 which
//seems to be in the same family.
//Comments on this based on Aosong AM2302, aka DHT22, datasheet.
//Believed to 一般に 適用する to DHT11 同様に, except in the
//事例/患者 of the DHT11, I believe the second and fourth bytes are
//always 無.
//***N.B.****
//The comments may not yet be EXACTLY 権利.
//See the web-page 特記する/引用するd above for 最新の news.
//作品 with Aosong DHT11. Only 3 wires are 伴う/関わるd:
//Vcc, ground, and a 選び出す/独身 data line. No extra 構成要素s
//needed. One of the DHT11's 4 pins goes nowhere.
//You should not need to change anything except the next line to use
//the ソフトウェア with the sensor on a different line, or for a DHT22.
//Just "huffing" on the sensor from 深く,強烈に filled 肺s should show
//a 近づく-instant rise in 湿度
#define dht_dpin 14 //no ; here. 始める,決める equal to channel sensor is on,
//where if dht_dpin is 14, sensor is on 数字表示式の line 14, aka analog 0
//Other 数字表示式の lines can be used, e.g. D2
byte bGlobalErr;//for passing error code 支援する from コンビナート/複合体 機能(する)/行事s.
byte dht_dat[5];//Array to 持つ/拘留する the bytes sent from sensor.
// That was, wrongly, ..dat[4] until 28 Jan 13. See
// http://sheepdogguides.com/arduino/aht9ArrIndex
// for 指導/手引 on array 索引 gotcha
無効の 体制/機構(){
InitDHT();//Does what's necessary to 準備する for reading DHT
Serial.begin(9600);
延期する(300);//Let system settle
Serial.println("湿度 and 気温\n\n");
延期する(700);//Wait 残り/休憩(する) of 1000ms recommended 延期する before
//接近ing sensor
}//end "体制/機構()"
無効の 宙返り飛行(){
ReadDHT();//This is the "heart" of the program.
//Fills 全世界の array dht_dpin[], and bGlobalErr, which
//will 持つ/拘留する 無 if ReadDHT went okay.
//Must call InitDHT once (in "体制/機構()" is usual) before
//calling ReadDHT.
//に引き続いて: 陳列する,発揮するs what was seen...
switch (bGlobalErr){
事例/患者 0:
Serial.print("現在の 湿度 = ");
Serial.print(dht_dat[0], DEC);
Serial.print(".");
Serial.print(dht_dat[1], DEC);
Serial.print("% ");
Serial.print("気温 = ");
Serial.print(dht_dat[2], DEC);
Serial.print(".");
Serial.print(dht_dat[3], DEC);
Serial.println("C ");
break;
事例/患者 1:
Serial.println("Error 1: DHT start 条件 1 not met.");
break;
事例/患者 2:
Serial.println("Error 2: DHT start 条件 2 not met.");
break;
事例/患者 3:
Serial.println("Error 3: DHT checksum error.");
break;
default:
Serial.println("Error: Unrecognized code 遭遇(する)d.");
break;
}//end "switch"
延期する(800);//Don't try to 接近 too frequently... in theory
//should be once per two seconds, fastest,
//but seems to work after 0.8 second.
}// end 宙返り飛行()
/*Below here: Only "黒人/ボイコット box" elements which can just be plugged
不変の into programs. 供給する InitDHT() and ReadDHT(), and a 機能(する)/行事
one of them uses.*/
無効の InitDHT(){
pinMode(dht_dpin,OUTPUT);
digitalWrite(dht_dpin,HIGH);
}//end InitDHT
無効の ReadDHT(){
/*Uses 全世界の variables dht_dat[0-4], and bGlobalErr to pass
"answer" 支援する. bGlobalErr=0 if read went okay.
Depends on 全世界の dht_dpin for where to look for sensor.*/
bGlobalErr=0;
byte dht_in;
byte i;
// Send "start read and 報告(する)/憶測" 命令(する) to sensor....
// First: pull-負かす/撃墜する I/O pin for 23000us
digitalWrite(dht_dpin,LOW);
延期する(23);
/*aosong.com datasheet for DHT22 says pin should be low at least
500us. I infer it can be low longer without any]
刑罰,罰則 apart from making "read sensor" 過程 take
longer. */
//Next line: Brings line high again,
// second step in giving "start read..." 命令(する)
digitalWrite(dht_dpin,HIGH);
delayMicroseconds(30);//DHT22 datasheet says host should
//keep line high 20-40us, then watch for sensor taking line
//low. That low should last 80us. 認めるs "start read
//and 報告(する)/憶測" 命令(する).
//Getting the 延期する exacly 権利 is 批判的な...
// put 40 in for the 延期する, in one of the later
// 版s of this code, and the program
// will work SOMETIMES, but not ALWAYS.
// You have to 許す for 過程ing 総計費s,
// etc, too. Using, say, 30 won't "magically"
// make the TOTAL 延期する (with 総計費s, etc)
// 正確に/まさに 30.0000 milliseconds. (This 見解/翻訳/版 may
// also have been unreliable with 延期する=40.. now
// deprecated.)
//Next: Change Arduino pin to an input, to
//watch for the 80us low explained a moment ago.
pinMode(dht_dpin,INPUT);
delayMicroseconds(40);
dht_in=digitalRead(dht_dpin);
if(dht_in){
bGlobalErr=1;//dht start 条件 1 not met
return;
}//end "if..."
delayMicroseconds(80);
dht_in=digitalRead(dht_dpin);
if(!dht_in){
bGlobalErr=2;//dht start 条件 2 not met
return;
}//end "if..."
/*After 80us low, the line should be taken high for 80us by the
sensor. The low に引き続いて that high is the start of the first
bit of the forty to come. The 決まりきった仕事 "read_dht_dat()"
推定する/予想するs to be called with the system already into this low.*/
delayMicroseconds(80);
//now ready for data 歓迎会... 選ぶ up the 5 bytes coming from
// the sensor
for (i=0; i<5; i++)
dht_dat[i] = read_dht_dat();
//Next: 回復する pin to 生産(高) 義務s
pinMode(dht_dpin,OUTPUT);
//Next: Make data line high again, as 生産(高) from Arduino
digitalWrite(dht_dpin,HIGH);
//Next see if data received 一貫した with checksum received
byte dht_check_sum =
dht_dat[0]+dht_dat[1]+dht_dat[2]+dht_dat[3];
/*条件 in に引き続いて "if" says "if fifth byte from sensor
not the same as the sum of the first four..."*/
if(dht_dat[4]!= dht_check_sum)
{bGlobalErr=3;}//DHT checksum error
};//end ReadDHT()
byte read_dht_dat(){
//Collect 8 bits from datastream, return them 解釈する/通訳するd
//as a byte. I.e. if 0000.0101 is sent, return decimal 5.
//Code 推定する/予想するs the system to have recently entered the
//dataline low 条件 at the start of every data bit's
//伝達/伝染 BEFORE this 機能(する)/行事 is called.
byte i = 0;
byte result=0;
for(i=0; i< 8; i++){
//We enter this during the first start bit (low for 50uS) of the byte
//Next: wait until pin goes high
while(digitalRead(dht_dpin)==LOW);
//signalling end of start of bit's 伝達/伝染.
//Dataline will now stay high for 27 or 70 uS, depending on
//whether a 0 or a 1 is 存在 sent, それぞれ.
delayMicroseconds(30);//AFTER pin is high, wait その上の period, to be
//into the part of the タイミング diagram where a 0 or a 1 denotes
//the datum 存在 send. The "その上の period" was 30uS in the ソフトウェア
//that this has been created from. I believe that a higher number
//(45?) might be more appropriate.
//Next: Wait while pin still high
if (digitalRead(dht_dpin)==HIGH)
result |=(1<<(7-i));// "追加する" (not just 新規加入) the 1
//to the growing byte
//Next wait until pin goes low again, which signals the (米ソ間の)戦略兵器削減交渉
//of the NEXT bit's 伝達/伝染.
while (digitalRead(dht_dpin)==HIGH);
}//end of "for.."
return result;
}//end of "read_dht_dat()"
I've done a more 徹底的な 探検 of the "fancy" code in the デモ, and of what I did while 変えるing it to the above, if you want to understand what was in the old code.
As I said: The code above is not 適切に 実験(する)d, commented, etc, etc... but it may work. That section was just "dropped into" a better webpage. Here we return to that more sober, more carefully done 構成要素.
The code below, (すぐに below the "stop 圧力(をかける)" notice"!) uses some "fancy" 命令(する)s. I will discuss them later.
It also looks 広大な and 複雑にするd. Don't be alarmed by all the comments. Ignore them, if it makes you happier! Connect three wires. Maybe change one line of the code (the "#define" line. There's no need to change it, if you connected your sensor to analog0, aka digital14, aka nuelectronics 保護物,者 P4.) And it should just work!
Pretty Good code... and least "messed about with"... very closely 関係のある to the code recommended by nuelectronics...
//ReadHumTturDHT11alternate
//vers 19Jly10
//See for more (警察などへの)密告,告訴(状)....
//http://sheepdogguides.som/arduino/ar3ne1humDHT11.htm
//N.B. "bit" is used in the 狭くする, computer "1 or 0"
// sense throughout.
//Comments on this based on Aosong AM2302, aka DHT22, datasheet.
//Believed to 一般に 適用する to DHT11 同様に, except in the
//事例/患者 of the DHT11, I believe the second and fourth bytes are
//always 無.
//***N.B.****
//The code WORKS... the comments may not yet be EXACTLY 権利.
//See the web-page 特記する/引用するd above for 最新の news.
//This code 作品 with a DHT11 湿度/ 気温 sensing module
//from nuelectronics.com, 従うd with vers 0018 of the Arduino 環境
//Sensor 大(公)使館員d to P4 (nuelectronics 保護物,者)/ analog 0, aka 数字表示式の 14.
//That "module", によれば the
//nuelectronics 場所/位置, and visual 査察 簡単に 供給するs for 平易な
//関係 of an Aosong DHT11 部隊 to the nuelectronics datalogging
//保護物,者. Only 3 wires are 伴う/関わるd: Vcc, ground, and a 選び出す/独身 data
//line. One of the DHT11's 4 pins goes nowhere.
//You should not need to change anything except the next line to use
//the ソフトウェア with the sensor on a different line, or for a DHT22.
//Just "huffing" on the sensor from 深く,強烈に filled 肺s should show
//a 近づく instant rise in 湿度
#define DHT11_PIN 0 // ADC0...
//even though we are using it as a 数字表示式の pin.
//Other parts of code 制限する us to using
//ADC0-5, aka D14-19
byte read_dht11_dat()
//Collect 8 bits from datastream, return them 解釈する/通訳するd
//as a byte. I.e. if 0000.0101 is sent, return decimal 5.
//Code 推定する/予想するs the system to have recently entered the
//dataline low 条件 at the start of every data bit's
//伝達/伝染 BEFORE this 機能(する)/行事 is called.
{
byte i = 0;
byte result=0;
for(i=0; i< 8; i++){
//We enter this during the first start bit (low for 50uS) of the byte
while(!(PINC & _BV(DHT11_PIN))); // wait until pin goes high
//signalling end of start of bit's 伝達/伝染.
//Dataline will stay high for 27 or 70 uS, depending on if
// a 0 or a 1 is 存在 sent, それぞれ
delayMicroseconds(30);//AFTER pin is high, wait その上の period, to be
//into the part of the タイミング diagram where a 0 or a 1 denotes
//the datum 存在 send. The "その上の period" was 30uS in the ソフトウェア
//that this has been created from. I believe that a higher number
//(45?) would be more appropriate.
if(PINC & _BV(DHT11_PIN))//If pin still high
result |=(1<<(7-i));// "追加する" (not just 新規加入) the 1 to the
//growing byte
while((PINC & _BV(DHT11_PIN))); // wait for pin to go low again, which
//signals he (米ソ間の)戦略兵器削減交渉 of the NEXT bit's 伝達/伝染.
}//end of "for.."
return result;
}//end of "read_dht11_dat()"
無効の 体制/機構()
{
DDRC |= _BV(DHT11_PIN);//始める,決める data pin... for now... as out(?)put
//DDRC is data direction 登録(する) for pins A0-5 are on
PORTC |= _BV(DHT11_PIN);//(dis?)-connect 内部の pull up on line
//PORTC relates to the pins A0-5 are on.
Serial.begin(9600);
延期する(300);//Let system settle
Serial.println("湿度 and 気温\n\n");
延期する(700);//Wait 残り/休憩(する) of 1000ms recommended 延期する before
//接近ing sensor.
}
無効の 宙返り飛行()
{
byte dht11_dat[5];//Array to 持つ/拘留する the bytes sent from sensor. 0-4 should be enough
//don't believe dht11_dat[5] is used.
byte dht11_in;
byte i;
// Send "start read and 報告(する)/憶測" 命令(する) to sensor....
// 1. pull-負かす/撃墜する i/o pin for 18ms
PORTC &= ~_BV(DHT11_PIN);
延期する(18);
延期する(5);//TKB, from Quine at Arduino 会議
//Aosong datasheet for DHT22 says pin should be low at least
//500us. I infer it can be low longer without any]
//刑罰,罰則 apart from making "read sensor" 過程 take
//longer
PORTC |= _BV(DHT11_PIN);//Bring line high again
delayMicroseconds(30);//DHT22 datasheet says host should
//keep line high 20-40us, then watch for sensor taking line
//low. That low should last 80us. 認めるs "start read
//and 報告(する)/憶測" 命令(する).
//Getting the 延期する exacly 権利 is 批判的な...
// put 40 in for the 延期する, in one of the later
// 版s of this code, and the program
// will work SOMETIMES, but not ALWAYS.
// You have to 許す for 過程ing 総計費s,
// etc, too. Using, say, 30 won't "magically"
// make the TOTAL 延期する (with 総計費s, etc)
// 正確に/まさに 30.0000 milliseconds. (This 見解/翻訳/版 may
// also have been unreliable with 延期する=40.. now
// deprecated.)
DDRC &= ~_BV(DHT11_PIN);//Change Arduino pin to an input, to
//watch for the 80us low explained a moment ago.
delayMicroseconds(40);
dht11_in = PINC & _BV(DHT11_PIN);
if(dht11_in){
Serial.println("dht11 start 条件 1 not met");
return;
}
delayMicroseconds(80);
dht11_in = PINC & _BV(DHT11_PIN);
if(!dht11_in){
Serial.println("dht11 start 条件 2 not met");
return;
}
//After 80us low, the line should be taken high for 80us by the
//sensor. The low に引き続いて that high is the start of the first
//bit of the forty to come. The 決まりきった仕事 "read_dht11_dat()"
//推定する/予想するs to be called with the system already into this low.
delayMicroseconds(80);
//now ready for data 歓迎会... 選ぶ up the 5 bytes coming from
// the sensor
for (i=0; i<5; i++)
dht11_dat[i] = read_dht11_dat();
DDRC |= _BV(DHT11_PIN);//回復する pin to in?out?put 義務s
PORTC |= _BV(DHT11_PIN);//re-(dis?) connect 内部の pullup?
byte dht11_check_sum = dht11_dat[0]+dht11_dat[1]+dht11_dat[2]+dht11_dat[3];
// check check_sum
if(dht11_dat[4]!= dht11_check_sum)
//says "if fifth byte from sensor not the same as the sum of the first four..."
{
Serial.println("DHT11 checksum error");
}
Serial.print("現在の 湿度 = ");
Serial.print(dht11_dat[0], DEC);
Serial.print(".");
Serial.print(dht11_dat[1], DEC);
Serial.print("% ");
Serial.print("気温 = ");
Serial.print(dht11_dat[2], DEC);
Serial.print(".");
Serial.print(dht11_dat[3], DEC);
Serial.println("C ");
延期する(800);//Don't try to 接近 too frequently
}My first experience of this 明らかな family of 装置s (機の)カム with the 井戸/弁護士席 made module from nuelectronics, part of their 範囲 for their very reasonably 定価つきの datalogging 保護物,者 for the Arduino. It was 述べるd as 存在 DHT11 based.
In the course of working with that, I 遭遇(する)d について言及する of a DHT22 and 設立する the AM2302 and AM2303 on the aosong.com 場所/位置.
Now- I am not complaining. 多重の 製品 codes and 類似の 装置s are just a fact of life. I を取り引きする it (mostly). But I について言及する it in 事例/患者 you are also doing 研究 and wonder if you are going mad. (An Aosong datasheet 明確に 明言する/公表するs that the AM2302 is also called a DHT22.)
I believe that the four 装置s, if they are not two 装置s by two 指名するs, or something like that, can be 交換d. The 金物類/武器類 appears to have 類似の specs. It seems that it would 答える/応じる "nicely" to the same ソフトウェア.
One "little 汚い": I 嫌疑者,容疑者/疑う that the DHT11, while it will return something for the わずかの parts of the 湿度 and 気温s sensed, will only ever return "無". This might seem daft, but think about it: If the DHT22 returns something other than 無 for the わずかの part, isn't it nice that you don't have to change your code if you decide to 昇格 your sensor? That's what I guess the logic to be, anyway. I've looked closely at the code, and it seems to me that the sensor truly is returning 無s; it does not seem that there's a bug in the code.
Word of advice: While the sensor does have a 気温 sensor, that may be 影響する/感情d by 存在 on the PCB with the 湿度 sensor. While it will of course be an 指示,表示する物 of ambient 気温s, when 追加するing a separate 気温 sensor is so 平易な, if you need 正確な 気温s, it might make sense to do that.
After the next section, specifications, we will start a long 探検 of the 詳細(に述べる)s of using these sensors. Remember that you can Just Use the code already 現在のd.
Here are a few things I've learned about the "care and feeding" of the sensors...
The sensors should not be used where there is very high 湿度- steam rooms, or places dew 落ちるs. (The DHT11 明示するs the operating 範囲 as 20-90% RH.)
Exposing them to some 化学製品 vapors may 干渉する with the sensor and degrade its sensitivity.
If you have a problem arising from too much 湿度, or some bad vapors, the に引き続いて may 影響 a cure:
Step one: Expose the sensor to a 乾燥した,日照りの (<10% RH) 50° to 60° Celsius for 2 hours.
Step two: Expose the sensor to a 70% - 85% RH at 20° to 30° Celsius for 5 hours.
公式文書,認める: 親族 湿度 堅固に depend on 気温. That is why 気温 補償(金) 科学(工学)技術 is used in the sensor to 促進する 正確な 測定 of RH. If you have a 調印(する)d jar with 50% RH at one 気温, at a different 気温, you may see a different RH... or you may not. It may be necessary to arrange for the 圧力 in the jar to stay constant 同様に as the water in the 空気/公表する, while changing the 気温. 底(に届く) line: 親族 湿度 is NOT a simple 概念!
Long time (危険などに)さらす to strong light or 紫外 may degrade the sensor's 業績/成果.
At the Aosong website, I learned that it isn't recommended that the sensor be interrogated too frequently. The 最小限 time between reads for one was 1.7 seconds, for another: 2 seconds. Not "long" in human 条件, but without a "延期する" line in your "宙返り飛行()" 機能(する)/行事, you're going to read too often!
The 支援する of my sensor, from nuelectronics, July 2010, says:
From the Aosong website, I 選ぶd up the に引き続いて for 類似の sensors...
Data on the AM2302. The data sheet says that "DHT22" is another 指名する for the AM2302
Data on the AM2303.
P.S. Oh dear. The に引き続いて, it turned out after I'd spent about three ours on it, isn't やめる 権利! But 恐れる not! It is All Good Stuff. The only part that is wrong is how the sensor sends the forty 1's and 0's it sends. Believe almost everything you see below. Just be ready to "adjust" things you read about how the 1's and 0's are "seen". Everything else is 罰金 as it is! Whew. Even the わずかに wrong story is worthy of 熟考する/考慮する, as some systems work 正確に/まさに as below.
I am now, as I hope you will see, carrying on what I was 説 earlier. What follows is meant to give 負わせる to my 主張s about the DHT11 really just returning 無s for the "fraction" part of the readings. It is also part of your 準備 for later discussions about how the code is put together.
In 幅の広い 条件, here's how the sensor module 作品:
It 一般に "just sits there". It has to be "woken up" by a signal from the system it is plugged into, hereafter called the "host". For the code above to "just work", the host must be an Arduino, but the 議定書 is simple, and should be 平易な enough to port to other microcontrollers, e.g. PICs.
Once "woken up", the sensor, after an interval, sends forty 1's and 0's 支援する to the host, as one big undifferentiated stream. This makes it (比較して) 平易な to read the 返答. It is up to the host to "chop" that stream into five 8 bit binary numbers.
I should について言及する two things in passing:
1) The sensor has four pins, but only three of them are used: Vcc, data, ground.
2) The data pin is いつかs sending 生産(高) from the sensor, いつかs 受託するing data into the sensor. Be careful: You can't interconnect two 生産(高)s willy/ nilly. But THIS data pin, and an Arduino data pin can talk to one another, swapping data direction as the conversation proceeds, if you use the 権利 ソフトウェア.
To make things 平易な, we will say that the 湿度 is 11.22 %, and the 気温 is 33.44 degrees C.
If those were the 条件s, I would 推定する/予想する the に引き続いて stream of 1's and 0's from the sensor when it is asked to 答える/応じる.....
0000101100010110001000010010110001101110
What?? I hear you cry!!
恐れる not... I'll explain. But I want beginners aware of as much of the 詳細(に述べる) as I can 伝える.
Like that, the datastream looks hopelessly arcane. But, if you were to "look" at what comes from the sensor, I think that's what you would see. For the 条件s I 明示するd.
Let's break it up into it's parts. There's nothing in the "real world", nothing in the signal, voltages, タイミングs, etc, to tell anyone where to 分裂(する) that stream into five 封鎖するs of 8 bits. You just have to "know"
My 事前の mess, with a few carriage returns, becomes...
00001011 00010110 00100001 00101100 01101110
If you 変える those numbers from binary to decimal, you get....
11 22 33 44 110
Those are the simplest possible 解釈/通訳s of those patterns of binary digits, bits, 1's and 0's, after they have been grouped into 始める,決めるs of eight. "by eight"s... "byte"s... get it? (Not important if not)
公式文書,認める that there are other ways of 解釈する/通訳するing bytes. 01000001 is いつかs used to stand for "A". Or for 41. But today, happily, we aren't "存在 clever".
Remember that I 規定するd an 環境 with that pattern in it's 人物/姿/数字s. We're not done with the first four numbers, but we're going to look at the last one next.
The sensor's designers were worried that the message from the sensor might be misread by the host. So they used an old trick: A checksum. As the sensor was sending 11, 22, 33 and 44, it was 追加するing them together. The result is 110. The sensor then sent that to the host, too. As the host received the first four numbers, it, 独立して, 追加するd up the numbers it had received. The host then fetched the fifth number, and compared that to the total the it had. If they match, the data was probably received 正確に. (It would be possible for the data to have been received as 10,23,33,44,110 which would still pass the checksum 実験(する)... but 正確に/まさに complementary errors are 極端に ありそうもない.
Going 支援する to the first four numbers "hidden" in the forty bits: I lied. I said I'd 推定する/予想する the sensor to send the binary stream 明言する/公表するd. In fact, if the sensor is a DHT11, I would guess it would 現実に send the binary 同等(の) of....
11 0 33 0 44
公式文書,認める it is and it isn't sending the わずかの part of the 湿度 and 気温. Instead of a meaningful number in the 2nd and 4th parts of the datastream, a DHT11, I think, always sends a 無. From things I have read, I think the DHT22 does send meaningful numbers for the わずかの parts of the readings. I.e., I think a DHT22 would send the binary 同等(の) of 11,22,33,44,110
I am やめる 確信して that my しっかり掴む of this is 訂正する as the ソフトウェア does look at the checksum, and it does not read the 2nd and 4th parts of the stream 異なって from how it reads the 残り/休憩(する).
Enough? Remember: the code at the 最高の,を越す does work! The 残り/休憩(する) of this is knowledge for anyone trying to tinker with it. You have to understand the sensor before you can 令状, or 分析する, code that will work with it!
The "strange" stuff in the code above may 単に 反映する the experience of the person who wrote it. It is elegant, it does use perfectly respectable "words" and 操作者s. But it also goes 井戸/弁護士席 beyond some of the basic "words" and 操作者s familiar to beginners.
Remember that the 職業 of the ソフトウェア is to watch a 数字表示式の input while forty 1's and 0's go by? How does the ソフトウェア "know" that the first bit has been sent, and that what it is now "seeing" is the second bit? If the first two bits are 1 and 0, then the change of the pin's 明言する/公表する is a hint. But how can ソフトウェア know the difference between 00010 and 001000 by changes of 明言する/公表する alone?
The next paragraph isn't やめる 権利 for how the DHT11 作品... but, for the moment, pretend it is!
The ソフトウェア can't tell the difference between 00010 and 001000. The "trick" is that the sensor sends the 1's and 0's at a very 正確な 速度(を上げる).
Let's pretend that it is sending one bit per second, starting 5 seconds after the signal from the host to send the 湿度 and 気温 data. The に引き続いて ソフトウェア would then be what we need....
While the "延期する before start" is not 5 seconds, and the "time per bit" is not 1 second, what 現実に happens is さもなければ as the above.... with a little 新たな展開...
The sending of the data is (almost) 正確に/まさに as above.
The receiving is fancied up a bit. In pseudo-code:
Create 5 element array DataByte[0]... DataByte[4]
Create variables ECounter, BCounter
(for "element 反対する, bit 反対する)
Tell sensor to start sending
Wait for the period "time 'til data stream starts"
Wait for one half of the time a bit is left on the data line
反対する=0
REPEAT:
DataByte[ECounter]=0
FOR BCounter= 0 to 7 do...
Read a bit. Change contents of DataByte[ECounter]
Wait for start of next bit
//End of FOR 宙返り飛行
ECounter=ECounter+1
UNTIL ECounter > 4
That "Change contents" is deliberately a bit vague. 基本的に, as we go through the "FOR" 宙返り飛行, if we get 0,0,0,0,0,1,0,1, we should, at the end, have "5" in the 関連した element of the DataByte array. (Binary 000001010 is "5" in our usual system for 令状ing 負かす/撃墜する numbers.) The "tricks" for 組み立てる/集結するing a number from a stream of bits can give you a 頭痛, but they are simple... really. When you know how. But they'd be a distraction here. Just 信用 me?
A 詳細(に述べる): We are assuming, with 推論する/理由, that the time to "read a bit" and to "change contents" will be but a tiny fraction of the time a bit is 陳列する,発揮するd.
Shake your 長,率いる. 深い breath. That's pretty 井戸/弁護士席 "it". But there's more to say about it, of course.
If you think about what I've just explained, you should see that the ソフトウェア must 遂行する/発効させる at a 正確な 速度(を上げる). That may be the 推論する/理由 that more programmer-friendly words weren't used in the program code.
It turned out, after three hours of creating the above, that there was a small 欠陥 in my しっかり掴む of how the forty 1's and 0's are sent. But don't worry! Almost everything above is 正確に/まさに 権利. And the 詳細(に述べる) I'm about to go into means that the messages from the sensor are more 強健な than they would be in the シナリオ I painted.
I went to the aosong.com 場所/位置 and collected the datasheet for an AM2302/ DHT22. You may think that it "isn't (疑いを)晴らす", but I 保証する you: It is much more (疑いを)晴らす than many 文書s of its 肉親,親類d. The author may not use English as his first language, but he does 伝える what you need to know. Want to complain? Learn 伝統的な Chinese and read the 初めの datasheet. The datasheet's 翻訳家's English is brilliant compared to my 伝統的な Chinese.
There's very little wrong in what I've told you so far. The problem was in the bit where I asked/ said....
"How can the ソフトウェア know the difference between 00010 and 001000 by changes of 明言する/公表する alone? It can't. The "trick" is that the sensor sends the 1's and 0's at a very 正確な 速度(を上げる).
That's only true if the system 簡単に sends a 0 or a 1 for a 直す/買収する,八百長をするd period of time, and then sends the next bit.
The DHT22 (and probably all the other sensors について言及するd in this essay) does something more clever. The "cleverness" has a price: It takes more time to send the forty bits than "my" simple method. But it sends them with a way to keep the sending more reliable. It sends them in a way that is harder to misread.
At the end of getting ready for the forty bits to be sent, and at the end of sending each of them, the data line is high. At the start of sending every bit, be it a 1 or a 0, the sensor 原因(となる)s the data line to go low for 50us (micro seconds). This is the "bit starting" signal. After 50us, the sensor makes the data line high again... for 27us if it is sending a 0, for 70 us if it is sending a 1.
Sorry for the 混乱. Imagine how my heart sank when I saw the error?
The pseudo-code used to say....
... Wait for the time a bit is 陳列する,発揮するd ...
... where it now says...
... Wait for start of next bit ...
以前 you should have worried: "How do I know the next bit has started?".... but I 嫌疑者,容疑者/疑う (and hope) that didn't occur to you. Now that I have 訂正するd my explanation of how the forty 1's and 0's are sent, I hope you aren't worried about knowing a bit is starting?
Within the "read_dht11_dat()" 機能(する)/行事, something やめる clever... if hard to しっかり掴む or explain... is 存在 done. At the start of each pass through the "for..." 宙返り飛行, the program should ALREADY be in the part of the タイミング diagram when the pin is low, the 伝達/伝染 of the 50us "bit starting" signal is 存在 sent. And the "for..." 宙返り飛行 code ends with waiting for the start of the next 50us "bit starting" signal, dataline low.
(The pseudo-code is a bit "off" regarding how the start of the whole "read from sensor" 過程 進歩s, but it isn't far off! The 原則s in reality are the same as the 機械装置 I said was at work. Instead of 確かな periods of time passing, "辛勝する/優位s" (changes from high to low, or from low to high) will be the important 問題/発行するs. Time still plays a 役割, because you are "約束d" that things will happen more or いっそう少なく at 確かな times, and that you will have 明言する/公表するd intervals to "notice" that something has changed 明言する/公表する.)
Wow. I reached this point in this essay perhaps five hours into 令状ing this page, having just spent an 激しい period going through the nuelectronics- 供給(する)d "read sensor" code, looking at each line closely. They... the sensor and the code... are marvelous! I have a few "問題/発行するs" with how the elements of the code are "wrapped"... I shall be making an 代案/選択肢 見解/翻訳/版... but after the 激しい 査察, I see that it is clever! And looks sound! My code, in 原則, will not be very different. It will 異なる just in the organization of the code.
Most of the "hard" code in the program is either 概して 同等(の) to a "pinMode()" 命令(する) (DDRC lines), or a digitalRead() or digitalWrite() ("PORTC |=" lines.) You'll see a mysterious "_BV()" in several places. It takes a binary 00000001 and moves the "1" left a number of places. "_BV(2)" results in 00000100... or is it 00000010? Something like that. I will try to build another page someday, with more 詳細(に述べる)s... 特に if my 試みる/企てる to re-令状 the code without the "clever" bits fails. (My code may be a bit longer than the 初めの, too. There has been a discussion of this 転換 追求(する),探索(する) at the Arduino 会議.)
I've seen 公式文書,認めるs at the Arduino 会議 speaking of interfacing a DHT22... seems as 平易な. This thread also 会談 about the "fraction always 無" 問題/発行する.
There were also "(民事の)告訴s" about these sensors... I'm not 納得させるd (yet!) that there is a problem... but I 港/避難所't had my sensor long.
I don't care if I 解決する it! The 気温 reading is only the 気温 of the 半導体素子, which is 支配する to heating by the 過程s going on within it. It will be loosely connected to ambient 気温... useful... but an 正確な reading of ambient 気温 is best 達成するd by other, simple, 安価な means.
I 港/避難所't time, now, to 令状 up everything that might be said about the に引き続いて. It does essentially what the code above did... but BETTER. Please let me know if you use this, and if you 遭遇(する) any difficulties with it.
For many months, at this point there was text 説 "Try the 見解/翻訳/版 at the 最高の,を越す of the page first"
At 3/13, I can't think why I would have said that. I think it should have said "Try the 見解/翻訳/版 at the BOTTOM of the page first". Sorry.
Anyway... に引き続いて the "最高の,を越す/底(に届く)" text, I had the に引き続いて which remains true... but I'm not sure which 見解/翻訳/版 of the code is which... except that the one at the 底(に届く) of the page is the one I would try first, if I were you!
(Some 見解/翻訳/版 or other... old text... )... MAY be better... but it is いっそう少なく 井戸/弁護士席 実験(する)d than what is below (above?). And what is below(above?) 保持するs comments explaining how I got from the nuelectronics-示唆するd code to what's below(above?)... if you want to know.
ARGH! Here's one 見解/翻訳/版, anyway. And remember: The one at the end of the page is probably the best. It is the 最新の sent to me. I just sort of take it on 約束 that only a sensible person would get around to bothering to send his work, and he must have 設立する something to 直す/買収する,八百長をする to bother 令状ing.
//ReadHumTturDHT11alternate2
//vers 19Jly10
//This is a re-written DHT11/ DHT22 reading code.
//DHT stuff in subroutines.
//See for more (警察などへの)密告,告訴(状)....
//http://sheepdogguides.som/arduino/ar3ne1humDHT11.htm
//N.B. "bit" is used in the 狭くする, computer "1 or 0"
// sense throughout.
//"DHT" from sensor's 指名するs: DHT11, DHT22.
//DHT aka Aosong AM2302, and there's an AM2303 which
//seems to be in the same family.
//Comments on this based on Aosong AM2302, aka DHT22, datasheet.
//Believed to 一般に 適用する to DHT11 同様に, except in the
//事例/患者 of the DHT11, I believe the second and fourth bytes are
//always 無.
//***N.B.****
//The code WORKS... the comments may not yet be EXACTLY 権利.
//See the web-page 特記する/引用するd above for 最新の news.
//This code 作品 with a DHT11 湿度/ 気温 sensing module
//from nuelectronics.com, 従うd with vers 0018 of the Arduino 環境
//Sensor 大(公)使館員d to P4 (nuelectronics 保護物,者)/ analog 0, aka 数字表示式の 14.
//That "module", によれば the
//nuelectronics 場所/位置, and visual 査察 簡単に 供給するs for 平易な
//関係 of an Aosong DHT11 部隊 to the nuelectronics datalogging
//保護物,者. Only 3 wires are 伴う/関わるd: Vcc, ground, and a 選び出す/独身 data
//line. One of the DHT11's 4 pins goes nowhere.
//You should not need to change anything except the next line to use
//the ソフトウェア with the sensor on a different line, or for a DHT22.
//Just "huffing" on the sensor from 深く,強烈に filled 肺s should show
//a 近づく instant rise in 湿度
//#define dht_PIN 0 //no ; here. deprecate ADC0...
//even though we are using it as a 数字表示式の pin.
//Other parts of code 制限する us to using
//ADC0-5, aka D14-19
#define dht_dpin 14 //no ; here. 始める,決める equal to channel sensor is on,
//where if dht_dpin is 14, sensor is on 数字表示式の line 14, aka analog 0
byte bGlobalErr;//for passing error code 支援する from コンビナート/複合体 機能(する)/行事s.
byte dht_dat[5];//Array to 持つ/拘留する the bytes sent from sensor.
無効の 体制/機構(){
InitDHT();//Do what's necessary to 準備する for reading DHT
Serial.begin(9600);
延期する(300);//Let system settle
Serial.println("湿度 and 気温\n\n");
延期する(700);//Wait 残り/休憩(する) of 1000ms recommended 延期する before
//接近ing sensor
}//end "体制/機構()"
無効の 宙返り飛行(){
ReadDHT();//This is the "heart" of the program.
//Fills 全世界の array dht_dpin[], and bGlobalErr, which
//will 持つ/拘留する 無 if ReadDHT went okay.
//Must call InitDHT once (in "体制/機構()" is usual) before
//calling ReadDHT.
//に引き続いて: 陳列する,発揮する what was seen...
switch (bGlobalErr){
事例/患者 0:
Serial.print("現在の 湿度 = ");
Serial.print(dht_dat[0], DEC);
Serial.print(".");
Serial.print(dht_dat[1], DEC);
Serial.print("% ");
Serial.print("気温 = ");
Serial.print(dht_dat[2], DEC);
Serial.print(".");
Serial.print(dht_dat[3], DEC);
Serial.println("C ");
break;
事例/患者 1:
Serial.println("Error 1: DHT start 条件 1 not met.");
break;
事例/患者 2:
Serial.println("Error 2: DHT start 条件 2 not met.");
break;
事例/患者 3:
Serial.println("Error 3: DHT checksum error.");
break;
default:
Serial.println("Error: Unrecognized code 遭遇(する)d.");
break;
}//end "switch"
延期する(800);//Don't try to 接近 too frequently... in theory
//should be once per two seconds, fastest,
//but seems to work after 0.8 second.
}// end 宙返り飛行()
/*Below here: Only "黒人/ボイコット box" elements which can just be plugged 不変の
不変の into programs. 供給する InitDHT() and ReadDHT(), and a 機能(する)/行事
one of them uses.*/
無効の InitDHT(){
//DDRC |= _BV(dht_PIN);//始める,決める data pin... for now... as 生産(高)
//DDRC is data direction 登録(する) for pins A0-5 are on
//PORTC |= _BV(dht_PIN);//始める,決める line high
//PORTC relates to the pins A0-5 are on.
//代案/選択肢 code...
// if (dht_dpin-14 != dht_PIN){Serial.println("ERROR- dht_dpin must be 14 more than dht_PIN");};//end InitDHT
pinMode(dht_dpin,OUTPUT);// 取って代わるs DDRC... as long as dht_dpin=14->19
digitalWrite(dht_dpin,HIGH);//取って代わるs PORTC |= if dht_pin=14->19
}//end InitDHT
無効の ReadDHT(){
/*Uses 全世界の variables dht_dat[0-4], and bGlobalErr to pass
"answer" 支援する. bGlobalErr=0 if read went okay.
Depends on 全世界の dht_PIN for where to look for sensor.*/
bGlobalErr=0;
byte dht_in;
byte i;
// Send "start read and 報告(する)/憶測" 命令(する) to sensor....
// First: pull-負かす/撃墜する i/o pin for 18ms
digitalWrite(dht_dpin,LOW);//Was: PORTC &= ~_BV(dht_PIN);
延期する(18);
延期する(5);//TKB, from Quine at Arduino 会議
/*aosong.com datasheet for DHT22 says pin should be low at least
500us. I infer it can be low longer without any]
刑罰,罰則 apart from making "read sensor" 過程 take
longer. */
//Next line: Brings line high again,
// second step in giving "start read..." 命令(する)
digitalWrite(dht_dpin,HIGH);//Was: PORTC |= _BV(dht_PIN);
delayMicroseconds(30);//DHT22 datasheet says host should
//keep line high 20-40us, then watch for sensor taking line
//low. That low should last 80us. 認めるs "start read
//and 報告(する)/憶測" 命令(する).
//Getting the 延期する exacly 権利 is 批判的な...
// put 40 in for the 延期する, in one of the later
// 版s of this code, and the program
// will work SOMETIMES, but not ALWAYS.
// You have to 許す for 過程ing 総計費s,
// etc, too. Using, say, 30 won't "magically"
// make the TOTAL 延期する (with 総計費s, etc)
// 正確に/まさに 30.0000 milliseconds. (This 見解/翻訳/版 may
// also have been unreliable with 延期する=40.. now
// deprecated.)
//Next: Change Arduino pin to an input, to
//watch for the 80us low explained a moment ago.
pinMode(dht_dpin,INPUT);//Was: DDRC &= ~_BV(dht_PIN);
delayMicroseconds(40);
dht_in=digitalRead(dht_dpin);//Was: dht_in = PINC & _BV(dht_PIN);
if(dht_in){
bGlobalErr=1;//Was: Serial.println("dht11 start 条件 1 not met");
return;
}//end "if..."
delayMicroseconds(80);
dht_in=digitalRead(dht_dpin);//Was: dht_in = PINC & _BV(dht_PIN);
if(!dht_in){
bGlobalErr=2;//Was: Serial.println("dht11 start 条件 2 not met");
return;
}//end "if..."
/*After 80us low, the line should be taken high for 80us by the
sensor. The low に引き続いて that high is the start of the first
bit of the forty to come. The 決まりきった仕事 "read_dht_dat()"
推定する/予想するs to be called with the system already into this low.*/
delayMicroseconds(80);
//now ready for data 歓迎会... 選ぶ up the 5 bytes coming from
// the sensor
for (i=0; i<5; i++)
dht_dat[i] = read_dht_dat();
//Next: 回復する pin to 生産(高) 義務s
pinMode(dht_dpin,OUTPUT);//Was: DDRC |= _BV(dht_PIN);
//N.B.: Using DDRC put 制限s on value of dht_pin
//Next: Make data line high again, as 生産(高) from Arduino
digitalWrite(dht_dpin,HIGH);//Was: PORTC |= _BV(dht_PIN);
//N.B.: Using PORTC put 制限s on value of dht_pin
//Next see if data received 一貫した with checksum received
byte dht_check_sum =
dht_dat[0]+dht_dat[1]+dht_dat[2]+dht_dat[3];
/*条件 in に引き続いて "if" says "if fifth byte from sensor
not the same as the sum of the first four..."*/
if(dht_dat[4]!= dht_check_sum)
{bGlobalErr=3;}//Was: Serial.println("DHT11 checksum error");
};//end ReadDHT()
byte read_dht_dat(){
//Collect 8 bits from datastream, return them 解釈する/通訳するd
//as a byte. I.e. if 0000.0101 is sent, return decimal 5.
//Code 推定する/予想するs the system to have recently entered the
//dataline low 条件 at the start of every data bit's
//伝達/伝染 BEFORE this 機能(する)/行事 is called.
byte i = 0;
byte result=0;
for(i=0; i< 8; i++){
//We enter this during the first start bit (low for 50uS) of the byte
//Next: wait until pin goes high
while(digitalRead(dht_dpin)==LOW);//Was: while(!(PINC & _BV(dht_PIN)));
//signalling end of start of bit's 伝達/伝染.
//Dataline will now stay high for 27 or 70 uS, depending on
//whether a 0 or a 1 is 存在 sent, それぞれ.
delayMicroseconds(30);//AFTER pin is high, wait その上の period, to be
//into the part of the タイミング diagram where a 0 or a 1 denotes
//the datum 存在 send. The "その上の period" was 30uS in the ソフトウェア
//that this has been created from. I believe that a higher number
//(45?) would be more appropriate.
//Next: Wait while pin still high
if (digitalRead(dht_dpin)==HIGH)//Was: if(PINC & _BV(dht_PIN))
result |=(1<<(7-i));// "追加する" (not just 新規加入) the 1
//to the growing byte
//Next wait until pin goes low again, which signals the (米ソ間の)戦略兵器削減交渉
//of the NEXT bit's 伝達/伝染.
while (digitalRead(dht_dpin)==HIGH);//Was: while((PINC & _BV(dht_PIN)));
}//end of "for.."
return result;
}//end of "read_dht_dat()"
Once again (3/13) I find myself 説: I 港/避難所't time, now, to 令状 up everything that might be said about the に引き続いて. It does essentially what the code above did... but BETTER. Please let me know if you use this, and if you 遭遇(する) any difficulties with it. What is below 保持するs comments from previous 見解/翻訳/版s by さまざまな authors of this 発展させるing code. Comments ALWAYS a good idea! (But... isn't it sad? I hesitated to use the word "発展させるing", in 事例/患者 it upset some creationist!)
My thanks to DaveB for 与える/捧げるing the に引き続いて... May it serve everyone 井戸/弁護士席!
//ReadHumTturDHT11alternate2
//vers 08 損なう 13, 09:36 (Dave had an old "見解/翻訳/版" ID here (TKB edit))
//Changes 追加するd by DaveB 5 March 2013 as a result of Thomas Henry's
//article on the DHT22 in the March 2013 問題/発行する of Nuts&Volts.
//Thanks to http://sheepdogguides.com/arduino/ar3ne1humDHT11.htm
//for 供給するing the 初めの code to work from.
//This is a re-written DHT11/ DHT22 reading code.
//DHT stuff in subroutines.
//See for more (警察などへの)密告,告訴(状)....
//http://sheepdogguides.som/arduino/ar3ne1humDHT11.htm
//N.B. "bit" is used in the 狭くする, computer "1 or 0"
// sense throughout.
//"DHT" from sensor's 指名するs: DHT11, DHT22.
//DHT aka Aosong AM2302, and there's an AM2303 which
//seems to be in the same family.
//Comments on this based on Aosong AM2302, aka DHT22, datasheet.
//Believed to 一般に 適用する to DHT11 同様に, except in the
//事例/患者 of the DHT11, I believe the second and fourth bytes are
//always 無.
//***N.B.****
//The code WORKS... the comments may not yet be EXACTLY 権利.
//See the web-page 特記する/引用するd above for 最新の news.
//This code 作品 with a DHT11 湿度/ 気温 sensing module
//from nuelectronics.com, 従うd with vers 0018 of the Arduino 環境
//Sensor 大(公)使館員d to P4 (nuelectronics 保護物,者)/ analog 0, aka 数字表示式の 14.
//That "module", によれば the
//nuelectronics 場所/位置, and visual 査察 簡単に 供給するs for 平易な
//関係 of an Aosong DHT11 部隊 to the nuelectronics datalogging
//保護物,者. Only 3 wires are 伴う/関わるd: Vcc, ground, and a 選び出す/独身 data
//line. One of the DHT11's 4 pins goes nowhere.
//You should not need to change anything except the next line to use
//the ソフトウェア with the sensor on a different line, or for a DHT22.
//Just "huffing" on the sensor from 深く,強烈に filled 肺s should show
//a 近づく instant rise in 湿度
//#define dht_PIN 0 //no ; here. deprecate ADC0...
//even though we are using it as a 数字表示式の pin.
//Other parts of code 制限する us to using
//ADC0-5, aka D14-19
#define dht_dpin 14 //no ; here. 始める,決める equal to channel sensor is on,
//where if dht_dpin is 14, sensor is on 数字表示式の line 14, aka analog 0
byte bGlobalErr;//for passing error code 支援する from コンビナート/複合体 機能(する)/行事s.
byte dht_dat[5];//Array to 持つ/拘留する the bytes sent from sensor.
float lastHumidity; //追加するd by DaveB
float lastTemperatureF; //追加するd by DaveB
float lastTemperatureC; //追加するd by DaveB
const byte mask = 0x7f; //追加するd by DaveB
const int ledPin = 13; // the number of the LED pin, 追加するd by DaveB
int ledState = LOW; // ledState used to 始める,決める the LED, 追加するd by DaveB
無効の 体制/機構(){
InitDHT();//Do what's necessary to 準備する for reading DHT
Serial.begin(9600);
延期する(1000); //Let system settle, changed from 300 to 1000 by DaveB to 許す
//2 sec 延期する before 演説(する)/住所ing DHT22 again.
Serial.println("湿度 and 気温\n\n");
延期する(1000); //Wait 残り/休憩(する) of 1000ms recommended 延期する before 接近ing sensor,
// changed from 700 to 1000 for a total of 2000 by DaveB
// 始める,決める the 数字表示式の pin as 生産(高) to use the Arduino LED, 追加するd by DaveB
pinMode(ledPin, OUTPUT); //追加するd by DaveB
}//end "体制/機構()"
無効の 宙返り飛行(){
ReadDHT();//This is the "heart" of the program.
//Fills 全世界の array dht_dpin[], and bGlobalErr, which
//will 持つ/拘留する 無 if ReadDHT went okay.
//Must call InitDHT once (in "体制/機構()" is usual) before
//calling ReadDHT.
//に引き続いて: 陳列する,発揮する what was seen...
switch (bGlobalErr){
事例/患者 0: //事例/患者 0 changed by DaveB
//Changes 追加するd by DaveB as a result of Thomas Henry's article on the DHT22
//in the March 2013 問題/発行する of Nuts&Volts.
//
//Values received from the DHT22 are in two bytes that 代表する 10 times the value.
//Therefore 256 * first word 加える second word 供給するs 10 times the 湿度 or
//気温 value. MSB of dht_dat[2] tells us if the 気温 is
//肯定的な or 消極的な. MSB value of 0 is pos temp, MSB value of 1 is a neg temp.
lastHumidity = (dht_dat[0]*256+dht_dat[1]); //Calculate float for 10*湿度
lastHumidity = lastHumidity/10; //Calculate 湿度
Serial.print("湿度: ");
Serial.print(lastHumidity);
Serial.println("% ");
//dht_dat[2] = dht_dat[2] + 128; // debug for showing 消極的な 気温, uncomment to show neg temps
// check for MSB 始める,決める in dht_dat[2] which means 消極的な 気温
if ((dht_dat[2] & mask) != dht_dat[2]) { //if they're different, it's 消極的な
dht_dat[2]= dht_dat[2] & mask;
lastTemperatureC = -1*(dht_dat[2]*256+dht_dat[3]); //Calculate neg float 10*気温 C
}
else { //if they're not different, it's 肯定的な
lastTemperatureC = (dht_dat[2]*256+dht_dat[3]); //Calculate float 10*気温 C
}
lastTemperatureC = lastTemperatureC/10; //Calculate 気温 C
Serial.print("Temp: ");
Serial.print(lastTemperatureC); //Uncomment these two lines for C temps
Serial.print("C, "); //Uncomment these two lines for C temps
lastTemperatureF=lastTemperatureC*9/5+32; //Calculate 気温 F
Serial.print(lastTemperatureF);
Serial.println("F ");
Serial.println("");
if (ledState == LOW)// alternately light and 消滅させる the LED
ledState = HIGH;
else
ledState = LOW;
digitalWrite(ledPin, ledState);
break;
事例/患者 1:
Serial.println("Error 1: DHT start 条件 1 not met.");
break;
事例/患者 2:
Serial.println("Error 2: DHT start 条件 2 not met.");
break;
事例/患者 3:
Serial.println("Error 3: DHT checksum error.");
break;
default:
Serial.println("Error: Unrecognized code 遭遇(する)d.");
break;
}//end "switch"
//延期する changed from 800 to 2000 by DaveB
延期する(2000);//Don't try to 接近 too frequently... in theory
//should be once per two seconds, fastest,
//but seems to work after 0.8 second.
}// end 宙返り飛行()
/*Below here: Only "黒人/ボイコット box" elements which can just be plugged 不変の
不変の into programs. 供給する InitDHT() and ReadDHT(), and a 機能(する)/行事
one of them uses.*/
無効の InitDHT(){
//DDRC |= _BV(dht_PIN);//始める,決める data pin... for now... as 生産(高)
//DDRC is data direction 登録(する) for pins A0-5 are on
//PORTC |= _BV(dht_PIN);//始める,決める line high
//PORTC relates to the pins A0-5 are on.
//代案/選択肢 code...
// if (dht_dpin-14 != dht_PIN){Serial.println("ERROR- dht_dpin must be 14 more than dht_PIN");};//end InitDHT
pinMode(dht_dpin,OUTPUT);// 取って代わるs DDRC... as long as dht_dpin=14->19
digitalWrite(dht_dpin,HIGH);//取って代わるs PORTC |= if dht_pin=14->19
}//end InitDHT
無効の ReadDHT(){
/*Uses 全世界の variables dht_dat[0-4], and bGlobalErr to pass
"answer" 支援する. bGlobalErr=0 if read went okay.
Depends on 全世界の dht_PIN for where to look for sensor.*/
bGlobalErr=0;
byte dht_in;
byte i;
// Send "start read and 報告(する)/憶測" 命令(する) to sensor....
// First: pull-負かす/撃墜する i/o pin for 18ms
digitalWrite(dht_dpin,LOW);//Was: PORTC &= ~_BV(dht_PIN);
延期する(18);
//db 削除するd に引き続いて line
//延期する(5);//TKB, from Quine at Arduino 会議
/*aosong.com datasheet for DHT22 says pin should be low at least
500us. I infer it can be low longer without any]
刑罰,罰則 apart from making "read sensor" 過程 take
longer. */
//Next line: Brings line high again,
// second step in giving "start read..." 命令(する)
digitalWrite(dht_dpin,HIGH);//Was: PORTC |= _BV(dht_PIN);
delayMicroseconds(30);//DHT22 datasheet says host should
//keep line high 20-40us, then watch for sensor taking line
//low. That low should last 80us. 認めるs "start read
//and 報告(する)/憶測" 命令(する).
//Getting the 延期する exacly 権利 is 批判的な...
// put 40 in for the 延期する, and the program
// will work SOMETIMES, but not ALWAYS.
// You have to 許す for 過程ing 総計費s,
// etc, too. Using, say, 30 won't "magically"
// make the TOTAL 延期する (with 総計費s, etc)
// 正確に/まさに 30.0000 milliseconds.
//Next: Change Arduino pin to an input, to
//watch for the 80us low explained a moment ago.
pinMode(dht_dpin,INPUT);//Was: DDRC &= ~_BV(dht_PIN);
delayMicroseconds(40);
//Read the LO
dht_in=digitalRead(dht_dpin);//Was: dht_in = PINC & _BV(dht_PIN);
if(dht_in){
bGlobalErr=1;//Was: Serial.println("dht11 start 条件 1 not met");
return;
}//end "if..."
delayMicroseconds(80);
dht_in=digitalRead(dht_dpin);//Was: dht_in = PINC & _BV(dht_PIN);
if(!dht_in){
bGlobalErr=2;//Was: Serial.println("dht11 start 条件 2 not met");
return;
}//end "if..."
/*After 80us low, the line should be taken high for 80us by the
sensor. The low に引き続いて that high is the start of the first
bit of the forty to come. The 決まりきった仕事 "read_dht_dat()"
推定する/予想するs to be called with the system already into this low.*/
delayMicroseconds(80);
//now ready for data 歓迎会... 選ぶ up the 5 bytes coming from
// the sensor
for (i=0; i<5; i++)
dht_dat[i] = read_dht_dat();
//Next: 回復する pin to 生産(高) 義務s
pinMode(dht_dpin,OUTPUT);//Was: DDRC |= _BV(dht_PIN);
//N.B.: Using DDRC put 制限s on value of dht_pin
//Next: Make data line high again, as 生産(高) from Arduino
digitalWrite(dht_dpin,HIGH);//Was: PORTC |= _BV(dht_PIN);
//N.B.: Using PORTC put 制限s on value of dht_pin
//Next see if data received 一貫した with checksum received
byte dht_check_sum =
dht_dat[0]+dht_dat[1]+dht_dat[2]+dht_dat[3];
/*条件 in に引き続いて "if" says "if fifth byte from sensor
not the same as the sum of the first four..."*/
if(dht_dat[4]!= dht_check_sum)
{bGlobalErr=3;}//Was: Serial.println("DHT11 checksum error");
};//end ReadDHT()
byte read_dht_dat(){
//Collect 8 bits from datastream, return them 解釈する/通訳するd
//as a byte. I.e. if 0000.0101 is sent, return decimal 5.
//Code 推定する/予想するs the system to have recently entered the
//dataline low 条件 at the start of every data bit's
//伝達/伝染 BEFORE this 機能(する)/行事 is called.
byte i = 0;
byte result=0;
for(i=0; i< 8; i++){
//We enter this during the first start bit (low for 50uS) of the byte
//Next: wait until pin goes high
while(digitalRead(dht_dpin)==LOW);//Was: while(!(PINC & _BV(dht_PIN)));
//signalling end of start of bit's 伝達/伝染.
//Dataline will now stay high for 27 or 70 uS, depending on
//whether a 0 or a 1 is 存在 sent, それぞれ.
delayMicroseconds(30);//AFTER pin is high, wait その上の period, to be
//into the part of the タイミング diagram where a 0 or a 1 denotes
//the datum 存在 send. The "その上の period" was 30uS in the ソフトウェア
//that this has been created from. I believe that a higher number
//(45?) would be more appropriate.
//Next: Wait while pin still high
if (digitalRead(dht_dpin)==HIGH)//Was: if(PINC & _BV(dht_PIN))
result |=(1<<(7-i));// "追加する" (not just 新規加入) the 1
//to the growing byte
//Next wait until pin goes low again, which signals the (米ソ間の)戦略兵器削減交渉
//of the NEXT bit's 伝達/伝染.
while (digitalRead(dht_dpin)==HIGH);//Was: while((PINC & _BV(dht_PIN)));
}//end of "for.."
return result;
}//end of "read_dht_dat()"
--------------------------------------
So! Thank you DaveB. Let's see how long that 見解/翻訳/版 stands up to the community's scrutiny! (^_^)
... until the next person sends me a "better better" to 株 with you all?
本気で: 改良s welcome. I am always glad to publish a link to any 場所/位置 contributors want 促進するd... be it their own, or a worthwhile charity, etc. (The most 最近の contributor 拒絶する/低下するd the 申し込む/申し出)
Also, the 成果/努力 of the さまざまな people who have written to 与える/捧げる 改良s have done a real service to "those who've come after them", don't you think? I just わびる that the 初めの code was 欠陥のある! I thought it worked, when I used it, but, maybe not? Oh 井戸/弁護士席. 価値(がある) what you paid for it!
=========Flattr: Have you heard of Flattr? 広大な/多数の/重要な new idea to make it 平易な for you to send small thank you$ to people who 供給する Good Stuff on the web. If you want to send $$erious thank yous, there are better ways, but for a small "tip" here and there, Flattr ticks a lot of boxes which no one else has 設立する a way to do yet. Please at least check out my introduction to Flattr, if you 港/避難所't heard of it? "No 義務", as they say!
その上の to the Arduino ideas here, I have 地位,任命するd a 一連の essays which try to help you become a better Arduino programmer and engineer... but, for the best result, you will have to buckle 負かす/撃墜する and work your way through them in sequence. The "How To's" here can be 接近d in whatever order you like.
Feel 解放する/自由な to use this (警察などへの)密告,告訴(状) in programming courses, etc, but a credit of the source would be 高く評価する/(相場などが)上がるd. If you 簡単に copy the pages to other web pages you will do your readers a disservice: Your copies won't stay 現在の. Far better to link to these pages, and then your readers see up-to-date 見解/翻訳/版s. For those who care- thank you- I have 地位,任命するd a page with more (警察などへの)密告,告訴(状) on what copyright 権利放棄s I 延長する, and suggestions for those who wish to put this 構成要素 on CDs, etc.
See the discussion 近づく the 底(に届く) of the "最高の,を越す level" page covering the 本体,大部分/ばら積みの of my Arduino 出資/貢献s. There is (警察などへの)密告,告訴(状) there, too, about things like "May I copy your 構成要素?", and the system of とじ込み/提出する 指名するs I am trying to work to.
If you visit 1&1's 場所/位置 from here, it helps me. They host my website, and I wouldn't put this link up for them if I wasn't happy with their service... although I was いっそう少なく than pleased the other day to have what I was doing interrupted by a telephone call from their sales team, trying to get me to 延長する my 関与. Sigh. Hardly a rare event, but I'd thought 1&1 were a bit classier that some of the people who have my telephone number.
Click here to visit editor's Sheepdog ソフトウェア (tm) freeware, shareware pages.
Click here to visit the homepage of my biggest 場所/位置.
Click here to visit the homepage of Sheepdogsoftware.co.uk. 陳謝s if the "?Frmar3ne1humDHT11" I 追加するd to that link 原因(となる)s your browser problems. Please let me know, if so?
Page has been 実験(する)d for 同意/服従 with INDUSTRY (not MS-only) 基準s, using the 解放する/自由な, 公然と accessible validator at validator.w3.org. Mostly passes.
....... P a g e . . . E n d s .....