Arduino Bluetooth Issue
i connecting ardunio via bluetooth bee (actually , seeedstudio 'bee') . anyhow , send command turn on/off led on pin 3 . problem having seems work 75% of time. can turn on , off have enter a/b several times , if 1 foot away device. hardware issue? please let me know if problems obvious in code , maybe delay?
thanks in advance!
thanks in advance!
code: [select]
#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 1 on");
}
if(bluetoothserial.read() == 'b')
{
digitalwrite(ledpin1, low);
bluetoothserial.println(" led on pin 1 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())
{
= 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();
}
code: [select]
if(bluetoothserial.read() == 'a')
{
digitalwrite(ledpin1, high);
bluetoothserial.println(" led on pin 1 on");
}
if(bluetoothserial.read() == 'b')
{
digitalwrite(ledpin1, low);
bluetoothserial.println(" led on pin 1 off");
}
if character in buffer b, first statement read it, , decide not a. next statement read next, possibly non-existant, character, , decide not b.
you need test there character read first. then, need read , store character. then, need test stored character. not read every time.
Arduino Forum > Using Arduino > Programming Questions > Arduino Bluetooth Issue
arduino
Comments
Post a Comment