Is there any way to use the following code in arduino ide (with dht library adafruit) :

IS THERE ANY WAY TO USE THE FOLLOWING CODE IN ARDUINO IDE (WITH DHT LIBRARY ADAFRUIT) :

PD. DO I HAVE TO DEFINE AS VECTOR THE dht[] for Reading from sensors ???????

float h[]={0,0,0,0,0};

float t[]={0,0,0,0,0};

VGPIO[]={25,26,27,28,29};

VMODEL[]={ "DHT22","DHT22","DHT22","DHT11","DHT11"}

for (int i=0, i < 5, i++) { DHT dht[i](VGPIO[i],VMODEL[i]); }

void setup() {

                for (int i=0, i < 5, i++) { dht\[i\].begin(); }

               }

void loop() {

                for (int i=0, i < 5, i++) { h\[i\]= dht\[i\].readHumidity();

                                                    t\[i\]= dht\[i\].readTemperature(); }

               }

First read the Pinned post re 'How to get the most from the forum'. Then edit your post and put code tags around the code.

Why are you screaming. To answer your question you can use that code, I have no idea of what it will do. To be safe put it in a function and never call that function.

No you can't have a for loop outside a function

āžœ In C++ you cannot directly create objects in an array with different constructor arguments using the for loop initializer syntax you tried.

You need to declare an array of objects and initialize them properly. With the Adafruit DHT library, the constructor takes (pin, type) where type is a constant like DHT22 or DHT11, not a string.

The equivalent static array setup would look like this:

#include <DHT.h>

const uint8_t VGPIO[] = {25, 26, 27, 28, 29};
const uint8_t VMODEL[] = {DHT22, DHT22, DHT22, DHT11, DHT11};

DHT dht[] = {
  DHT(VGPIO[0], VMODEL[0]),
  DHT(VGPIO[1], VMODEL[1]),
  DHT(VGPIO[2], VMODEL[2]),
  DHT(VGPIO[3], VMODEL[3]),
  DHT(VGPIO[4], VMODEL[4])
};

const size_t numDht = sizeof dht / sizeof *dht;

void setup() {
  Serial.begin(115200);
  for (size_t i = 0; i < numDht; i++) dht[i].begin();
}

void loop() {}

You can also get rid of the constant arrays and have that directly into the DHT array initialisation

DHT dht[] = {
  {25, DHT22},
  {26, DHT22},
  {27, DHT22},
  {28, DHT11},
  {29, DHT11}
};

PS: Please correct your post and add code tags around your code.

There is a small pencil image below your existing posts.

  • click on this pencil āžœ that will let you edit your post.
  • Select the part of the text that corresponds to the code
  • Click on the <code/> icon in the toolbar to indicate that it is code
  • click image Save Edit

(Also make sure to properly indent the code in the IDE before copying and pasting it here. This can be done by pressing ctrlT on a PC or cmdT on a Mac)

Hi, @logisoft
Welcome to the forum.

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Tom.... :smiley: :+1: :coffee: :australia: