Combining code for multiple sensors in a single program I need to operate 3 different sensors (taken 6 sensors of each type) by Arduino Mega2560 :- 


DHT11 (temperature-Humidity sensor)
MQ5 (gas sensor) and
Soil Moisture sensor
{taken 6 of each type (total=18).} 


I have managed to run each one of them separately and I don't know how to manipulate the code so they could work all together. My background in coding is basic, so it would be great help if someone can help me for combining these codes.

Here are the sketches:

MQ5 Code:

int sensor=7;
int gas_value;
void setup()
{

pinMode(sensor,INPUT);
Serial.begin(9600);

}

void loop()
{

gas_value=digitalRead(sensor);
Serial.println(gas_value);
}


{other digital pins to be used: D6,D5,D4,D3,D2}

DHT11 Code:

#include &lt;dht.h&gt;
#include &lt;dht.h&gt;
#include "dht.h"
#define dht_apin A0 // Analog Pin sensor is connected to
dht DHT;

void setup(){

  Serial.begin(9600);
  delay(500);//Delay to let system boot
  Serial.println("DHT11 Humidity &amp; temperature Sensor\n\n");
  delay(1000);//Wait before accessing Sensor

}//end "setup()"

void loop(){
  //Start of Program 

    DHT.read11(dht_apin);

    Serial.print("Current humidity = ");
    Serial.print(DHT.humidity);
    Serial.print("%  ");
    Serial.print("temperature = ");
    Serial.print(DHT.temperature); 
    Serial.println("C  ");

    delay(5000);//Wait 5 seconds before accessing sensor again.

  //Fastest should be once every two seconds.

}// end loop() 


{analog pins for other sensors: A1,A2,A3,A4,A5}

Soil_Moisture Sensor Code:

int sensor_pin = A6;
int output_value ;

void setup() {

   Serial.begin(9600);
   Serial.println("Reading From the Sensor ...");
   delay(2000);

   }

void loop() {

   output_value= analogRead(sensor_pin);
   output_value = map(output_value,550,0,0,100);
   Serial.print("Mositure : ");
   Serial.print(output_value);
   Serial.println("%");
   delay(1000);
   }


{Analog pins for other sensors to be used: A6,A7,A8,A9,A10}
In general:

All #defines and #includes get put from all separate sketches, at the top.

Globals and function definitions (except for setup() and loop()) get mixed together below the #defines and #includes.

All code in the separate setup()s get put in the combined setup().

All code in the separate loop()s get put in the combined loop().

You have to be aware that any loop() from the separate programs that include a delay() will likely have to be refactored to use the same kind of scheduled execution method as the Blink Without Delay example from Arduino. delay() stops the whole program while it twiddles thumbs, which means your other sensors aren't being read or managed during that time.