I am connecting to my ardunio via a Bluetooth Bee (Actually , its a Seeedstudio 'bee') . Anyhow , I send a command to turn on/off an LED on pin 3 . The problem I am having is that it only seems to work 75% of the time. I can turn it on and off but sometimes I have to enter a/b several times , even if I am 1 foot away from the device. Could this be a hardware issue? Please let me know if any problems are obvious in my code , maybe a delay?
Thanks in advance!
#include <SoftwareSerial.h>
#define RxD 12
#define TxD 13
#define DEBUG_ENABLED 1
SoftwareSerial blueToothSerial(RxD,TxD);
int ledpin1 = 3;
void setup()
{
pinMode(ledpin1, OUTPUT);
pinMode(RxD, INPUT);
pinMode(TxD, OUTPUT);
setupBlueToothConnection();
}
void loop()
{
if(blueToothSerial.read() == 'a')
{
digitalWrite(ledpin1, HIGH);
blueToothSerial.println(" LED on pin one is now on");
}
if(blueToothSerial.read() == 'b')
{
digitalWrite(ledpin1, LOW);
blueToothSerial.println(" LED on pin one is now off");
}
}
void setupBlueToothConnection()
{
blueToothSerial.begin(38400);
delay(1000);
sendBlueToothCommand("\r\n+STWMOD=0\r\n");
sendBlueToothCommand("\r\n+STNA=TestMode\r\n");
sendBlueToothCommand("\r\n+STAUTO=0\r\n");
sendBlueToothCommand("\r\n+STOAUT=1\r\n");
sendBlueToothCommand("\r\n +STPIN=0000\r\n");
delay(2000);
sendBlueToothCommand("\r\n+INQ=1\r\n");
delay(2000);
}
void CheckOK()
{
char a,b;
while(1)
{
if(blueToothSerial.available())
{
a = blueToothSerial.read();
if('O' == a)
{
while(blueToothSerial.available())
{
b = blueToothSerial.read();
break;
}
if('K' == b)
{
break;
}
}
}
}
while( (a = blueToothSerial.read()) != -1)
{
}
}
void sendBlueToothCommand(char command[])
{
blueToothSerial.print(command);
CheckOK();
}