title: Arduino making decision according to a packet received from serial port
body: <p>The program listen to messages from serial port in the form  or  where first character (A or D) means analog or digital, the 2nd character - pin, the 3rd character - 1/0 or from 0 to 255. The markers &lt; and > show the beginning and the end of packet.</p>
  
  <p>For example, if packet  is received, the light is turned on by digitalWrite(13,1)
  But nothing happens. When I send via serial monitor, for instance:  light is supposed to blink but it does not. The same with analogue outputs.</p>
  
  <pre><code>bool started = false;
  bool ended = false;
  char inData[5];
  byte index;
  
  
  void setup() 
  {
    Serial.begin(9600);
  }
  
  void loop() 
  {
    while (Serial.available() &gt; 0)
    {
      char inChar = Serial.read();
  
  
      if (inChar == '&lt;')
      {
        index = 0;
        started = true;
        ended = false; 
      }
  
      else if (inChar == '&gt;')
      {
        ended = true;
        break;
      }
  
      else
      {
        if (index &lt;= 4)
        {
          inData[index] = inChar;
          index++;
        }
      }
  
  
  
       if (started &amp;&amp; ended)
      {
        if (inData[0] == 'A')
        {
          pinMode(inData[2],OUTPUT);
          analogWrite(inData[2],inData[4]);
        }
  
  
        else if (inData[0] == 'D')
        {
          if (inData[4] == 1)
          {
            pinMode(inData[2],OUTPUT);
            digitalWrite(inData[2],HIGH);
          }
  
          else if (inData[4] == 0)
          {
             pinMode(inData[2],OUTPUT);
             digitalWrite(inData[2],LOW);
          }
        }
        started = false;
        ended = false;
        index = 0; 
      }
  
    }
  
  
  Serial.println("Sending");  
  
  
  
  }
  </code></pre>