 
 void updateBoardState()
 {
+  // Update input pins first
   for(int i=0; i<TOTAL_NUM_OF_PINS; i++)
   {
-    if( g_aPins[i].is_input )
-    {
-      if( g_aPins[i].is_analog )
+    if( g_aPins[i].is_input ) // Process input pins
+    {
+      if( g_aPins[i].is_analog ) // Process analog pins
       {
         g_aPins[i].value = analogRead(i)/float(ANALOG_IN_MAX_VALUE);
       }
-      else
+      else // Process digital pins
       {
         g_aPins[i].value = digitalRead(i);
       }
     }
-    else
-    {
-      if( g_aPins[i].is_analog )
+  }
+
+  // Update outputs pins
+  for(int i=0; i<TOTAL_NUM_OF_PINS; i++)
+  {
+    if( !g_aPins[i].is_input ) // Process output pins
+    {
+       //Serial.println("HERE");
+//       Serial.print("Is Analog: ");Serial.println(g_aPins[i].is_analog);
+      if( g_aPins[i].is_analog ) // Process analog pins
       {
-        analogWrite(i, int(g_aPins[i].value*ANALOG_OUT_MAX_VALUE) );
+        //analogWrite(i, int(g_aPins[i].value*ANALOG_OUT_MAX_VALUE) );
+  //      Serial.print("Analog Pin "); Serial.print(i); Serial.print(": ");Serial.println(getTotalPinAnalogValue(i));
+        analogWrite(i, getTotalPinAnalogValue(i)*ANALOG_OUT_MAX_VALUE );
       }
-      else
+      else // Process digital pins
       {
-        digitalWrite(i, int(g_aPins[i].value) );
+        //digitalWrite(i, int(g_aPins[i].value) );
+  //      Serial.print("Digital Pin "); Serial.print(i); Serial.print(": ");Serial.println(getTotalPinDigitalValue(i));        
+        digitalWrite(i, getTotalPinDigitalValue(i) );
       }
     }
   }
 }
 
+float getTotalPinAnalogValue(int _iPinNum)
+{
+  float fPinValSum = 0;
+  
+  for(int i=0; i<TOTAL_NUM_OF_PINS; i++)
+  {
+    // Checking what pins are connected to iPinNum
+    if(g_aPins[_iPinNum].connections[i])
+    {
+      fPinValSum += g_aPins[i].value;  
+      Serial.print(i); Serial.print(" --> "); Serial.println(_iPinNum);
+      Serial.print("Value: "); Serial.println(fPinValSum);
+    }    
+  }
+  
+  if(fPinValSum > 1.0)
+    fPinValSum = 1.0;
+  
+  return fPinValSum;
+}
+
+int getTotalPinDigitalValue(int _iPinNum)
+{ 
+  for(int i=0; i<TOTAL_NUM_OF_PINS; i++)
+  {
+    // Checking what pins are connected to iPinNum
+    if(g_aPins[_iPinNum].connections[i])
+    {
+      if(g_aPins[i].value == 1.0)
+        return 1.0;
+    }    
+  }
+  
+  return 0.0;
+}
 
 // Get the board state and return a JSON object
 aJsonObject* getJsonBoardState()

      // Print pin value after assigment
//            Serial.println("Pin Data: ");
      //      char sPinState[128];
  //    snprintf(sPinState, sizeof(sPinState), "%s,%i,%i,%f", g_aPins[i].label, g_aPins[i].is_analog, g_aPins[i].is_input, g_aPins[i].value);
  //    Serial.println(sPinState);

    }    
  }


    ///////////////////////////////////////
    // Test receiving message
    
    if(g_ToggleFlag)
      snprintf(g_acMessage,sizeof(g_acMessage),g_acPin3_On);  
    else
      snprintf(g_acMessage,sizeof(g_acMessage),g_acPin3_Off);
    
    g_ToggleFlag = ~g_ToggleFlag;
    
    processMessage(g_acMessage);   
    updateBoardState();
    

    ///////////////////////////////////////
    /*
    // Connect/Disconnect
    switch(g_iDebugState)
    {
      case 0:
        snprintf(g_acMessage,sizeof(g_acMessage),g_acPin13_On_3_Analog);  
        processMessage(g_acMessage);
        g_iDebugState = 1;
        Serial.println("HERE 0");
      break;
      
      case 1:
        snprintf(g_acMessage,sizeof(g_acMessage),g_acConnA0_TO_3);  
        processMessage(g_acMessage);   
        g_iDebugState = 2;       
         Serial.println("HERE 1");      
      break;
      
      default:
      
      break;
    }
   
    updateBoardState();
    */
    ///////////////////////////////////////

    //////////////////////////////////////////
