     return 0;
 }
 
-


void setup() 
{ 
  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);
    }
    
    */
    
    
} 

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))
    {                                              
        recv_str[i] = (char)Serial.read();
        i++;
    }
    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
    }
    */
    
}

//used for compare two string, return 0 if one equals to each other

#define RxD 2
#define TxD 3


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

