Multiplexer Problem

Hi everyone I really need your help. Alright I've attach a image below:

As everyone can see from the Serial Monitor , There's actually 2 sensor working together at the same time using one serial port and I'm trying to get the data properly from each sensor but it work fine for the first few data that it's been transmitted in after a while all the data get jumble up.
At first , I thought it was the delay but I try from 0.5 sec to 3 second nothing changes..... Thus I'm not really sure what I can do to solve this problem. Can anyone advice me on this problem?

perhaps if you post the code you are using...

You can only have one thing attached at each end of the serial connection.

Thanks Michinyong and BulldogLowell for replying.

Okay I’ve sort of solve the problem and right now I’m facing another probelm
as per mention by BulldogLowell Here’s my code:
The Device I’m using is : Arduino Uno, Multiplexer 4052 and also 2 serial devices

//This softserial library Automatically sets TX as pin 9 and RX as pin 8.




#include <AltSoftSerial.h>          //Include the software serial library  
AltSoftSerial altSerial;            //Name the software serial library altSerial (this cannot be omitted)  

int s0 = 7;                         //Arduino pin 7 to control pin S0
int s1 = 6;                         //Arduino pin 6 to control pin S1

char computerdata[20];               //A 20 byte character array to hold incoming data from a pc/mac/other 
char sensordata[30];                 //A 30 byte character array to hold incoming data from the sensors
byte computer_bytes_received=0;      //We need to know how many characters bytes have been received         
byte sensor_bytes_received=0;        //We need to know how many characters bytes have been received
boolean datacomplete = false;

char *channel;                       //Char pointer used in string parsing
char *cmd;                           //Char pointer used in string parsing



void setup() {
  pinMode(s1, OUTPUT);              //Set the digital pin as output.
  pinMode(s0, OUTPUT);              //Set the digital pin as output.
  Serial.begin(38400);              //Set the hardware serial port to 38400
  altSerial.begin(38400);           //Set the soft serial port to 38400
  channel = "2";
  open_channel();
  altSerial.print("f\r");
}




void serialEvent(){               //This interrupt will trigger when the data coming from the serial monitor(pc/mac/other) is received   
  computer_bytes_received=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[computer_bytes_received]=0; //We add a 0 to the spot in the array just after the last character we received.. This will stop us from transmitting incorrect data that may have been left in the buffer
}    


void loop(){

  if(computer_bytes_received!=0){                 //If computer_bytes_received does not equal zero  
    channel=strtok(computerdata, ":");            //Let's parse the string at each colon
    cmd=strtok(NULL, ":");                        //Let's parse the string at each colon 
    open_channel();                               //Call the function "open_channel" to open the correct data path
    altSerial.print(cmd);                         //Send the command from the computer to the Atlas Scientific device using the softserial port 
    altSerial.print("\r");                        //After we send the command we send a carriage return <CR> 
    computer_bytes_received=0;                    //Reset the var computer_bytes_received to equal 0 
    datacomplete= true;
  }

  if(datacomplete)
  {  
    if(altSerial.available() > 0){                   //If data has been transmitted from an Atlas Scientific device
      sensor_bytes_received=altSerial.readBytesUntil(13,sensordata,30); //we read the data sent from the Atlas Scientific device until we see a <CR>. We also count how many character have been received 
      sensordata[sensor_bytes_received]=0;           //we add a 0 to the spot in the array just after the last character we received. This will stop us from transmitting incorrect data that may have been left in the buffer
       Serial.println(sensordata);                    //let’s transmit the data received from the Atlas Scientific device to the serial monitor  
      Serial.print("Channel:'");
      Serial.print(channel);
      Serial.println("'");
      char *FirstValue = strtok (sensordata ,",");
      Serial.println(FirstValue);
      char *SecondValue =strtok(NULL,",");
      Serial.println(SecondValue);
      char *ThirdValue = strtok(NULL,",");
      Serial.println(ThirdValue);
      char *FourthValue = strtok(NULL, ",");
      Serial.println(FourthValue);
    }
   datacomplete = false; 
    delay(1500); 
  }
  channel = "0";
  open_channel();
  delay(500);
  altSerial.print("r\r");

  if(altSerial.available()>0)
  {
    sensor_bytes_received=altSerial.readBytesUntil(13,sensordata,30); //we read the data sent from the Atlas Scientific device until we see a <CR>. We also count how many character have been received 
    sensordata[sensor_bytes_received]=0;           //we add a 0 to the spot in the array just after the last character we received. This will stop us from transmitting incorrect data that may have been left in the buffer
    char *TotalVolume = strtok(sensordata ,",");
    Serial.print("Make Up Total Volume:'");
    Serial.print(TotalVolume);
    Serial.println("'");
    char *LiterPerHour = strtok(NULL, ",");
    Serial.print("Make up Liter per hour :'");
    Serial.print(LiterPerHour);
    Serial.println("'");
    char *LiterPerMinute = strtok(NULL,",");
    Serial.print("Make up Liter per min :'");
    Serial.print(LiterPerMinute);
    Serial.println("'");

  }
  delay(2000); 
  channel = "1";
  open_channel();
  altSerial.print("r\r");
  if(altSerial.available()>0)
  {
    sensor_bytes_received=altSerial.readBytesUntil(13,sensordata,30); //we read the data sent from the Atlas Scientific device until we see a <CR>. We also count how many character have been received 
    sensordata[sensor_bytes_received]=0;           //we add a 0 to the spot in the array just after the last character we received. This will stop us from transmitting incorrect data that may have been left in the buffer

    char *TotalVolume2 = strtok(sensordata ,",");
    Serial.print("Blow Off Total Volume:'");
    Serial.print(TotalVolume2);
    Serial.println("'");
    char *LiterPerHour2 = strtok(NULL, ",");
    Serial.print("Blow off Liter per hour :'");
    Serial.print(LiterPerHour2);
    Serial.println("'");
    char *LiterPerMinute2 = strtok(NULL,",");
    Serial.print("Blow off Liter per min :'");
    Serial.print(LiterPerMinute2);
    Serial.println("'");
    delay(1000);

  }
  delay(2000);
 
/*
   channel = "2";
   open_channel();
   altSerial.print("R\r");
   if(altSerial.available()>0)
   {
   sensor_bytes_received=altSerial.readBytesUntil(13,sensordata,30); //we read the data sent from the Atlas Scientific device until we see a <CR>. We also count how many character have been received 
   sensordata[sensor_bytes_received]=0;           //we add a 0 to the spot in the array just after the last character we received. This will stop us from transmitting incorrect data that may have been left in the buffer
   
   char *phValue=strtok(sensordata,",");
   float ph = atof(phValue);
   Serial.print("Ph value :'");
   Serial.print(ph);
   Serial.println("'");
   
   if(ph>=7.5){
   Serial.println("high\r");
   } //This is the proof that it has been converted into a float.
   if(ph<7.5){
   Serial.println("low\r");
   }   //This is the proof that it has been converted into a float.
   delay(1000);
   
   }

*/
}

void open_channel(){                                  //This function controls what UART port is opened. 

  switch (*channel) {                              //Looking to see what channel to open   

  case '0':                                      //If channel==0 then we open channel 0     
    digitalWrite(s0, LOW);                       //S0 and S1 control what channel opens 
    digitalWrite(s1, LOW);                       //S0 and S1 control what channel opens  
    break;                                         //Exit switch case

  case '1':
    digitalWrite(s0, HIGH);
    digitalWrite(s1, LOW);
    break;

  case '2':
    digitalWrite(s0, LOW);
    digitalWrite(s1, HIGH);
    break;

  case '3':
    digitalWrite(s0, HIGH);
    digitalWrite(s1, HIGH); 
    break;
  }
}

Here’s what I see on my Serial Monitor

Here the part 2 of the reply I post previously,

Okay so the above one is just a demo of a multiplexer data logging 2 serial devices using one serial port at the same time.

Right now I want to data log even more so I add another serial device and my code is like this

//This softserial library Automatically sets TX as pin 9 and RX as pin 8.




#include <AltSoftSerial.h>          //Include the software serial library  
AltSoftSerial altSerial;            //Name the software serial library altSerial (this cannot be omitted)  

int s0 = 7;                         //Arduino pin 7 to control pin S0
int s1 = 6;                         //Arduino pin 6 to control pin S1

char computerdata[20];               //A 20 byte character array to hold incoming data from a pc/mac/other 
char sensordata[30];                 //A 30 byte character array to hold incoming data from the sensors
byte computer_bytes_received=0;      //We need to know how many characters bytes have been received         
byte sensor_bytes_received=0;        //We need to know how many characters bytes have been received
boolean datacomplete = false;

char *channel;                       //Char pointer used in string parsing
char *cmd;                           //Char pointer used in string parsing



void setup() {
  pinMode(s1, OUTPUT);              //Set the digital pin as output.
  pinMode(s0, OUTPUT);              //Set the digital pin as output.
  Serial.begin(38400);              //Set the hardware serial port to 38400
  altSerial.begin(38400);           //Set the soft serial port to 38400
  channel = "2";
  open_channel();
  altSerial.print("f\r");
}




void serialEvent(){               //This interrupt will trigger when the data coming from the serial monitor(pc/mac/other) is received   
  computer_bytes_received=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[computer_bytes_received]=0; //We add a 0 to the spot in the array just after the last character we received.. This will stop us from transmitting incorrect data that may have been left in the buffer
}    


void loop(){

  if(computer_bytes_received!=0){                 //If computer_bytes_received does not equal zero  
    channel=strtok(computerdata, ":");            //Let's parse the string at each colon
    cmd=strtok(NULL, ":");                        //Let's parse the string at each colon 
    open_channel();                               //Call the function "open_channel" to open the correct data path
    altSerial.print(cmd);                         //Send the command from the computer to the Atlas Scientific device using the softserial port 
    altSerial.print("\r");                        //After we send the command we send a carriage return <CR> 
    computer_bytes_received=0;                    //Reset the var computer_bytes_received to equal 0 
    datacomplete= true;
  }

  if(datacomplete)
  {  
    if(altSerial.available() > 0){                   //If data has been transmitted from an Atlas Scientific device
      sensor_bytes_received=altSerial.readBytesUntil(13,sensordata,30); //we read the data sent from the Atlas Scientific device until we see a <CR>. We also count how many character have been received 
      sensordata[sensor_bytes_received]=0;           //we add a 0 to the spot in the array just after the last character we received. This will stop us from transmitting incorrect data that may have been left in the buffer
       Serial.println(sensordata);                    //let’s transmit the data received from the Atlas Scientific device to the serial monitor  
      Serial.print("Channel:'");
      Serial.print(channel);
      Serial.println("'");
      char *FirstValue = strtok (sensordata ,",");
      Serial.println(FirstValue);
      char *SecondValue =strtok(NULL,",");
      Serial.println(SecondValue);
      char *ThirdValue = strtok(NULL,",");
      Serial.println(ThirdValue);
      char *FourthValue = strtok(NULL, ",");
      Serial.println(FourthValue);
    }
   datacomplete = false; 
    delay(1500); 
  }
  channel = "0";
  open_channel();
  delay(500);
  altSerial.print("r\r");

  if(altSerial.available()>0)
  {
    sensor_bytes_received=altSerial.readBytesUntil(13,sensordata,30); //we read the data sent from the Atlas Scientific device until we see a <CR>. We also count how many character have been received 
    sensordata[sensor_bytes_received]=0;           //we add a 0 to the spot in the array just after the last character we received. This will stop us from transmitting incorrect data that may have been left in the buffer
    char *TotalVolume = strtok(sensordata ,",");
    Serial.print("Make Up Total Volume:'");
    Serial.print(TotalVolume);
    Serial.println("'");
    char *LiterPerHour = strtok(NULL, ",");
    Serial.print("Make up Liter per hour :'");
    Serial.print(LiterPerHour);
    Serial.println("'");
    char *LiterPerMinute = strtok(NULL,",");
    Serial.print("Make up Liter per min :'");
    Serial.print(LiterPerMinute);
    Serial.println("'");

  }
  delay(2000); 
  channel = "1";
  open_channel();
  altSerial.print("r\r");
  if(altSerial.available()>0)
  {
    sensor_bytes_received=altSerial.readBytesUntil(13,sensordata,30); //we read the data sent from the Atlas Scientific device until we see a <CR>. We also count how many character have been received 
    sensordata[sensor_bytes_received]=0;           //we add a 0 to the spot in the array just after the last character we received. This will stop us from transmitting incorrect data that may have been left in the buffer

    char *TotalVolume2 = strtok(sensordata ,",");
    Serial.print("Blow Off Total Volume:'");
    Serial.print(TotalVolume2);
    Serial.println("'");
    char *LiterPerHour2 = strtok(NULL, ",");
    Serial.print("Blow off Liter per hour :'");
    Serial.print(LiterPerHour2);
    Serial.println("'");
    char *LiterPerMinute2 = strtok(NULL,",");
    Serial.print("Blow off Liter per min :'");
    Serial.print(LiterPerMinute2);
    Serial.println("'");
    delay(1000);

  }
  delay(2000);
 
   channel = "2";
   open_channel();
   altSerial.print("R\r");
   if(altSerial.available()>0)
   {
   sensor_bytes_received=altSerial.readBytesUntil(13,sensordata,30); //we read the data sent from the Atlas Scientific device until we see a <CR>. We also count how many character have been received 
   sensordata[sensor_bytes_received]=0;           //we add a 0 to the spot in the array just after the last character we received. This will stop us from transmitting incorrect data that may have been left in the buffer
   
   char *phValue=strtok(sensordata,",");
   float ph = atof(phValue);
   Serial.print("Ph value :'");
   Serial.print(ph);
   Serial.println("'");
   
   if(ph>=7.5){
   Serial.println("high\r");
   } //This is the proof that it has been converted into a float.
   if(ph<7.5){
   Serial.println("low\r");
   }   //This is the proof that it has been converted into a float.
   delay(1000);
   
   }


}

void open_channel(){                                  //This function controls what UART port is opened. 

  switch (*channel) {                              //Looking to see what channel to open   

  case '0':                                      //If channel==0 then we open channel 0     
    digitalWrite(s0, LOW);                       //S0 and S1 control what channel opens 
    digitalWrite(s1, LOW);                       //S0 and S1 control what channel opens  
    break;                                         //Exit switch case

  case '1':
    digitalWrite(s0, HIGH);
    digitalWrite(s1, LOW);
    break;

  case '2':
    digitalWrite(s0, LOW);
    digitalWrite(s1, HIGH);
    break;

  case '3':
    digitalWrite(s0, HIGH);
    digitalWrite(s1, HIGH); 
    break;
  }
}

and here’s what I see on the serial monitor

Here's part 3 of my reply

As you observe on the image , Blow off serial device on 2 pins of the multiplexer is working fine and the Makeup serial device is gone on the serial monitor for the first 2 cycle then it appear later on but the value is wrong .
This is due to the addition serial device I code in. But The value of the ph serial device appear on the Make Up Total Volume which mean it's doing the right task my the number are all allocated wrongly. On the other hand , I'm not sure where the ph value came from. Could anyone explain to me what's going on.

At first I thought it was the delay, but that wasn't the case as I try from 0.5 sec delay to 5 sec delay and the serial monitor doesn't change.

Thank you for reading this long post Much appreciated ^^ Looking forward to any suggestion given. Thanks!