title: Why does Arduino only run my loop twice when communicating with the serial port?
body: <p>Long story short, I am trying to use the Arduino as an IO module, and this code is for writing values to the digital and analogue outports. The commands are sent through the serial port bytewise(to reduce lagging) from a Python program, hence the weird structure.</p>
  <p>My problem now is that for some reason, Arduino can read two commands (one at a time) and then it stops. No errors or anything, it just doesn't do anything, and I cannot figure out where my mistake is for the life of it.</p>
  <p>The messages are structured as three bytes with x's as the separator. The first indicates if the outport is analogue <code>o</code> or digital <code>p</code>, the second defines the channel number, and the last the value that is to be sent to said channel. Example: <code>ox2x50</code> means A2 should be set to 50%.</p>
  <p>BTW: This is just a snippet of a more extensive program, so atm I'm simply typing the commands in the serial port.</p>
  <p>Can anyone tell me why it only runs twice?</p>
  <pre><code>/* Still in development, hence the excessive amount of printing commands*/
  
  const byte numChars = 32;
  char receivedChars[numChars];   // an array to store the received data
  String stringpart;
  String sortedData[3];
  
  boolean newData = false;
  
  // Declare outputs
  const int DO0 = 2;
  const int DO1 = 4;
  const int DO2 = 7;
  const int DO3 = 8;
  
  const int AO0 =3;
  const int AO1 =5;
  const int AO2 =6;
  const int AO3 =9;
  
  void setup() {
      Serial.begin(9600);
      //Serial.println(&quot;&lt;Arduino is ready&gt;&quot;);
      pinMode(DO0, OUTPUT);
      pinMode(DO1, OUTPUT);
      pinMode(DO2, OUTPUT);
      pinMode(DO3, OUTPUT);
      pinMode(AO0, OUTPUT);
      pinMode(AO1, OUTPUT);
      pinMode(AO2, OUTPUT);
      pinMode(AO3, OUTPUT);
  }
  
  void loop() {
    if(Serial.available()&gt;0){           // Return the number of bytes available on serial. if &lt;0, = no info on serial.
  
    
      recvWithEndMarker();
      showNewData();
      analogueOut();     //NB! Resets newdata
      //readarray = recvWithEndMarker();
  
      
    }//If serial available
  }
  
  // Reads from recial port until \n is received.
  String recvWithEndMarker() {
      static byte ndx = 0;
      char endMarker = '\n';
      char rc;
      
      while (Serial.available() &gt; 0 &amp;&amp; newData == false) {
          rc = Serial.read();
  
          if (rc != endMarker) {
              receivedChars[ndx] = rc;
              ndx++;
              if (ndx &gt;= numChars) {
                  ndx = numChars - 1;
              }
          }
          else {
              receivedChars[ndx] = '\0'; // terminate the string
  
              // Extracting each vlaue split by X
              for(int j = 0; j &lt;= 2; j++) {                       // Only runs 3 times, as it needs to extract 1.A or D 2.ChNo and 3. Value
                 sortedData[j] = splitValue(receivedChars,'x',j);   // Extracts the j number position  
              }// for loop
  
              for(int i = 0; i &lt;= 2; i++) {                       // Only for test printing
                 Serial.println(sortedData[i]);  
              }// for loop
  
              ndx = 0;                                      // Reset counter
              newData = true;                               // Inform that new data has arrived
  
          }
      }
  
   
  }
  
  // Simply prints what's recieved
  void showNewData() {
      if (newData == true) {
          //Serial.print(&quot;This just in ... &quot;);
          Serial.println(receivedChars);
          
          
      }
  }
  
  // Splits the received string into its separate characters and stores them in array
  String splitValue(String data, char separator, int index)
  {
    int found = 0;
    int strIndex[] = {0, -1};
    int maxIndex = data.length()-1;
  
    for(int i=0; i&lt;=maxIndex &amp;&amp; found&lt;=index; i++){
      if(data.charAt(i)==separator || i==maxIndex){
          found++;
          strIndex[0] = strIndex[1]+1;
          strIndex[1] = (i == maxIndex) ? i+1 : i;
      }
    }
  
    return found&gt;index ? data.substring(strIndex[0], strIndex[1]) : &quot;&quot;;
  }
  
  
  // Function to convert ASCII symbol to integer. For sortData[2] 
  int stringInt(char recievedVal){ 
  
      int y = int(recievedVal);                     // Converts ASCII character to corresponding integers  0-100 
  
      int finalVal = constrain((map(y, 0, 100, 0, 255)), 0, 255);  //Converts 0-100(recieved as char) to 0-255 
      
      Serial.println(&quot;Recieved value: &quot;); 
      Serial.println(finalVal);           // Print value between 0-255.    
   
  } // stringInt function 
  
  
  
  // Functino to (hopefully) split and convert recieved data into something useful
  
  String analogueOut() { // Use sortedData as input, writes to analogue outputs
  
    if (newData == true) {
      // Result in an array with two elements. One A1/D2 etc and one Value/onoff
  
      int channel = (sortedData[1]).toInt();   // Converts the number recieved in channel number to an int
      char chVal = (sortedData[2]).toInt();    // Converts the number recieved in channel Val to an int which is then converted to a char
      int chValConv = stringInt(chVal);        // Sends the char chVal to string toInt function, returned scaled and converted to int.
    
  
      if (sortedData[0] == &quot;o&quot;) {
        //Serial.println(&quot;Analogue&quot;);
  
        switch (channel) {
            case 1:
              Serial.println(&quot;AO0&quot;);
              analogWrite(AO0, chValConv);
            break;
            case 2:
              Serial.println(&quot;AO1&quot;);
              analogWrite(AO1, chValConv);
            break;
            case 3:
              Serial.println(&quot;AO2&quot;);
              analogWrite(AO2, chValConv);
            break;
            case 4:
              Serial.println(&quot;AO3&quot;);
              analogWrite(AO3, chValConv);
            break;
            default:
              Serial.println(&quot;NaN&quot;);
            break;
        } // Switch case channel
        
      } // if recievedArray[0]=o
  
      else if (sortedData[0] == &quot;p&quot;) {
        //Serial.println(&quot;Digital&quot;);
  
        switch (channel) {
            case 1:
              Serial.println(&quot;DO0&quot;);
              analogWrite(DO0, chValConv);
            break;
            case 2:
              Serial.println(&quot;DO1&quot;);
              analogWrite(DO1, chValConv);
            break;
            case 3:
              Serial.println(&quot;DO2&quot;);
              analogWrite(DO2, chValConv);
            break;
            case 4:
              Serial.println(&quot;DO3&quot;);
              analogWrite(DO3, chValConv);
            break;
            default:
              Serial.println(&quot;NaN&quot;);
            break;
        } // Switch case channel
      
      } // if recievedArray[0]=p 
  
      else {
        Serial.println(&quot;Not valid&quot;);
      } // if recievedArray[0]=p 
  
      
      newData = false;
    } // if newData True
    
  } // String analogueOut
  </code></pre>