Nested Loops for Serial Communication

I'm making an arduino project that reads in some values from some Atlas Scientific http://atlas-scientific.com/product_pages/kits/ec-kit.html probes. There is example code for how to communicate with only one device using the serial monitor, and I'm trying to port that to 'select' which probe I want to talk to using the serial monitor. The problem is that the example code just waits for serial input in the loop(). It looks like this:

void loop(){                                                                   //here we go....

  if (input_stringcomplete){                                                   //if a string from the PC has been recived in its entierty 
      Serial2.print(inputstring);                                              //send that string to the Atlas Scientific product
      inputstring = "";                                                        //clear the string:
      input_stringcomplete = false;                                            //reset the flage used to tell if we have recived a completed string from the PC
      }

 if (sensor_stringcomplete){                                                   //if a string from the Atlas Scientific product has been recived in its entierty 
      Serial.println(sensorstring);                                            //send that string to to the PC's serial monitor
      sensorstring = "";                                                       //clear the string:
      sensor_stringcomplete = false;                                           //reset the flage used to tell if we have recived a completed string from the Atlas Scientific product
      }
 }

So basically, you type in a command and hit enter, it sends the command to the probe. The probe does its magic, and reports a value back to you. Pretty straightforward. Because I have two probes which use this same communication method, I'm trying to use case statements to select which probe I want to talk to (there's also a temperature sensor that doesn't use this protocol). I have a Mega right now so I don't think the issue is with SoftwareSerial not being able to talk to two things at once. I'm trying to use a while loop inside the case statements to talk to my peripherals, and I'm getting zero functionality out of it.

void loop(){                                                                  
void loop(){                                                                   //here we go....
// read the selection
 if (Serial.available()>0) {
   int inByte = Serial.read();
     switch(inByte) {
       case '1':{
      //read pH
      Serial.print("pH");
      while (inputstring != "break"){
      if (input_stringcomplete){                                               //if a string from the PC has been recived in its entierty 
      Serial3.print(inputstring);                                              //send that string to the Atlas Scientific product
      Serial.print(inputstring);                                               //debug
      inputstring = "";                                                        //clear the string:
      input_stringcomplete = false;                                            //reset the flage used to tell if we have recived a completed string from the PC
        }

 if (sensor_stringcomplete){                                                   //if a string from the Atlas Scientific product has been recived in its entierty 
      Serial.println(sensorstring);                                            //send that string to to the PC's serial monitor
      sensorstring = "";                                                       //clear the string:
      sensor_stringcomplete = false;                                           //reset the flage used to tell if we have recived a completed string from the Atlas Scientific product
        }
      }
      break;
     }
      case '2':{
      //read EC
      Serial.print("EC");
      while (inputstring != "break"){
      if (input_stringcomplete){                                               //if a string from the PC has been recived in its entierty 
      Serial2.print(inputstring);                                              //send that string to the Atlas Scientific product
      Serial.print(inputstring);                                               //debug
      inputstring = "";                                                        //clear the string:
      input_stringcomplete = false;                                            //reset the flage used to tell if we have recived a completed string from the PC
      }

 if (sensor_stringcomplete){                                                   //if a string from the Atlas Scientific product has been recived in its entierty 
      Serial.println(sensorstring);                                            //send that string to to the PC's serial monitor
      sensorstring = "";                                                       //clear the string:
      sensor_stringcomplete = false;                                           //reset the flage used to tell if we have recived a completed string from the Atlas Scientific product
        }
      }
      break;
     }
    .
    .
    .
    }

Like I said, I'm trying to use the while loops to run the individual terminal serial communication with the individual sensors, and be able to type "break" (or any command really) into the terminal to go back to the case statements and select another sensor. The while loop idea isn't working at all. I added debug lines inside the while loops to echo my commands, and it's not even doing that. What's the correct way of doing this?
I'll post the full program in the comments for reference. Thanks for any help!

Full program for reference. I had to install the OneWire and DallasTemperature libraries for my temperature sensor.

#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into pin 30 on the Arduino
#define ONE_WIRE_BUS 32
#define TEMPERATURE_PRECISION 9
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
// arrays to hold device address
DeviceAddress insideThermometer;

String inputstring = "";                                                       //a string to hold incoming data from the PC
String sensorstring = "";                                                      //a string to hold the data from the Atlas Scientific product
boolean input_stringcomplete = false;                                          //have we received all the data from the PC
boolean sensor_stringcomplete = false;                                         //have we received all the data from the Atlas Scientific product

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress;

void setup(void){                                                                //set up the hardware
    Serial.begin(9600);                                                       //set baud rate for the hardware serial port_0 to 38400
    Serial3.begin(38400);                                                     //set baud rate for software serial port_3 to 38400
    Serial2.begin(38400);                                                     //set baud rate for software serial port_3 to 38400
    inputstring.reserve(5);                                                   //set aside some bytes for receiving data from the PC
    sensorstring.reserve(30);                                                 //set aside some bytes for receiving data from Atlas Scientific produc
    // locate devices on the bus
    Serial.print("Locating temperature devices...");
    sensors.begin();
    // Grab a count of devices on the wire
    numberOfDevices = sensors.getDeviceCount();
    Serial.print("Found ");
    Serial.print(sensors.getDeviceCount(), DEC);
    Serial.println(" devices.");
     for(int i=0;i<numberOfDevices; i++)
  {
    if(sensors.getAddress(tempDeviceAddress, i))
	{
        sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
        }
  }   
    Serial.println("Type 1 to read pH, 2 to read EC, or 3 to read temperature.");
  }
//for reading pH/EC
void serialEvent() {                                                         //if the hardware serial port_0 receives a char              
               char inchar = (char)Serial.read();                               //get the char we just received
               inputstring += inchar;                                           //add it to the inputString
               if(inchar == '\r') {input_stringcomplete = true;}                //if the incoming character is a <CR>, set the flag
               input_stringcomplete = true;
              }  
void serialEvent3(){                                                         //if the hardware serial port_3 receives a char 
              char inchar = (char)Serial3.read();                              //get the char we just received
              sensorstring += inchar;                                          //add it to the inputString
              if(inchar == '\r') {sensor_stringcomplete = true;}               //if the incoming character is a <CR>, set the flag 
             }
void serialEvent2(){                                                         //if the hardware serial port_3 receives a char 
              char inchar = (char)Serial2.read();                              //get the char we just received
              sensorstring += inchar;                                          //add it to the inputString
              if(inchar == '\r') {sensor_stringcomplete = true;}               //if the incoming character is a <CR>, set the flag 
             }
             
//for reading temp
void printTemperature(DeviceAddress deviceAddress)
{
  // method 2 - faster
  float tempC = sensors.getTempC(deviceAddress);
  Serial.print("Temp C: ");
  Serial.print(tempC);
  Serial.print(" Temp F: ");
  Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
}
void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}



void loop(){                                                                   //here we go....
// read the selection
 int r = 0; 
 if (Serial.available()>0) {
   int inByte = Serial.read();
     switch(inByte) {
       case '1':{
      //read pH
      Serial.print("pH");
      while (inputstring != "break"){
      if (input_stringcomplete){                                               //if a string from the PC has been recived in its entierty 
      Serial3.print(inputstring);                                              //send that string to the Atlas Scientific product
      Serial.print(inputstring);                                               //debug
      inputstring = "";                                                        //clear the string:
      input_stringcomplete = false;                                            //reset the flage used to tell if we have recived a completed string from the PC
        }

 if (sensor_stringcomplete){                                                   //if a string from the Atlas Scientific product has been recived in its entierty 
      Serial.println(sensorstring);                                            //send that string to to the PC's serial monitor
      sensorstring = "";                                                       //clear the string:
      sensor_stringcomplete = false;                                           //reset the flage used to tell if we have recived a completed string from the Atlas Scientific product
        }
      }
      break;
     }
      case '2':{
      //read EC
      Serial.print("EC");
      while (inputstring != "break"){
      if (input_stringcomplete){                                               //if a string from the PC has been recived in its entierty 
      Serial2.print(inputstring);                                              //send that string to the Atlas Scientific product
      Serial.print(inputstring);                                               //debug
      inputstring = "";                                                        //clear the string:
      input_stringcomplete = false;                                            //reset the flage used to tell if we have recived a completed string from the PC
      }

 if (sensor_stringcomplete){                                                   //if a string from the Atlas Scientific product has been recived in its entierty 
      Serial.println(sensorstring);                                            //send that string to to the PC's serial monitor
      sensorstring = "";                                                       //clear the string:
      sensor_stringcomplete = false;                                           //reset the flage used to tell if we have recived a completed string from the Atlas Scientific product
        }
      }
      break;
     }
   
      case '3':
      // call sensors.requestTemperatures() to issue a global temperature 
      // request to all devices on the bus
      Serial.print("Requesting temperatures...");
      sensors.requestTemperatures(); // Send the command to get temperatures
      Serial.println("DONE");
      // Loop through each device, print out temperature data
      for(int i=0;i<numberOfDevices; i++)
      {
        // Search the wire for address
        if(sensors.getAddress(tempDeviceAddress, i))
	    {
		// Output the device ID
		Serial.print("Temperature for device: ");
		Serial.println(i,DEC);
		
		// It responds almost immediately. Let's print out the data
		printTemperature(tempDeviceAddress); // Use a simple function to print out the data
	    } 
	//else ghost device! Check your power requirements and cabling
	
        }
      break;
    }
   }
}

mtomovich:

   while (inputstring != "break"){

if (input_stringcomplete){     //if a string from the PC has been recived in its entierty
     Serial3.print(inputstring);     //send that string to the Atlas Scientific product
     Serial.print(inputstring);       //debug
     inputstring = "";                    //clear the string:
     input_stringcomplete = false;      
      //reset the flage used to tell if we have recived a completed string from the PC
     }
  }

Where, in that while loop, are you checking inputstring to see if it has changed to "break".
All I can see is you setting it to a null string, which is also !="break". The while loop will never finish.

void serialEvent() {                                                         //if the hardware serial port_0 receives a char              
  char inchar = (char)Serial.read();                               //get the char we just received
  inputstring += inchar;                                           //add it to the inputString
  if(inchar == '\r') {
    input_stringcomplete = true;
  }                //if the incoming character is a <CR>, set the flag
  input_stringcomplete = true;
}  

...

void loop(){                                                                   //here we go....
  // read the selection
  int r = 0; 
  if (Serial.available()>0) {
    int inByte = Serial.read();
    switch(inByte) {
...

Why are you reading from Serial both in loop and serialEvent?

Henry,
What I thought i was doing was setting inputstring using SerialEvent()

void serialEvent() {                                                         //if the hardware serial port_0 receives a char              
               char inchar = (char)Serial.read();                               //get the char we just received
               inputstring += inchar;                                           //add it to the inputString
               if(inchar == '\r') {input_stringcomplete = true;}                //if the incoming character is a <CR>, set the flag
               input_stringcomplete = true;
              }

And that the while loop would run until inputstring was set to “break” (that is, the while statement was checking to see if inputstring was set to “break”). Is that not the case? Thanks for the reply.

Nick,
Perhaps this is just bad form or me not knowing the optimal way to do this, but I’m using SerialEvent in the code I took from the supplied example, and I’m reading from serial in loop() specifically to use the case statements. Here’s the example program:

String inputstring = "";                                                       //a string to hold incoming data from the PC
String sensorstring = "";                                                      //a string to hold the data from the Atlas Scientific product
boolean input_stringcomplete = false;                                          //have we received all the data from the PC
boolean sensor_stringcomplete = false;                                         //have we received all the data from the Atlas Scientific product


  void setup(){                                                                //set up the hardware
     Serial.begin(38400);                                                      //set baud rate for the hardware serial port_0 to 38400
     Serial3.begin(38400);                                                     //set baud rate for software serial port_3 to 38400
     inputstring.reserve(5);                                                   //set aside some bytes for receiving data from the PC
     sensorstring.reserve(30);                                                 //set aside some bytes for receiving data from Atlas Scientific product
     }
 
 
 
   void serialEvent() {                                                         //if the hardware serial port_0 receives a char              
               char inchar = (char)Serial.read();                               //get the char we just received
               inputstring += inchar;                                           //add it to the inputString
               if(inchar == '\r') {input_stringcomplete = true;}                //if the incoming character is a <CR>, set the flag
              }  


  void serialEvent3(){                                                         //if the hardware serial port_3 receives a char 
              char inchar = (char)Serial3.read();                              //get the char we just received
              sensorstring += inchar;                                          //add it to the inputString
              if(inchar == '\r') {sensor_stringcomplete = true;}               //if the incoming character is a <CR>, set the flag 
             }



 void loop(){                                                                   //here we go....
     
  if (input_stringcomplete){                                                   //if a string from the PC has been recived in its entierty 
      Serial3.print(inputstring);                                              //send that string to the Atlas Scientific product
      inputstring = "";                                                        //clear the string:
      input_stringcomplete = false;                                            //reset the flage used to tell if we have recived a completed string from the PC
      }

 if (sensor_stringcomplete){                                                   //if a string from the Atlas Scientific product has been recived in its entierty 
      Serial.println(sensorstring);                                            //send that string to to the PC's serial monitor
      sensorstring = "";                                                       //clear the string:
      sensor_stringcomplete = false;                                           //reset the flage used to tell if we have recived a completed string from the Atlas Scientific product
      }
 }

I’m basically trying to take that and ‘select’ which probe I want to talk inside loop() and using Serial.read like in the code snippet you posted.

void loop(){                                                                   //here we go....
  // read the selection
  int r = 0; 
  if (Serial.available()>0) {
    int inByte = Serial.read();
    switch(inByte) {
...

Is this incorrect?

This is different to the code above, right? Can you post your whole sketch now? BTW I wouldn't be using String myself.

Please note that in versions of the IDE up to and including 1.0.3, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.), as described here for example.

Alternatively, install the fix described here: Fixing String Crashes

Preferably upgrade your IDE to version 1.0.4 or above at: http://arduino.cc/en/Main/Software

This may help with decoding incoming text:

Here’s the full code. I’ll work through the links you showed me and report back in a bit.

#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into pin 30 on the Arduino
#define ONE_WIRE_BUS 32
#define TEMPERATURE_PRECISION 9
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
// arrays to hold device address
DeviceAddress insideThermometer;

String inputstring = "";                                                       //a string to hold incoming data from the PC
String sensorstring = "";                                                      //a string to hold the data from the Atlas Scientific product
boolean input_stringcomplete = false;                                          //have we received all the data from the PC
boolean sensor_stringcomplete = false;                                         //have we received all the data from the Atlas Scientific product

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress;

void setup(void){                                                                //set up the hardware
    Serial.begin(9600);                                                       //set baud rate for the hardware serial port_0 to 38400
    Serial3.begin(38400);                                                     //set baud rate for software serial port_3 to 38400
    Serial2.begin(38400);                                                     //set baud rate for software serial port_3 to 38400
    inputstring.reserve(5);                                                   //set aside some bytes for receiving data from the PC
    sensorstring.reserve(30);                                                 //set aside some bytes for receiving data from Atlas Scientific produc
    // locate devices on the bus
    Serial.print("Locating temperature devices...");
    sensors.begin();
    // Grab a count of devices on the wire
    numberOfDevices = sensors.getDeviceCount();
    Serial.print("Found ");
    Serial.print(sensors.getDeviceCount(), DEC);
    Serial.println(" devices.");
     for(int i=0;i<numberOfDevices; i++)
  {
    if(sensors.getAddress(tempDeviceAddress, i))
	{
        sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
        }
  }   
    Serial.println("Type 1 to read pH, 2 to read EC, or 3 to read temperature.");
  }
//for reading pH/EC
void serialEvent() {                                                         //if the hardware serial port_0 receives a char              
               char inchar = (char)Serial.read();                               //get the char we just received
               inputstring += inchar;                                           //add it to the inputString
               if(inchar == '\r') {input_stringcomplete = true;}                //if the incoming character is a <CR>, set the flag
               input_stringcomplete = true;
              }  
void serialEvent3(){                                                         //if the hardware serial port_3 receives a char 
              char inchar = (char)Serial3.read();                              //get the char we just received
              sensorstring += inchar;                                          //add it to the inputString
              if(inchar == '\r') {sensor_stringcomplete = true;}               //if the incoming character is a <CR>, set the flag 
             }
void serialEvent2(){                                                         //if the hardware serial port_3 receives a char 
              char inchar = (char)Serial2.read();                              //get the char we just received
              sensorstring += inchar;                                          //add it to the inputString
              if(inchar == '\r') {sensor_stringcomplete = true;}               //if the incoming character is a <CR>, set the flag 
             }
             
//for reading temp
void printTemperature(DeviceAddress deviceAddress)
{
  // method 2 - faster
  float tempC = sensors.getTempC(deviceAddress);
  Serial.print("Temp C: ");
  Serial.print(tempC);
  Serial.print(" Temp F: ");
  Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
}
void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}



void loop(){                                                                   //here we go....
// read the selection
 int r = 0; 
 if (Serial.available()>0) {
   int inByte = Serial.read();
     switch(inByte) {
       case '1':{
      //read pH
      Serial.print("pH");
      while (inputstring != "break"){
      if (input_stringcomplete){                                               //if a string from the PC has been recived in its entierty 
      Serial3.print(inputstring);                                              //send that string to the Atlas Scientific product
      Serial.print(inputstring);                                               //debug
      inputstring = "";                                                        //clear the string:
      input_stringcomplete = false;                                            //reset the flage used to tell if we have recived a completed string from the PC
        }

 if (sensor_stringcomplete){                                                   //if a string from the Atlas Scientific product has been recived in its entierty 
      Serial.println(sensorstring);                                            //send that string to to the PC's serial monitor
      sensorstring = "";                                                       //clear the string:
      sensor_stringcomplete = false;                                           //reset the flage used to tell if we have recived a completed string from the Atlas Scientific product
        }
      }
      break;
     }
      case '2':{
      //read EC
      Serial.print("EC");
      while (inputstring != "break"){
      if (input_stringcomplete){                                               //if a string from the PC has been recived in its entierty 
      Serial2.print(inputstring);                                              //send that string to the Atlas Scientific product
      Serial.print(inputstring);                                               //debug
      inputstring = "";                                                        //clear the string:
      input_stringcomplete = false;                                            //reset the flage used to tell if we have recived a completed string from the PC
      }

 if (sensor_stringcomplete){                                                   //if a string from the Atlas Scientific product has been recived in its entierty 
      Serial.println(sensorstring);                                            //send that string to to the PC's serial monitor
      sensorstring = "";                                                       //clear the string:
      sensor_stringcomplete = false;                                           //reset the flage used to tell if we have recived a completed string from the Atlas Scientific product
        }
      }
      break;
     }
   
      case '3':
      // call sensors.requestTemperatures() to issue a global temperature 
      // request to all devices on the bus
      Serial.print("Requesting temperatures...");
      sensors.requestTemperatures(); // Send the command to get temperatures
      Serial.println("DONE");
      // Loop through each device, print out temperature data
      for(int i=0;i<numberOfDevices; i++)
      {
        // Search the wire for address
        if(sensors.getAddress(tempDeviceAddress, i))
	    {
		// Output the device ID
		Serial.print("Temperature for device: ");
		Serial.println(i,DEC);
		
		// It responds almost immediately. Let's print out the data
		printTemperature(tempDeviceAddress); // Use a simple function to print out the data
	    } 
	//else ghost device! Check your power requirements and cabling
	
        }
      break;
    }
   }
}

FYI I have been using 1.0.5

Nick, this is an easier to read version of the full code. I took out all the onewire stuff for the temperature sensor so I can solely focus on debugging this specific issue. I press ‘1’ and the switch statement takes me to the correct case (it prints out ‘pH’), but then I’m not getting any serial communication inside the while loop.

String inputstring = "";                                                       //a string to hold incoming data from the PC
String sensorstring = "";                                                      //a string to hold the data from the Atlas Scientific product
boolean input_stringcomplete = false;                                          //have we received all the data from the PC
boolean sensor_stringcomplete = false;                                         //have we received all the data from the Atlas Scientific product


  void setup(){                                                                //set up the hardware
     Serial.begin(38400);                                                      //set baud rate for the hardware serial port_0 to 38400
     Serial2.begin(38400);                                                     //set baud rate for software serial port_2 to 38400
     Serial3.begin(38400);                                                     //set baud rate for software serial port_3 to 38400
     inputstring.reserve(5);                                                   //set aside some bytes for receiving data from the PC
     sensorstring.reserve(30);                                                 //set aside some bytes for receiving data from Atlas Scientific product
     Serial.println("Type 1 to read pH or 2 to read EC.");  
     }
 
 
 
   void serialEvent() {                                                         //if the hardware serial port_0 receives a char              
               char inchar = (char)Serial.read();                               //get the char we just received
               inputstring += inchar;                                           //add it to the inputString
               if(inchar == '\r') {input_stringcomplete = true;}                //if the incoming character is a <CR>, set the flag
              }  
  void serialEvent2(){                                                         //if the hardware serial port_3 receives a char 
              char inchar = (char)Serial2.read();                              //get the char we just received
              sensorstring += inchar;                                          //add it to the inputString
              if(inchar == '\r') {sensor_stringcomplete = true;}               //if the incoming character is a <CR>, set the flag 
             }
  void serialEvent3(){                                                         //if the hardware serial port_3 receives a char 
              char inchar = (char)Serial3.read();                              //get the char we just received
              sensorstring += inchar;                                          //add it to the inputString
              if(inchar == '\r') {sensor_stringcomplete = true;}               //if the incoming character is a <CR>, set the flag 
             }



 void loop(){                                                                   //here we go....
 if (Serial.available()>0) {
  int inByte = Serial.read();
  switch(inByte) {
    case '1':{
      Serial.println("pH");
      while (inputstring != "break") {
        if (input_stringcomplete){                                                   //if a string from the PC has been recived in its entierty 
          Serial3.print(inputstring);                                              //send that string to the Atlas Scientific product
          inputstring = "";                                                        //clear the string:
          input_stringcomplete = false;                                            //reset the flage used to tell if we have recived a completed string from the PC
          }

        if (sensor_stringcomplete){                                                   //if a string from the Atlas Scientific product has been recived in its entierty 
        Serial.println(sensorstring);                                            //send that string to to the PC's serial monitor
        sensorstring = "";                                                       //clear the string:
        sensor_stringcomplete = false;                                           //reset the flage used to tell if we have recived a completed string from the Atlas Scientific product
        }
     }
     break;
    }
    
    case '2':{
      Serial.println("EC");
      while (inputstring != "break") {
        if (input_stringcomplete){                                                   //if a string from the PC has been recived in its entierty 
          Serial2.print(inputstring);                                              //send that string to the Atlas Scientific product
          inputstring = "";                                                        //clear the string:
          input_stringcomplete = false;                                            //reset the flage used to tell if we have recived a completed string from the PC
          }

        if (sensor_stringcomplete){                                                   //if a string from the Atlas Scientific product has been recived in its entierty 
        Serial.println(sensorstring);                                            //send that string to to the PC's serial monitor
        sensorstring = "";                                                       //clear the string:
        sensor_stringcomplete = false;                                           //reset the flage used to tell if we have recived a completed string from the Atlas Scientific product
        }
     }
     break;
    }
   }
  }
 }

Everything still works in the original program without the while loops and case statements where I’m only talking to one sensor. Here’s the code for that loop() again:

void loop(){                                                                   //here we go....

  if (input_stringcomplete){                                                   //if a string from the PC has been recived in its entierty 
      Serial2.print(inputstring);                                              //send that string to the Atlas Scientific product
      inputstring = "";                                                        //clear the string:
      input_stringcomplete = false;                                            //reset the flage used to tell if we have recived a completed string from the PC
      }

 if (sensor_stringcomplete){                                                   //if a string from the Atlas Scientific product has been recived in its entierty 
      Serial.println(sensorstring);                                            //send that string to to the PC's serial monitor
      sensorstring = "";                                                       //clear the string:
      sensor_stringcomplete = false;                                           //reset the flage used to tell if we have recived a completed string from the Atlas Scientific product
      }
 }

Thanks again.

Arch, I think I see what you're saying. I was under the impression that inputstring is being set to whatever I type into the serial monitor, and therefore if I typed 'break' into the serial monitor, inputstring would then be set to 'break' and then I would exit the while loop. Am I wrong in assuming this?

Either way, it doesn't even seem like I'm inside the while loop right now, as I can't communicate with my probes.

mtomovich:
Arch, I think I see what you're saying. I was under the impression that inputstring is being set to whatever I type into the serial monitor, and therefore if I typed 'break' into the serial monitor, inputstring would then be set to 'break' and then I would exit the while loop. Am I wrong in assuming this?

Either way, it doesn't even seem like I'm inside the while loop right now, as I can't communicate with my probes.

I misread it, so I deleted my post. The problem is that when inputstring is complete, you null it out (by setting it to "") before it goes to check again, and viola! it's not "break" so the while loop continues. Once the loop breaks, you can then set inputstring to "".

For anyone who comes across this thread later, here’s what I ended up doing to get it to work.

String inputstring = "";                                                       //a string to hold incoming data from the PC
String sensorstring = "";                                                      //a string to hold the data from the Atlas Scientific product
boolean input_stringcomplete = false;                                          //have we received all the data from the PC
boolean sensor_stringcomplete = false;                                         //have we received all the data from the Atlas Scientific product
unsigned int probe_select = 0;

  void setup(){                                                                //set up the hardware
     Serial.begin(38400);                                                      //set baud rate for the hardware serial port_0 to 38400
     Serial2.begin(38400);                                                     //set baud rate for software serial port_2 to 38400
     Serial3.begin(38400);                                                     //set baud rate for software serial port_3 to 38400
     inputstring.reserve(5);                                                   //set aside some bytes for receiving data from the PC
     sensorstring.reserve(30);                                                 //set aside some bytes for receiving data from Atlas Scientific product
     Serial.println("Type 1 to read pH or 2 to read EC.");  
     }

   void serialEvent() {                                                         //if the hardware serial port_0 receives a char              
               char inchar = (char)Serial.read();                               //get the char we just received
               inputstring += inchar;                                           //add it to the inputString
               if(inchar == '\r') {input_stringcomplete = true;}                //if the incoming character is a <CR>, set the flag
              }  
  void serialEvent2(){                                                         //if the hardware serial port_3 receives a char 
              char inchar = (char)Serial2.read();                              //get the char we just received
              sensorstring += inchar;                                          //add it to the inputString
              if(inchar == '\r') {sensor_stringcomplete = true;}               //if the incoming character is a <CR>, set the flag 
             }
  void serialEvent3(){                                                         //if the hardware serial port_3 receives a char 
              char inchar = (char)Serial3.read();                              //get the char we just received
              sensorstring += inchar;                                          //add it to the inputString
              if(inchar == '\r') {sensor_stringcomplete = true;}               //if the incoming character is a <CR>, set the flag 
             }

void loop(){                                                                   //here we go....
if (input_stringcomplete){         //if a string from the PC has been recived in its entierty 
  if(inputstring.equals("1\r")){
      probe_select = 1;
     }
     else if(inputstring.equals("2\r")){
       probe_select = 2;
     }
     else{
      switch(probe_select){
       case 1:{
         Serial3.print(inputstring);
         break;
       }
       case 2: {
        Serial2.print(inputstring);
       break; 
       }
       case 3: {
         // other probe
         break;
       }
      }
     }
  inputstring = "";
  input_stringcomplete = false;
  }
 if (sensor_stringcomplete){                                                   //if a string from the Atlas Scientific product has been recived in its entierty 
      Serial.println(sensorstring);                                            //send that string to to the PC's serial monitor
      sensorstring = "";                                                       //clear the string:
      sensor_stringcomplete = false;                                           //reset the flage used to tell if we have recived a completed string from the Atlas Scientific product
      }
 }

mtomovich:
Henry,
What I thought i was doing was setting inputstring using SerialEvent()

But not within the while loop.

And that the while loop would run until inputstring was set to “break” (that is, the while statement was checking to see if inputstring was set to “break”). Is that not the case? Thanks for the reply.

Where are you setting it to anything other than the initial state or a null within the while loop? Unless you read the input within the while loop, what you have is a dog chasing its own tail.
But I see that you’ve now overcome the problem by getting rid of the while loop.