 
 void setup() 
 { 
-  pinMode(9, OUTPUT);
-  digitalWrite(9, HIGH);
+    // user PIN 9 for AT mode
+    pinMode(9, OUTPUT);
+    digitalWrite(9, HIGH);
   
     Serial.begin(9600);   //Serial port for debugging
     pinMode(RxD, INPUT);    //UART pin for Bluetooth
     pinMode(TxD, OUTPUT);   //UART pin for Bluetooth
     Serial.println("\r\nPower on!!");
     
-   // sendBlueToothCommand("AT+BAUD4");//reset the module's baud rate
-   // sendBlueToothCommand("AT+RESET");
-    
-    blueToothSerial.begin(9600);
-    //blueToothSerial.listen();
-    
-    //setupBlueToothConnection();
-
-    //sendBlueToothCommand("AT\r\n");
-    //sendBlueToothCommand("AT+UART9600");
-    
-    //setupBlueToothConnection2();
- /**  if(setupBlueToothConnection() != 0) while(1);   //initialize Bluetooth
-    //this block is waiting for connection was established.
-    while(1)
-    {
-        if(recvMsg(1000) == 0)
-        {
-            if(strcmp((char *)recv_str, (char *)"OK+CONB") == 0)
-            {
-                Serial.println("connected\r\n");
-                break;
-            }
-        }
-        delay(200);
-    }
-    
-    */
-    
-    
+    blueToothSerial.begin(9600); 
 } 
 
-void setupBlueToothConnection2()
-{
-    Serial.print("Setting up Bluetooth link");       //For debugging, Comment this line if not required    
-    blueToothSerial.begin(9600); //Set BluetoothBee BaudRate to default baud rate 38400
-    delay(1000);
-    sendBlueToothCommand2("\r\n+STWMOD=0\r\n");
-    sendBlueToothCommand2("\r\n+STNA=modem\r\n");
-    sendBlueToothCommand2("\r\n+STAUTO=0\r\n");
-    sendBlueToothCommand2("\r\n+STOAUT=1\r\n");
-    sendBlueToothCommand2("\r\n+STPIN=0000\r\n");
-    delay(2000); // This delay is required.
-    blueToothSerial.print("\r\n+INQ=1\r\n");
-    delay(2000); // This delay is required.
-    Serial.print("Setup complete");
- 
-}
-
-void sendBlueToothCommand2(char command[])
-{
-    char a;
-    blueToothSerial.print(command);
-    Serial.print(command);                          //For debugging, Comment this line if not required    
-    delay(3000);
- 
-    while(blueToothSerial.available())              //For debugging, Comment this line if not required  
-    {                                               //For debugging, Comment this line if not required   
-       Serial.print(char(blueToothSerial.read()));  //For debugging, Comment this line if not required  
-    }                                               //For debugging, Comment this line if not required   
-}
-
 void loop() 
-{ 
-  //sendBlueToothCommand("AT\r\n");
-   /** #if MASTER  //central role
-    //in master mode, the bluetooth send message periodically. 
-    delay(400);
-    Serial.println("send: hi");
-    blueToothSerial.print("hi");
-    delay(100);
-    //get any message to print
-    if(recvMsg(1000) == 0)
-    {
-        Serial.print("recv: ");
-        Serial.print((char *)recv_str);
-        Serial.println("");
-    }
-    #else   //peripheral role
-    delay(200);
-    //the slave role only send message when received one.
-    if(recvMsg(1000) == 0)
-    {
-        Serial.print("recv: ");
-        Serial.print((char *)recv_str);
-        Serial.println("");
-        Serial.println("send: hello");
-        blueToothSerial.print("hello");//return back message
-    }
-    #endif
-    
-    **/
-    
- /*  if( blueToothSerial.available() > 0 )
-     {
-       Serial.print("Recv: ");
-       byte recv = (byte)blueToothSerial.read();
-       Serial.print(recv);
-       Serial.println("");
-       blueToothSerial.flush();
-       //recvMsg(1000);
-       //Serial.print(recv_str);
-     }
-     */
- 
- int i = 0;
- while(Serial.available() && (i < 100))
+{
+    int i = 0;
+    while(blueToothSerial.available())
     {                                              
-        recv_str[i] = (char)Serial.read();
+        recv_str[i] = (char)blueToothSerial.read();
         i++;
-    }
+        delay(20);
+    }
+    
     if(i > 0)
     {
-      recv_str[i] = '\0';
-      Serial.print(" Received: ");
-      Serial.print((char *)recv_str);
-      blueToothSerial.print("H f A");
-    }
- 
-/** if( Serial.available() > 0 )
- {
-   char data = (char)Serial.read();
-   Serial.print(" Sent: ");
-   Serial.print(data); 
-   Serial.println("");
-    blueToothSerial.print(data);//return back message
- }
- */
- 
-  /* if(recvMsg(1000) == 0)
-    {
-        Serial.print("recv (char*): ");
-        Serial.print((char *)recv_str);
-        blueToothSerial.flush();
-        Serial.println("");
-        delay(3000);
-        Serial.println("send: Ack");
-        blueToothSerial.print("Ack");//return back message
-    }
-    */
-    
+        //check if there's any data sent from the remote bluetooth shield
+        recv_str[i] = '\0';
+        Serial.print("From BT: ");
+        Serial.println((char *)recv_str);
+
+        blueToothSerial.print("Return: ");
+        blueToothSerial.println((char *)recv_str);
+    }
+
+   //  String bluetooth_rx_buffer = "";
+
+ //   blueToothSerial.listen();
+
+/*
+    while (blueToothSerial.available()) 
+    {
+        char inByte = blueToothSerial.read();
+        
+      //  Serial.print("From BT: ");
+        Serial.print((char)inByte);
+
+delay(50);
+      //  blueToothSerial.print("Return: ");
+      //  blueToothSerial.print((char)inByte);
+    }
+
+*/
 }
 
 //used for compare two string, return 0 if one equals to each other

    return 0;
}

//Checks if the response "OK" is received
void CheckOK()
{
 char a,b;
 while(1)
 {
   if(blueToothSerial.available())
   {
     a = blueToothSerial.read();

     if('O' == a)
     {
       // Wait for next character K. available() is required in some cases, as K is not immediately available.
       while(blueToothSerial.available()) 
       {
         b = blueToothSerial.read();
         Serial.print(b);
         break;
       }
       if('K' == b)
       {
         break;
       }
     }
   }
 }

 while( (a = blueToothSerial.read()) != -1)
 {
   //Wait until all other response chars are received
 }
}

void sendBlueToothCommand3(char command[])
{
 blueToothSerial.print(command);
 Serial.print(command);
 CheckOK();   
}

//receive message from Bluetooth with time out
int recvMsg(unsigned int timeout)
{

#define RxD 2
#define TxD 3


SoftwareSerial blueToothSerial(RxD,TxD);//the software serial port 

