Hi Guys I need Help for my Project Air Quality monitoring

Hi Guys. I need some help with our codes.

we are creating a air quality monitoring system w/ features of LCD, GPS, SD card.

Our sensors are:

  1. Particle Sensor (PM-10) - Dust Sensor (GP2Y1010AU0F)

  2. AFE Board with 3 Gas Sensors (NO2,CO,SO2) of AlphaSense

PROBLEMS:

  1. The Serial Monitor output and the LCD to be synchronize in 1 sec every read. What is happening is that the time in the GPS is not 1 second per output and it is duplicating. While the LCD is blinking fast output that is not readable. We want the Serial monitor and LCD to be outputting a 1 data per second.

Our Output:

3:47:40 29 3 2013

3:47:40 29 3 2013

3:47:42 29 3 2013

3:47:42 29 3 2013

3:47:44 29 3 2013

3:47:44 29 3 2013

Wanted Example Output:

3:47:40 29 3 2013

3:47:41 29 3 2013

3:47:42 29 3 2013

3:47:43 29 3 2013

3:47:44 29 3 2013

  1. We want to know also how to code a User_defined_time. What I mean is that the User must have the capability that he can input how many hours he wants the specific sensor to be gathering data.

THANK YOU FOR YOUR HELP GUYS. MUCH APPRECIATED. :smiley:

THE CODES WILL BE COMMENTED.

OUR CODES with the AlphaSense are still separated because we are still examining it.

This the code:

#include <TinyGPS++.h>  //GPS Library
#include <Time.h>       //GPS
#include <TimeLib.h>    //GPS
#include <SD.h>
#include <SPI.h>
#include <SoftwareSerial.h>       //GPS        
#include <LiquidCrystal.h>

//PM Code
int dustPin = 0; //Connect dust sensor to Arduino A0 pin
int ledPower = 5; //Connect 3 led driver pins of dust sensor to Arduino D2

int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;
float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;

//SD Code
/*Choose two Arduino pins to use for software serial
 The GPS Shield uses D2 and D3 by default when in DLINE mode*/
int chipSelect = 4; //Set chipSelect=4
File AQ_GPSTemp_Data; //variable for working with our file object

//LCD Code
// initialize the library with the numbers of the interface pins 12-RS 11-E 9-6 - DPins
LiquidCrystal LCD(31, 30, 25, 24, 23, 22);

//GPS Code 
//initialization for GPS
static const int RXPin = 2; 
static const int TXPin = 3; 

//Temp Code
//initialization for TempSensor
int val;                    
int temPin = 1;              
float mv;
float cel;

// The Skytaq EM-506 GPS module included in the GPS Shield Kit
static const uint32_t GPSBaud = 9600;    //Set GPS Baud Rate
const int UTC_offset = 8;                // to correct the GPS UT time to the Phil. Local Time           
int Year;
byte Month;
byte Day;
byte Hour;
byte Minute;
byte Second;

// Create a TinyGPS++ object called "gps"
TinyGPSPlus gps;

// Create a software serial port called "gpsSerial"
SoftwareSerial gpsSerial(RXPin, TXPin);

//initialization for SD


void setup()
{
  Serial.begin(9600);
  gpsSerial.begin(GPSBaud);     //GPS Serial.begin
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized
  if (!SD.begin(chipSelect)){
    Serial.println("Card failed, or not present");
    //dont do anything
    return;
  }

  Serial.println("card initialized."); 

  pinMode(ledPower,OUTPUT); //PM output 
  pinMode(10, OUTPUT); //Reserve pin 10 as an output
  SD.begin(4); //initialize the SD card with chipSelectect connected in pin 4

    // set up the LCD's number of columns and rows: 
  LCD.begin(16, 2);

}

//MAIN LOOP
void loop()
{
  // This sketch displays information every time a new sentence is correctly encoded.
  while (gpsSerial.available() > 0)        //enabling the GPS Module
    if (gps.encode(gpsSerial.read()))
      displayInfo();                       //Provide the corresponding readings from the different sensors

  /* If 5000 milliseconds pass and there are no characters coming in
   over the software serial port, show a "No GPS detected" error*/
  if (millis() > 5000 && gps.charsProcessed() <10)
  {
    Serial.println(F("No GPS detected"));
    while(true);
  }
}

void displayInfo()    //Display Readings from sensors
{
  GPSLocation();      //GPS latitude and Longitude
  GPSDate();          //GPS Date in MM/DD/YY format
  GPSTime();          //GPS Phil. time in HH:MM:SS format
  TempSensor();       //Read Temperature Sensor
  PMSensor();         //Read PM Sensor

  /*LCD.setCursor(0,0); //Set LCD Cursor to upper left corner, column 0, row 0
  LCD.print("TEMP:");
  LCD.print(cel,4); 
  delay(250);

  LCD.setCursor(0,1); //Set LCD Cursor to upper left corner, column 0, row 0
  LCD.print("PM: ");
  LCD.print(dustDensity,4);
  delay(250);
  LCD.clear();
*/
  AQ_GPSTemp_Data= SD.open("GPSData.txt", FILE_WRITE);  //Open AirQuality_GPSandTEMP_Data.text on the SD card 
  //as a file to write the readings
  if (AQ_GPSTemp_Data){  
    AQ_GPSTemp_Data.print(F("Location: "));
    AQ_GPSTemp_Data.print(gps.location.lat(), 6);            //latitude
    AQ_GPSTemp_Data.print(F(",")); 
    AQ_GPSTemp_Data.print(gps.location.lng(), 6);            //longitude
    AQ_GPSTemp_Data.print(F("  Date/Time: "));
    AQ_GPSTemp_Data.print(gps.date.month());                 //month
    AQ_GPSTemp_Data.print(F("/"));
    AQ_GPSTemp_Data.print(gps.date.day());                   //day
    AQ_GPSTemp_Data.print(F("/"));
    AQ_GPSTemp_Data.print(gps.date.year());                  //year
    AQ_GPSTemp_Data.print(F(" "));
    AQ_GPSTemp_Data.print(Hour);                             //hour
    AQ_GPSTemp_Data.print(F(":"));
    AQ_GPSTemp_Data.print(Minute);                           //minute
    AQ_GPSTemp_Data.print(F(":"));
    AQ_GPSTemp_Data.print(Second);                           //second
    AQ_GPSTemp_Data.print(" TEMPERATURE=");
    AQ_GPSTemp_Data.print(cel);                              //temp in degree celsius
    AQ_GPSTemp_Data.print("*C");
    AQ_GPSTemp_Data.print(" - Voltage: ");
    AQ_GPSTemp_Data.print(calcVoltage);
    AQ_GPSTemp_Data.print(" - Dust Density: ");
    AQ_GPSTemp_Data.println(dustDensity); // unit: mg/m3
    AQ_GPSTemp_Data.close(); //close the file
  }
  Serial.println();
  //delay(10);
}

//Functions in the [displayInfo()].
void GPSLocation()                          //Location of the GPS
{
  Serial.print(F("Location: "));
  if (gps.location.isValid())              //checks if there is any valid information for the location 
  {
    Serial.print(gps.location.lat(), 6);    // Latitude
    Serial.print(F(","));
    Serial.print(gps.location.lng(), 6);    // Longitude
  }
  else
  {
    Serial.print(F("INVALID"));             // no valid info
  }
}

void GPSDate()                             //Current Date
{
  Serial.print(F("  Date/Time: "));
  if (gps.date.isValid())                  //checks if there is any valid information for the date
  {
    Serial.print(gps.date.month());        //month
    Serial.print(F("/"));
    Serial.print(gps.date.day());          //day
    Serial.print(F("/"));
    Serial.print(gps.date.year());         //year
  }
  else
  {
    Serial.print(F("INVALID"));    //no valid info
  }
}

void GPSTime()                             //Current Time
{
  Serial.print(F(" "));
  if (gps.time.isValid())                  //checks if there is any valid information for the time
  {
    Year = gps.date.year();
    Month = gps.date.month();
    Day = gps.date.day();
    Hour = gps.time.hour();
    Minute = gps.time.minute();
    Second = gps.time.second();

    setTime(Hour,Minute,Second,Day,Month,Year);      // Set Time from GPS data string
    adjustTime(UTC_offset * SECS_PER_HOUR);         //Calculate current Time Zone time by offset value


    Year = year();
    Month = month();
    Day = day();
    Hour = hour();
    Minute = minute();
    Second = second();

    if (Hour < 10) Serial.print(F("0"));
    Serial.print(Hour);                            //hour
    Serial.print(F(":"));
    if (Minute < 10) Serial.print(F("0"));
    Serial.print(Minute);                          //minute
    Serial.print(F(":"));
    if (Second < 10) Serial.print(F("0"));
    Serial.print(Second);
    Serial.print(F(" "));
  }
  else
  {
    Serial.print(F("INVALID"));    //no valid info
  }
}

void TempSensor()                                  //ambient temperature
{   
  val = analogRead(temPin);
  delay(10);
  mv = (val/1024.0)*5000;
  cel = mv/10;
  Serial.print(F(" TEMPERATURE = "));            
  Serial.print(cel);                             //temperature value in degree Celcius                         
  Serial.print(F("*C"));
  LCD.setCursor(0,0); //Set LCD Cursor to upper left corner, column 0, row 0
  LCD.print("TEMP:");
  LCD.print(cel,4); 
  delay(100);
  //delay(10);

}

void PMSensor()
{
  digitalWrite(ledPower, LOW); // power on the LED
  delayMicroseconds(samplingTime);

  voMeasured = analogRead(dustPin); // read the dust value
  delay(10);
  delayMicroseconds(deltaTime);
  digitalWrite(ledPower, HIGH); // turn the LED off
  delayMicroseconds(sleepTime);

  // 0 - 5V mapped to 0 - 1023 integer values
  // recover voltage
  calcVoltage = voMeasured * (5.0 / 1024.0);

  // linear eqaution taken from http://www.howmuchsnow.com/arduino/airquality/
  // Chris Nafis (c) 2012
  dustDensity = 0.17 * calcVoltage - 0.1;

  /*Serial.print("Raw Signal Value (0-1023): ");
   Serial.print(voMeasured);
   
   Serial.print(" - Voltage: ");
   Serial.print(calcVoltage);*/

  Serial.print(" - Dust Density: ");
  Serial.println(dustDensity,4); // unit: mg/m3
  LCD.setCursor(0,1); //Set LCD Cursor to upper left corner, column 0, row 0
  LCD.print("PM: ");
  LCD.print(dustDensity,4);
  //LCD.clear();
  delay(100);
  //delay(10);

}

This is the code for the AlphaSensors:

int SN1_WE;
int SN1_Aux;
int SN2_WE;
int SN2_Aux;
int SN3_WE;
int SN3_Aux;
int Volt_PT;
int WE1Pin = 2;
int Aux1Pin = 3;
int WE2Pin = 4;
int Aux2Pin = 5;
int WE3Pin = 6;
int Aux3Pin = 7;
int PTPin = 1;

// formulas
float Temp_PT;        // PT1000
float SN1_WE_Real;    // NO2 WE
float SN1_Aux_Real;   // NO2 Aux
float NO2;            // NO2 ppb
float SN2_WE_Real;    // CO WE
float SN2_Aux_Real;   // CO Aux
float CO;             // CO ppb
float SN3_WE_Real;    // SO2 WE
float SN3_Aux_Real;   // SO2 Aux
float SO2;            // SO2 ppb

//Calibration Constants
// for SN1 or NO2 
float SN1_TOTAL_WE = 0.1720;
float SN1_TOTAL_AE = 0.2240;
float SN1_SENSITIVITY = 0.2080;
// for SN2 or CO
float SN2_TOTAL_WE = 0.2880;
float SN2_TOTAL_AE = 0.2560;
float SN2_SENSITIVITY = 0.2630;
//for SN3 or SO2
float SN3_TOTAL_WE = 0.2630;
float SN3_TOTAL_AE = 0.2770;
float SN3_SENSITIVITY = 0.2970;

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

void loop()
{
  SN1_WE = analogRead(WE1Pin);
  delay(10);
  SN1_Aux = analogRead(Aux1Pin);
  delay(10);
  SN2_WE = analogRead(WE2Pin);
  delay(10);
  SN2_Aux = analogRead(Aux2Pin);
  delay(10);
  SN3_WE = analogRead(WE3Pin);
  delay(10);
  SN3_Aux = analogRead(Aux3Pin);
  delay(10);
  Volt_PT = analogRead(PTPin);
  
  Temp_PT = 20 + ((Volt_PT - (0.297)*(0.3000))/(0.0010));
  Serial.print("Temperature = ");
  Serial.print(Temp_PT);
  Serial.println();
  
  if(Temp_PT <= 10)
  {
    SN1_WE_Real = SN1_WE - SN1_TOTAL_WE;
    SN1_Aux_Real = 1.090*(SN1_Aux - SN1_TOTAL_AE);
    NO2 = (SN1_WE_Real - SN1_Aux_Real)/(SN1_SENSITIVITY);
    Serial.print("NO2 Voltage = ");
    Serial.print(SN1_WE);
    Serial.print(" V");
    Serial.print(" ");
    Serial.print(SN1_Aux);
    Serial.print(" V");
    Serial.print(" - NO2 CONCENTRATION = ");
    Serial.print(NO2);
    Serial.print(" ppb");
    Serial.println();
   
    SN2_WE_Real = SN2_WE - SN2_TOTAL_WE;
    SN2_Aux_Real = 1.000*(SN2_Aux - SN2_TOTAL_AE);
    CO = (SN2_WE_Real - SN2_Aux_Real)/(SN2_SENSITIVITY);
    Serial.print("CO Voltage = ");
    Serial.print(SN2_WE);
    Serial.print(" V");
    Serial.print(" ");
    Serial.print(SN2_Aux);
    Serial.print(" V");
    Serial.print(" - CO CONCENTRATION = ");
    Serial.print(CO);
    Serial.print(" ppb");
    Serial.println();

    SN3_WE_Real = SN3_WE - SN3_TOTAL_WE;
    SN3_Aux_Real = 1.1500*(SN3_Aux - SN3_TOTAL_AE);
    SO2 = (SN3_WE_Real - SN3_Aux_Real)/(SN3_SENSITIVITY);
    Serial.print("SO2 Voltage = ");
    Serial.print(SN3_WE);
    Serial.print(" V");
    Serial.print(" ");
    Serial.print(SN3_Aux);
    Serial.print(" V");
    Serial.print(" - SO2 CONCENTRATION = ");
    Serial.print(SO2);
    Serial.print(" ppb");
    Serial.println();
  }
  
  if(Temp_PT >= 30)
  {
    SN1_WE_Real = SN1_WE - SN1_TOTAL_WE;
    SN1_Aux_Real = 3.000*(SN1_Aux - SN1_TOTAL_AE);
    NO2 = (SN1_WE_Real - SN1_Aux_Real)/(SN1_SENSITIVITY);
    Serial.print("NO2 Voltage = ");
    Serial.print(SN1_WE);
    Serial.print(" V");
    Serial.print(" ");
    Serial.print(SN1_Aux);
    Serial.print(" V");
    Serial.print(" - NO2 CONCENTRATION = ");
    Serial.print(NO2);
    Serial.print(" ppb");
    Serial.println();
   
    SN2_WE_Real = SN2_WE - SN2_TOTAL_WE;
    SN2_Aux_Real = -0.7600*(SN2_Aux - SN2_TOTAL_AE);
    CO = (SN2_WE_Real - SN2_Aux_Real)/(SN2_SENSITIVITY);
    Serial.print("CO Voltage = ");
    Serial.print(SN2_WE);
    Serial.print(" V");
    Serial.print(" ");
    Serial.print(SN2_Aux);
    Serial.print(" V");
    Serial.print(" - CO CONCENTRATION = ");
    Serial.print(CO);
    Serial.print(" ppb");
    Serial.println();

    SN3_WE_Real = SN3_WE - SN3_TOTAL_WE;
    SN3_Aux_Real = 3.9300*(SN3_Aux - SN3_TOTAL_AE);
    SO2 = (SN3_WE_Real - SN3_Aux_Real)/(SN3_SENSITIVITY);
    Serial.print("SO2 Voltage = ");
    Serial.print(SN3_WE);
    Serial.print(" V");
    Serial.print(" ");
    Serial.print(SN3_Aux);
    Serial.print(" V");
    Serial.print(" - SO2 CONCENTRATION = ");
    Serial.print(SO2);
    Serial.print(" ppb");
    Serial.println();
  }
  
  else
  {
    SN1_WE_Real = SN1_WE - SN1_TOTAL_WE;
    SN1_Aux_Real = 1.3500*(SN1_Aux - SN1_TOTAL_AE);
    NO2 = (SN1_WE_Real - SN1_Aux_Real)/(SN1_SENSITIVITY);
    Serial.print("NO2 Voltage = ");
    Serial.print(SN1_WE);
    Serial.print(" V");
    Serial.print(" ");
    Serial.print(SN1_Aux);
    Serial.print(" V");
    Serial.print(" - NO2 CONCENTRATION = ");
    Serial.print(NO2);
    Serial.print(" ppb");
    Serial.println();
   
    SN2_WE_Real = SN2_WE - SN2_TOTAL_WE;
    SN2_Aux_Real = -1.000*(SN2_Aux - SN2_TOTAL_AE);
    CO = (SN2_WE_Real - SN2_Aux_Real)/(SN2_SENSITIVITY);
    Serial.print("CO Voltage = ");
    Serial.print(SN2_WE);
    Serial.print(" V");
    Serial.print(" ");
    Serial.print(SN2_Aux);
    Serial.print(" V");
    Serial.print(" - CO CONCENTRATION = ");
    Serial.print(CO);
    Serial.print(" ppb");
    Serial.println();

    SN3_WE_Real = SN3_WE - SN3_TOTAL_WE;
    SN3_Aux_Real = 1.8200*(SN3_Aux - SN3_TOTAL_AE);
    SO2 = (SN3_WE_Real - SN3_Aux_Real)/(SN3_SENSITIVITY);
    Serial.print("SO2 Voltage = ");
    Serial.print(SN3_WE);
    Serial.print(" V");
    Serial.print(" ");
    Serial.print(SN3_Aux);
    Serial.print(" V");
    Serial.print(" - SO2 CONCENTRATION = ");
    Serial.print(SO2);
    Serial.print(" ppb");
    Serial.println();
  }
  delay(1000);
}

Does the GPS module even produce a data string every second?

Your code outputs the same data twice, or every 2nd second. Your code seems to be doing multiple delays of hundreds of miliseconds which could sum up to something larger than a second. So that might be the cause of the 2-second skip. If you mean to reduce the update rate of the LCD, then only execute the LCD update functions when the underlying data has changed. So read the GPS serial port and decode it as often as possible, but only print to the LCD IF the seconds variable or other important data has changed.

Also do realize that sending data to the serial port at 9600 Baud means it can only handle 960 characters per second. There are so many serial.print statements in that code that it may attribute to the delays. [EDIT: some of it is commented out. I didn’t see that immediately]

Probably unrelated to the stated problem, but noteworthy to fix anyway:

if (millis() > 5000 && gps.charsProcessed() <10)
```In which sequence is this supposed to be processed? Does it give the required result, or is there a potential logic error developing here? Pay attention to logic operator precedence here, or force it by using round brackets.