title: Using the Arduino serial monitor and nested loops to talk to different peripherals
body: <p>I'm making an arduino project that reads in some values from some <a href="http://atlas-scientific.com/product_pages/kits/ec-kit.html" rel="nofollow">Atlas Scientific</a> 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:</p>
  
  <pre><code>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
        }
   }
  </code></pre>
  
  <p>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.
  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'm trying to use a while loop inside the case statements to talk to my peripherals. </p>
  
  <pre><code>void loop(){                                                                  
  // read the selection
   if (Serial.available()&gt;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);
    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
        }
        }
  
        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
            }
            }
      .
      .
      .
      }
  </code></pre>
  
  <p>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" into the terminal to go back to my case statements and select another sensor. The while loop idea isn't working at all. I added debug lines to echo my commands, and it's not even doing that. Is there a better way to do these nested loops?</p>
  
  <p>Here's the full program for reference.</p>
  
  <pre><code>/*
  This software was made to demonstrate how to quickly get your Atlas Scientific product running on the Arduino platform.
  An Arduino MEGA 2560 board was used to test this code.
  This code was written in the Arudino 1.0 IDE
  Modify the code to fit your system.
  **Type in a command in the serial monitor and the Atlas Scientific product will respond.**
  **The data from the Atlas Scientific product will come out on the serial monitor.**
  Code efficacy was NOT considered, this is a demo only.
  The TX3 line goes to the RX pin of your product.
  The RX3 line goes to the TX pin of your product.
  Make sure you also connect to power and GND pins to power and a common ground.
  Open TOOLS &gt; serial monitor, set the serial monitor to the correct serial port and set the baud rate to 38400.
  Remember, select carriage return from the drop down menu next to the baud rate selection; not "both NL &amp; CR".
  */
  #include &lt;OneWire.h&gt;
  #include &lt;DallasTemperature.h&gt;
  
  // 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(&amp;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&lt;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 &lt;CR&gt;, 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 &lt;CR&gt;, 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 &lt;CR&gt;, 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 &lt; 8; i++)
    {
      if (deviceAddress[i] &lt; 16) Serial.print("0");
      Serial.print(deviceAddress[i], HEX);
    }
  }
  
  
  
  void loop(){                                                                   //here we go....
  // read the selection
   int r = 0; 
   if (Serial.available()&gt;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
        }
        }
  
        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
        }
        }
  
        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&lt;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;
      }
     }
  } 
  </code></pre>
  
  <p>Thanks,
  Mike</p>