0

I need to operate 3 different sensors (taken 6 sensors of each type) by Arduino Mega2560 :-

  1. DHT11 (temperature-Humidity sensor)
  2. MQ5 (gas sensor) and
  3. 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 <dht.h>
#include <dht.h>
#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 & 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}

closed as too broad by gre_gor, sempaiscuba, Juraj, MatsK, per1234 Jan 31 at 10:29

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    your finished program has to have setup() and loop() blocks ..... what do you feel that should happen with the codes in the three setup() blocks? ..... what do you think that should happen with the code in the three loop() blocks? – jsotola Jan 28 at 21:07
0

All things before void setup() put before one setup() function. All things in void setup() put to the one void setup() - do not duplicate things like #include or Serial.begin(). And things from loop() function put to the loop() function. It's a good practice to don't use delay() in loop() when you are processing more things than one. Instead of delay() use millis() to get current uptime and after that compare this uptime to last uptime of check.

I modified your code, so read it and try it.

#include <dht.h>

#define dht_apin A0 // Analog Pin sensor is connected to
dht DHT;


int sensor=7;
int gas_value;

int sensor_pin = A6;
int output_value ;


unsigned long gas_last_time = 0;
unsigned long dht11_last_time = 0;
unsigned long analog_last_time = 0;



void setup(){
  Serial.begin(9600);
  pinMode(sensor,INPUT);
}

void loop(){
  if(millis() - gas_last_time >= 10){
    gas_last_time = millis();

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


  if(millis() - dht11_last_time >= 5000){
    dht11_last_time = millis();

    DHT.read11(dht_apin);

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


  if(millis() - analog_last_time >= 1000){
    analog_last_time = millis();

    output_value= analogRead(sensor_pin);
    output_value = map(output_value,550,0,0,100);
    Serial.print("Mositure : ");
    Serial.print(output_value);
    Serial.println("%");
  }
}
  • Thank you so much for your precious help, specially in letting me know the basic things you told me via your code. A lot of thanks to you, because of all help by all you guys, I completed my code.(i'm sorry for replying too late from the day of completion of code) – Deepanshu Feb 4 at 20:20
  • @Deepanshu No problem. :) – Matej Feb 4 at 20:59
2

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.

  • Thank you so much for your precious help. Because of all help by all you guys, I completed my code.(i'm sorry for replying too late from the day of completion of code) – Deepanshu Feb 4 at 20:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.