RobTillaart / DHTNew

Arduino library for DHT11 and DHT22 with automatic sensor recognition
MIT License
98 stars 15 forks source link

pin setting at runtime #46

Closed app-vrb closed 3 years ago

app-vrb commented 3 years ago

Hi, I am looking for a library with the possibility to define the pin at runtime. It is possibile with this one? Thanks

RobTillaart commented 3 years ago

You can create runtime an DHTNEW object with the pin you want. Never tried it however, please let me know if it works and I will add it to the examples

//
//    FILE: dhtnew_runtime.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: DHTNEW library test sketch
//     URL: https://github.com/RobTillaart/DHTNew
//
// HISTORY:
// 0.1.0    2020-01-04  intial version
//
// DHT PIN layout from left to right
// =================================
// FRONT : DESCRIPTION
// pin 1 : VCC
// pin 2 : DATA
// pin 3 : Not Connected
// pin 4 : GND

#include <dhtnew.h>

//  DHTNEW mySensor(16);  not needed

uint32_t lastTime = 0;

void setup()
{
  Serial.begin(115200);
  Serial.println(__FILE__);
  Serial.println(DHTNEW_LIB_VERSION);
}

void loop()
{
  if (millis() - lastTime > 2000)
  {
    lastTime = millis();
    for (int pin = 6; pin < 10; pin++)
    {
      DHTNEW sensor(pin);
      sensor.read();
      Serial.print(pin);
      Serial.print("\t");
      Serial.print(sensor.getHumidity(), 1);
      Serial.print("\t");
      Serial.println(sensor.getTemperature(), 1);
    }
  }

  // Do other things here
}

// END OF FILE
app-vrb commented 3 years ago

Hi Rob thanks for your answer.

One question: The line:

DHTNEW mySensor(16); It is mandatory to be before the setup()? The question is because I know the pin just inside the setup().

Thanks again for your support. Francesco

In data 4 gennaio 2021 15:52:51 Rob Tillaart notifications@github.com ha scritto:

You can create runtime an DHTNEW object with the pin you want. Never tried it however, pleas elet me know if it works and I will add it to the examples

// // FILE: dhtnew_runtime.ino // AUTHOR: Rob Tillaart // VERSION: 0.1.0 // PURPOSE: DHTNEW library test sketch // URL: https://github.com/RobTillaart/DHTNew // // HISTORY: // 0.1.0 2020-01-04 intial version // // DHT PIN layout from left to right // ================================= // FRONT : DESCRIPTION // pin 1 : VCC // pin 2 : DATA // pin 3 : Not Connected // pin 4 : GND

include

DHTNEW mySensor(16);

uint32_t lastTime = 0;

void setup() { Serial.begin(115200); Serial.println(FILE); Serial.println(DHTNEW_LIB_VERSION); }

void loop() { if (millis() - lastTime > 2000) { for (int pin = 6; pin < 10; pin++) { DHTNEW sensor(pin); sensor.read(); Serial.print(pin); Serial.print("\t"); Serial.print(sensor.getHumidity(), 1); Serial.print("\t"); Serial.println(sensor.getTemperature(), 1); } }

// Do other things here }

// END OF FILE

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

RobTillaart commented 3 years ago

ooops , copy paste error ... I will comment it in the example