Getting bad readings when using 2 sensors

Hello-

I am trying to use two different sensors on one Mega 2560. The sensors:
*K-Series CO2 meter from CO2 meter.com
*ENV-TMP-D Temperature sensor from Atlas Scientific

Basically, i have tried combining the example sketch of each sensor so as to have them both work on one board. Unfortunately what happens is that when i get one to work, the other either gives incorrect readings and vice versa.

I have a suspicion the problem lies somewhere in a few places: baud rate set within the library (for some reason i cannot edit the library), interrupts within the code itself (i have NO idea how to use these), trying to force this to use softserial, user error.

Before i go further, as far as the above issues that i think it might be, i am still pretty new to programing arduinos and i am an art student, not a programmer, so i apologize in advance if this seems like an simple and easily overlooked issue on my part. Anyway, here’s the code:

(i have commented some stuff out but included it here just in case it might be relevant)

#include "kSeries.h"
//include kSeries Library
kSeries K_30(12,13); //Initialize a kSeries Sensor with pin 12 as Rx and 13 (2) as Tx

const int led = 6;   // Analog output pin that the LED is attached to

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

//#include <SoftwareSerial.h>      //we have to include the SoftwareSerial library, or else we can't use it.  
//#define rx 2                     //define what pin rx is going to be.
//#define tx 3                     //define what pin Tx is going to be.

SoftwareSerial myserial(10, 11); //define how the soft serial port is going to work. 

char tmp_data[20];                //we make a 20 byte character array to hold incoming data from the ENV-TMP-D. 
char computerdata[20];            //we make a 20 byte character array to hold incoming data from a pc/mac/other.
byte pc_debug=1;                  //if you would like to debug the ENV-TMP-D through the serial monitor(pc/mac/other). if not set this to 0. 
byte received_from_computer=0;     //we need to know how many characters have been received.                                 
byte received_from_sensor=0;       //we need to know how many characters have been received.
byte arduino_only=1;               //if you would like to operate the ENV-TMP-D with the arduino only and not use the serial monitor to send it commands set this to 1. The data will still come out on the serial monitor, so you can see it working.  
byte startup=1;                   //used to make sure the arduino takes over control of the ENV-TMP-D properly.
float float_tmp=0;                 //used to hold a floating point number that is the ENV-TMP-D. 
byte string_received=0;            //used to identify when we have received a string from the ENV-TMP-D  .





void setup()
{
  Serial.begin(38400); //start a serial port to communicate with the computer
  Serial.begin(38400);
  //Serial.println("Serial Up!");
    pinMode(6, OUTPUT);
     myserial.begin(38400);     //enable the software serial port
      }

void serialEvent(){           //this interrupt will trigger when the data coming from the serial monitor(pc/mac/other) is received.    
        if(pc_debug==0){       //if pc debug is set to 0 this function will be bypassed.  
           received_from_computer=Serial.readBytesUntil(13,computerdata,20); //we read the data sent from the serial monitor(pc/mac/other) until we see a <CR>. We also count how many characters have been received.      
           computerdata[received_from_computer]=0; //we add a 0 to the spot in the array just after the last character we recived. This will stop us from transmiting incorrect data that may have been left in the buffer. 
           myserial.print(computerdata);          //we transmit the data received from the serial monitor(pc/mac/other) through the soft serial port to the ENV-TMP-D  . 
           myserial.print('\r');                  //all data sent to the ENV-TMP-D must end with a <CR>.  
          }    
        }
 
 void loop()
{
  double co2 = K_30.getCO2('p'); //returns co2 value in ppm ('p') or percent ('%')
  Serial.print("Co2 ppm = ");
  Serial.println(co2); //print value
  delay(1500); //wait 1.5 seconds

  //sensorValue = analogRead(co2);            

  //outputValue = co2;  
  if (float_tmp > 10.00) {
    // change the analog out value:
  digitalWrite(6, HIGH);   // turn the LED on (HIGH is the voltage level)

  }
  else {
      digitalWrite(6, LOW);   // turn the LED on (HIGH is the voltage level)

}


{ 
    
  if(myserial.available() > 0){                                           //if we see that the ENV-TMP-D has sent a character.
     received_from_sensor=myserial.readBytesUntil(13,tmp_data,20); //we read the data sent from ENV-TMP-D untill we see a <CR>. We also count how many character have been recived.  
     tmp_data[received_from_sensor]=0; //we add a 0 to the spot in the array just after the last character we recived. This will stop us from transmiting incorrect data that may have been left in the buffer. 
     string_received=1;                //a flag used when the arduino is controlling the ENV-TMP-D to let us know that a complete string has been received.
     Serial.println(tmp_data);         //lets transmit that data received to the serial monitor.
     }    
  
  if(arduino_only==1){               //if you set arduino_only to = 1.  
     if (startup==1){                //if the arduino just booted up, we need to set some things up first.  
         pc_debug=1;                 //make sure pc_debug is set to 0. You will no longer be able to write commands to the ENV-TMP-D   in the serial monitor.  
          myserial.print("e\r");     //take the ENV-TMP-D out of continues mode. 
          delay(50);                 //on start up sometimes the first command is missed. 
          myserial.print("e\r");     //so, let’s send it twice.
          delay(50);                 //a short delay after the ENV-TMP-D was taken out of continues mode is used to make sure we don’t over load it with commands.
          startup=1;                 //startup is completed, let's not do this again during normal operation. 
      }
 
    
  delay(800);                         //we will take a reading ever 800ms. You can make this longer or shorter if you like.
   myserial.print("R\r");             //send it the command to take a single reading.
   if(string_received==1){            //did we get data back from the ENV-TMP-D?
     float_tmp=atof(tmp_data);        //many people ask us "how do I convert a sting into a float?" this is how...(pretty tough) 
     if(float_tmp>=25){Serial.println("high\r");} //This is the proof that it has been converted into a string.
     if(float_tmp<25){Serial.println("low\r");}  //This is the proof that it has been converted into a string.
     string_received=0;}              //reset the string received flag.
  }   

} 
}

Why are you using SoftwareSerial on a Mega when it has 4 HardwareSerial ports?

...R

That's a great question. One of the example codes seems to require it. I commented it out for one because the library "#include "kSeries.h" makes it redundant and gives compiling errors. Hmmmm. Kinda clueless on how i would code for the hardware serial ports. I suppose that is my next research task. That said, was there anything other than that that stuck out to you that might be my problem?

SoftwareSerial does not work nearly as well as HardwareSerial, it can also conflict with other stuff. I didn't think about it any further.

Modify the code (and the choice of Rx Tx pins) to use Hardware Serial and see what happens. If you have two sensors that both use serial connect them to different serial ports.

...R

What you say makes good sense to me. Unfortunately i really havent been able to find much resourses as to how to use Hardware serial. (Art student here). Is there any examples of code using it? Do you need to define the pin as you would with the soft serial? Is there any websites you might suggest on learning more? Heck, might you even show me a basic example so that i could kinda see what im looking for? Thanks a million for all the help already!

Unfortunately i really havent been able to find much resourses as to how to use Hardware serial.

Try http://arduino.cc/en/reference/serial

For using the other serial I/O on the Mega simply use Serial1. in place of Serial. or Serial2. or Serial3.

Grumpy_Mike -

Thanks. Ok, if you dont mind me asking, but would the code for Serial1. look something like this?

#include "kSeries.h"

//include kSeries Library

kSeries K_30(18,19); //Initialize a kSeries Sensor with pin 18 as Rx and 19(2) as Tx

since the page you shared kinda implies that? Would i still need to include the library (it seems to be a variant of the SoftSerial library)? Or would i even assign the pins? Is there any example code out there on how to write code for hardware serial? (Im a bit of a visual learner)

Thank you both for your patience! You guys rock for pointing me in these good directions!

zara2355: kSeries K_30(18,19); //Initialize a kSeries Sensor with pin 18 as Rx and 19(2) as Tx

This is the right idea. Edit to add ... I think @Grumpy_Mike is correct in Reply #8. You probably need to look at the code inside the library.

I don't know which order the pins should be specified - you will need to look at the Library instructions for that. On my Mega pin 18 is Tx1 and pin 19 is Rx1.

However it is usual to connect the Tx pin on the sensor to the Rx pin on the Arduino etc.

...R

would the code for Serial1. look something like this?

No that looks like a software serial command to me.

You can not specify the pins of the hardware serial ports they are fixed. So giving you the option to specify them seems odd.

The code for Serial port 1 would look exactly like code for the normal serial port only with a number 1 after the word Serial.