Convert to Loops

Hey,

is there a more efficient way to code this?

Maybe loops or something?

Would be nice if u give me a just a little hint.

i have everything 4 times…

#include <dht_nonblocking.h>

#define DHT_SENSOR_TYPE DHT_TYPE_22

static const int DHT_SENSOR_PIN1 = 2;
static const int DHT_SENSOR_PIN2 = 3;
static const int DHT_SENSOR_PIN3 = 4;
static const int DHT_SENSOR_PIN4 = 5;

DHT_nonblocking dht_sensor1( DHT_SENSOR_PIN1, DHT_SENSOR_TYPE );
DHT_nonblocking dht_sensor2( DHT_SENSOR_PIN2, DHT_SENSOR_TYPE );
DHT_nonblocking dht_sensor3( DHT_SENSOR_PIN3, DHT_SENSOR_TYPE );
DHT_nonblocking dht_sensor4( DHT_SENSOR_PIN4, DHT_SENSOR_TYPE );

void setup( )
{
  Serial.begin( 115200 );
}

static bool measure_environment1( float *temperature, float *humidity )
{
  static unsigned long measurement_timestamp = millis( );

  /* Measure once every four seconds. */
  if( millis( ) - measurement_timestamp > 5000ul )
  {
    if( dht_sensor1.measure( temperature, humidity ) == true )
    {
      measurement_timestamp = millis( );
      return( true );
    }
  }

  return( false );
}

static bool measure_environment2( float *temperature, float *humidity )
{
  static unsigned long measurement_timestamp = millis( );

  /* Measure once every four seconds. */
  if( millis( ) - measurement_timestamp > 5000ul )
  {
    if( dht_sensor2.measure( temperature, humidity ) == true )
    {
      measurement_timestamp = millis( );
      return( true );
    }
  }

  return( false );
}

static bool measure_environment3( float *temperature, float *humidity )
{
  static unsigned long measurement_timestamp = millis( );

  /* Measure once every four seconds. */
  if( millis( ) - measurement_timestamp > 5000ul )
  {
    if( dht_sensor3.measure( temperature, humidity ) == true )
    {
      measurement_timestamp = millis( );
      return( true );
    }
  }

  return( false );
}

static bool measure_environment4( float *temperature, float *humidity )
{
  static unsigned long measurement_timestamp = millis( );

  /* Measure once every four seconds. */
  if( millis( ) - measurement_timestamp > 5000ul )
  {
    if( dht_sensor4.measure( temperature, humidity ) == true )
    {
      measurement_timestamp = millis( );
      return( true );
    }
  }

  return( false );
}


/*
 * Main program loop.
 */
void loop( )
{
  float temperature1;
  float humidity1;
  float temperature2;
  float humidity2;
  float temperature3;
  float humidity3;
  float temperature4;
  float humidity4;

  /* Measure temperature and humidity.  If the functions returns
     true, then a measurement is available. */
  if( measure_environment1( &temperature1, &humidity1 ) == true )
  {
    Serial.print( "T1 = " );
    Serial.print( temperature1, 1 );
    Serial.print( " deg. C, H1 = " );
    Serial.print( humidity1, 1 );
    Serial.println( "%" );
  }
    
  if( measure_environment2( &temperature2, &humidity2 ) == true )
  {
    Serial.print( "T2 = " );
    Serial.print( temperature2, 1 );
    Serial.print( " deg. C, H2 = " );
    Serial.print( humidity2, 1 );
    Serial.println( "%" );
  }
  
  if( measure_environment3( &temperature3, &humidity3 ) == true )
  {
    Serial.print( "T3 = " );
    Serial.print( temperature3, 1 );
    Serial.print( " deg. C, H3 = " );
    Serial.print( humidity3, 1 );
    Serial.println( "%" );
  }
  
  if( measure_environment4( &temperature4, &humidity4 ) == true )
  {
    Serial.print( "T4 = " );
    Serial.print( temperature4, 1 );
    Serial.print( " deg. C, H4 = " );
    Serial.print( humidity4, 1 );
    Serial.println( "%" );
  }
}

Sure, just use structs and pointers.

ok, thx.