#include <SoftwareSerial.h>
int bluetoothTx = 11; // TX-O pin of bluetooth mate, Arduino
int bluetoothRx = 10;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup() {
Serial.begin(9600); // initialization
bluetooth.begin(9600);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}

void loop() {
int pin; int type; int value; int buffer_length=0; int process=0;
while(bluetooth.available()) { // if the data came
int incomingByte = bluetooth.read(); // read incoming buffer
if(buffer_length==0){
type=incomingByte;
}
if(buffer_length==1){
pin=incomingByte;
}
if(buffer_length==2){
value=incomingByte;
}
buffer_length++;
process=1;
Serial.println(incomingByte); // Important do not remove this..
}
if(process==1){
if(type == 1){
analogWrite(pin, value);
}
else
{
if(value == 1){
digitalWrite(pin, HIGH);
}
if(value == 0){
digitalWrite(pin, LOW);
}
}
}
}