2017-02-24 39 views
1

我有一個Arduino Pro Mini 5v,16 mhz,它連接到引腳2上的數字開關。此開關用於通過外部數字中斷將Arduino從睡眠中喚醒。我還有一個連接到引腳9的DHT11溫度傳感器。我想要達到的是當Arduino喚醒5秒鐘並且引腳2上的開關爲高電平時,我想讀取溫度傳感器並返回溫度。我正在使用Tillart的DHT11庫,當我這樣做時,它會返回一個TIME_OUT錯誤。我唯一可能的解釋是,當DHT11和引腳2上的開關一起讀取時,電壓會發生改變?任何指向解決方案的指針都將不勝感激。謝謝。外部數字中斷和dht11

編輯1:添加的代碼

 #include <LowPower.h> 
     #include <dht.h> 
     int pin2 = 2; 
     dht DHT; 
     #define DHT11_PIN 9 

     void pin2interrupt(void) 
     { 
      // Function called when awoken from sleep 
      // Detach interrupt to stop it from continuosly firing when in normal mode 
     } 

     void enterSleep(void) 
     { 
      attachInterrupt(0, pin2interrupt, HIGH); 
      Serial.println("Sleeping"); 
      delay(100); 
      LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); 
      Serial.println("Awake!"); 
     } 


     void setup() 
     { 
      Serial.begin(115200); 
      pinMode(pin2, INPUT); 
      pinMode(DHT11_PIN, INPUT); 
     } 

     int seconds = 0; 

     void loop() 
     { 
      delay(1000); 
      seconds++; 
      Serial.println("Awake in the loop!"); 
      Serial.println(seconds); 

      if (digitalRead(pin2) == LOW && seconds == 5) 
      { 
       seconds = 0; 
       Serial.println("No child detected, so going to sleep!"); 
       delay(200); 
       enterSleep(); 
      } 
      else if (seconds == 5) 
      { 
       Serial.print("DHT11, \t"); 
       int chk = DHT.read11(DHT11_PIN); 
       switch (chk) 
       { 
        case DHTLIB_OK: 
        Serial.print("OK,\t"); 
        break; 
        case DHTLIB_ERROR_CHECKSUM: 
        Serial.print("Checksum error,\t"); 
        break; 
        case DHTLIB_ERROR_TIMEOUT: 
        Serial.print("Time out error,\t"); 
        break; 
        default: 
        Serial.print("Unknown error,\t"); 
        break; 
       } 
      // DISPLAY DATA 
      Serial.println(DHT.temperature, 1); 

      delay(2000); 
      seconds = 0; 
      } 
     } 

編輯2:我也忘了提,我使用的是低功耗庫通過RocketScream把Arduino的睡覺。這個庫可以在這裏找到:https://github.com/rocketscream/Low-Power

+0

@PatrickTrentin對不起。添加了代碼。希望它的格式正確。 – Jigmeister

+0

你會嘗試改變attachInterrupt(0,pin2interrupt,HIGH);'attachInterrupt(0,pin2interrupt,RISING);'看看會發生什麼?請將'seconds'重命名爲'loop_iterations'。並用'seconds> = 5'替換'seconds == 5'。 –

+0

@PatrickTrentin感謝您花時間給我提出您的建議。我會嘗試並報告結果。 – Jigmeister

回答

0

正如RobTillart在DHT11庫的官方Github頁面上的問題部分所討論的那樣,問題是由於一些DHT11傳感器需要更長的時間將數據傳輸回電路板,數據表中指定的50ms左右。因此,如果您遇到此問題,請嘗試通過減小將F_CPU值除以400左右的值並再次嘗試,來增加dht頭文件上的DHTLIB_TIMEOUT。這使電路板可以等待超過50ms,電路板才能從傳感器接收數據。如果此修補程序不起作用,則可能需要嘗試使用示波器測量響應時間,因爲似乎有些DHT11的構建方式不同。