Redaclaration problem

Hi guys,

I’ve been stuck for a while with the following problem. I have some DHT22 sensors to measure temperature. I’m using a library to read them out but when i initialize the library i assing a certain pin as output pin. But since i’m using multiple sensors i’ve multiple output pins.

What i need to do is change the output pin in the library every time i read another pin. But it seems that i can’t redeclare the library, any ideas?

int eeprom_index = 0;
#include "DHT.h"
#define DHTTYPE DHT22 // define sensor type

void setup() {
  Serial.begin(9600);
  
  Serial.println("starting up");
  pinMode(13,OUTPUT);
  digitalWrite(13,LOW); //make sure that this one doesn't consume power
  delay (500);
}


void loop() {
  unsigned long time_step = 300000;
  for (int i = eeprom_index; i <1300; i++){
    #define DHTPIN 2     // what digital pin we're connected to
    DHT dht(DHTPIN, DHTTYPE); //Initialize DHT sensor.
    dht.begin();
    unsigned int h1 = round(dht.readHumidity());
    unsigned int t1 = round(dht.readTemperature()*5);
    Serial.print("Humidity: ");
    Serial.print(h1);
    Serial.print("   ");
    Serial.print("Temperature: ");
    Serial.println(t1/5.0);
    #define DHTPIN 3     // what digital pin we're connected to
    DHT dht(DHTPIN, DHTTYPE); //Initialize DHT sensor.
    dht.begin();
    unsigned int h2 = round(dht.readHumidity());
    unsigned int t2 = round(dht.readTemperature()*5);
    Serial.print("Humidity: ");
    Serial.print(h2);
    Serial.print("   ");
    Serial.print("Temperature: ");
    Serial.println(t2/5.0);
   delay(1000);
  }
}

You mean input pin? A sensor is an input device (edit: wow I really wrote that? I must have been drunk, sorry)

Simply use a different name and pin number, in the constructor:

DHT dht1( 2, DHTTYPE ); //Initialize DHT sensor 1, on pin 2.
DHT dht2( 3, DHTTYPE ); //Initialize DHT sensor 2, on pin 3.
...

If you have more than 2 sensors, use an array and for loops.

Or I didn't understand your question?

    #define DHTPIN 2     // what digital pin we're connected to

It does not make sense to have a preprocessor directive in the loop() function.

Why are you looping from 0 to 1300? Surely you don't plan to have 1300 sensors connected to one Arduino, do you? Have you counted the available pins?

Why are you calling the constructor twice in the for loop, creating the same instance? Sounds like the Bob Newhart skit with my brother Darryl and my other brother Darryl.

Hi guys thanks for the fast replies!

I meant to say input pin ofcourse :p I think what you said will solve my problem indeed guix! I'm going to try it out right now. And the for loop is because it's part of a code cut out of my other program, it shouldn't be there. (Normally it's used to write the data to different memory places of the eeprom memory).

Aight this works thanks guys! I just don't completely understand what you mean with the 'array' thing. I'm planning to use 4 of those sensors, why should i use an array?

why should i use an array?

To avoid the need to have the same code multiple times in the same program.