0

I'm in a project and I need to put multiple grove sensors in arduino and I've done with the others, but when I put the code of MQ3 sensor all the sensors stop showing in the serial monitor. I'm going to send the codes. OBS: the mq3 sensor alone it's just fine it only stop working when I put his code with others. I need to put the 2 codes in just one and they have to show the values in monitor serial. Thanks.

All the sensores without MQ3

//TODOS OS SENSORES 

//O2 SENSOR
//HCHO SENSOR
//DUST SENSOR
//BAROMETER 280 SENSOR
//CO2 SENSOR




//02 SENSOR
const float VRefer = 3.3;       // voltage of adc reference

const int pinAdc   = A0;




//HCHO SENSOR
#include <math.h>
#define Vc 4.95
//the number of R0 you detected just now
#define R0 34.28




//DUST SENSOR
int pin = 6;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 30000;//sampe 30s ;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;




//BAROMETER SENSOR
#include "Seeed_BMP280.h"
#include <Wire.h>

BMP280 bmp280;




//CO2 SENSOR
#include <SoftwareSerial.h>
SoftwareSerial s_serial(8, 9);      // TX, RX

#define sensor s_serial

const unsigned char cmd_get_sensor[] =
{
    0xff, 0x01, 0x86, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x79
};

unsigned char dataRevice[9];
int temperature;
int CO2PPM;





void setup() {




//O2 SENSOR
    Serial.begin(9600);
    Serial.println("Grove - Gas Sensor Test Code...");




//HCHO SENSOR
    Serial.begin(9600);




//DUST SENSOR
    Serial.begin(9600);
    pinMode(pin,INPUT);
    starttime = millis();//get the current time;




//BAROMETER SENSOR 
  Serial.begin(9600);
  if(!bmp280.init()) {
    Serial.println("Device not connected or broken!");
  }




//CO2 SENSOR
    sensor.begin(9600);
    Serial.begin(9600);
    Serial.println("get a 'g', begin to read from sensor!");
    Serial.println("********************************************************");
    Serial.println();
}

void loop() {




//O2 SENSOR
float Vout =0;
    Serial.print("Vout =");

    Vout = readO2Vout();
    Serial.print(Vout);
    Serial.print(" V, Concentration of O2 is ");
    Serial.println(readConcentration());
    delay(500);




//HCHO SENSOR
    int sensorValue=analogRead(A1);
    double Rs=(1023.0/sensorValue)-1;
    Serial.print("Rs = ");
    Serial.println(Rs);
    double ppm=pow(10.0,((log10(Rs/R0)-0.0827)/(-0.4807)));
    Serial.print("HCHO ppm = ");
    Serial.println(ppm);
    delay(1000);




//DUST SENSOR
    duration = pulseIn(pin, LOW);
    lowpulseoccupancy = lowpulseoccupancy+duration;

    if ((millis()-starttime) > sampletime_ms)//if the sampel time == 30s
    {
        ratio = lowpulseoccupancy/(sampletime_ms*10.0);  // Integer percentage 0=>100
        concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve
        Serial.print(lowpulseoccupancy);
        Serial.print(",");
        Serial.print(ratio);
        Serial.print(",");
        Serial.println(concentration);
        lowpulseoccupancy = 0;
        starttime = millis();
     }




//BAROMETER SENSOR
  float pressure;

  //get and print temperatures
  Serial.print("Temp: ");
  Serial.print(bmp280.getTemperature());
  Serial.println("C"); // The unit for  Celsius because original arduino don't support speical symbols

  //get and print atmospheric pressure data
  Serial.print("Pressure: ");
  Serial.print(pressure = bmp280.getPressure());
  Serial.println("Pa");

  //get and print altitude data
  Serial.print("Altitude: ");
  Serial.print(bmp280.calcAltitude(pressure));
  Serial.println("m");

  Serial.println("\n");//add a line between output of different times.

  delay(1000);




//CO2 SENSOR
    if(dataRecieve())
    {
        Serial.print("Temperature: ");
        Serial.print(temperature);
        Serial.print("  CO2: ");
        Serial.print(CO2PPM);
        Serial.println("");
    }
    delay(1000);





}




//O2 SENSOR
float readO2Vout()
{
    long sum = 0;
    for(int i=0; i<32; i++)
    {
        sum += analogRead(pinAdc);
    }

    sum >>= 5;

    float MeasuredVout = sum * (VRefer / 1023.0);
    return MeasuredVout;
}

float readConcentration()
{
    // Vout samples are with reference to 3.3V
    float MeasuredVout = readO2Vout();

    //float Concentration = FmultiMap(MeasuredVout, VoutArray,O2ConArray, 6);
    //when its output voltage is 2.0V,
    float Concentration = MeasuredVout * 0.21 / 2.0;
    float Concentration_Percentage=Concentration*100;
    return Concentration_Percentage;
}




//CO2 SENSOR
bool dataRecieve(void)
{
    byte data[9];
    int i = 0;

    //transmit command data
    for(i=0; i<sizeof(cmd_get_sensor); i++)
    {
        sensor.write(cmd_get_sensor[i]);
    }
    delay(10);
    //begin reveiceing data
    if(sensor.available())
    {
        while(sensor.available())
        {
            for(int i=0;i<9; i++)
            {
                data[i] = sensor.read();
            }
        }
    }

    for(int j=0; j<9; j++)
    {
        Serial.print(data[j]);
        Serial.print(" ");
    }
    Serial.println("");

    if((i != 9) || (1 + (0xFF ^ (byte)(data[1] + data[2] + data[3] + data[4] + data[5] + data[6] + data[7]))) != data[8])
    {
        return false;
    }

    CO2PPM = (int)data[2] * 256 + (int)data[3];
    temperature = (int)data[4] - 40;

    return true;
}

//
//
//
//
//
//

now just mq3 sensor(it's working alone)

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

void loop() {

    float sensor_volt;
    float RS_gas; // Get value of RS in a GAS
    float ratio; // Get ratio RS_GAS/RS_air
    int sensorValue = analogRead(A2);
    sensor_volt=(float)sensorValue/1024*5.0;
    RS_gas = (5.0-sensor_volt)/sensor_volt; // omit *RL

    /*-Replace the name "R0" with the value of R0 in the demo of First Test -*/
    ratio = RS_gas/0.06;  // ratio = RS/R0
    /*-----------------------------------------------------------------------*/

    Serial.print("sensor_volt = ");
    Serial.println(sensor_volt);
    Serial.print("RS_ratio = ");
    Serial.println(RS_gas);
    Serial.print("Rs/R0 = ");
    Serial.println(ratio);

    Serial.print("\n\n");

    delay(1000);

}
  • Using only Upper Case letters is considered as SHOUTING! Please correct it. – MatsK Sep 19 '19 at 5:10
  • why are you repeating the Serial.begin(9600);? – jsotola Sep 19 '19 at 14:22
  • I'm not very experient with Arduino, I just copy and paste the code of each sensor and put them together. I'm going to remove. – Keyvisson Macena Sep 19 '19 at 22:18

Browse other questions tagged or ask your own question.