Go Down

Topic: blueTooth serial shield help (Read 3076 times) previous topic - next topic

May 08, 2012, 11:30 pm Last Edit: May 11, 2012, 12:35 am by strigoi8183 Reason: 1
This is the code i am using to test bluetooth serial shield on arduino with android. So far i can connect and see the commands being sent
and received but cannot turn led on or off with the following code Please HELP!!!!!

The bluetooth shield is a:

SEED TECHNOLOGY INC (SEEEDUINO)
Bluetooth Shield
Model: SLD63030P

They Recommend using Bluetoothspp from the market place on android for this shield.

I have followed their instructions and tried to get help from their forum but they are no help!!

Code: [Select]

/*
simple LED serial controll test with bluetooth
*/
#include <SoftwareSerial.h>
#define RxD 6
#define TxD 7
#define DEBUG_ENABLED 1
SoftwareSerial blueToothSerial(RxD,TxD);

char recvChar;         // variable to receive data from the serial port
int ledpin = 13;  // LED connected to pin 13 (on-board LED)

void setup()
{
 pinMode(ledpin, OUTPUT);  // pin 13 (on-board LED) as OUTPUT
 Serial.begin(9600);       // start serial communication at 9600bps
 pinMode(RxD, INPUT);
 pinMode(TxD, OUTPUT);
 setupBlueToothConnection();
}

void loop() {
 char recvChar;
 while(1){
   if(blueToothSerial.available()){
     recvChar = blueToothSerial.read();
     Serial.print(recvChar);
   }
   if( Serial.available() ) {      // if data is available to read
     recvChar = Serial.read();
     blueToothSerial.print(recvChar);
   }
 }
          // read it and store it in 'val'
 if( recvChar == '0' )               // if '0' was received led 13 is switched off
 {
   digitalWrite(ledpin, LOW);    // turn Off pin 13 off
   delay(1000);                  // waits for a second  
   Serial.println("13 off");
 }

 if( recvChar == '1' )               // if '1' was received led 13 on
 {
   digitalWrite(ledpin = 13, HIGH);  // turn ON pin 13 on
   delay(1000);                  // waits for a second
   Serial.println("13 on");
 }
}
void setupBlueToothConnection() {
 blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
 blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
 blueToothSerial.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as "SeeedBTSlave"
 blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
 blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
 delay(2000); // This delay is required.
 blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
 Serial.println("The slave bluetooth is inquirable!");
 delay(2000); // This delay is required.
 blueToothSerial.flush();
}

pylon

You don't check for result codes from the bluetooth shield. Have you checked that the TX/RX lines are connected the right way? If you put the jumpers in the opposite position the bluetooth shield will never heard anything from the Arduino and the same is true for the other direction.

Have you tried connecting the bluetooth shield to your PC (putting the jumpers to 0 and 1) and sending the AT commands from a serial terminal programm? What response do you get from the bluetooth module on the shield if you send the AT commands?

PaulS

Why do you have an infinite loop in the loop() function? That infinite loop never ends, so, of course you can't do anything with the data that you get.

liudr

Paul has sharp eyes. Your code will not work elegantly without some additional stuff, such as return if no data is available or have an initial value of zero for recvChar.

Thank you guys for looking over the code, thank you PaulS for pointing out where I have mistakes and could use some changes.

To answer pylon('s) question.... No I have not tried moving the jumpers as I can send and receive serial data just fine, I just couldnt get it to respond to what I was sending to it. Other than it displaying in a serial monitor, and android device...

I will revise my code and repost the changes if it does/doesnt work.

again thank you guys for the time and patience as I am still kinda new to micro controllers and their language.

Ok so I took the advice from PaulS and just removed the infinite loop inside the loop and all problems resolved... Thank you guys you are the best. Now to use this new info to do my bidding... or maybe some robot project is in order....

liudr


Ok so I took the advice from PaulS and just removed the infinite loop inside the loop and all problems resolved... Thank you guys you are the best. Now to use this new info to do my bidding... or maybe some robot project is in order....


That will make the program seem to work, but combing through the logic will reveal a mistake. Think about what happens if you had a '1' last time and don't have any serial input next time you run loop(). It won't kill your program but is a logic flaw that will do real damage when your program starts controlling more serious stuff than LED.

#7
May 10, 2012, 11:05 pm Last Edit: May 11, 2012, 12:36 am by strigoi8183 Reason: 1
So then I should have loop inside loop but make sure there is a break or some way to exit the loop at close or between begin and end somewhere? or
Code: [Select]
while(1){//code here...}...return 0;  Sorry if this seems like a newbish question but as I stated b4 I am still new with Micro controllers.   Also can see live demonstration of code at : http://www.youtube.com/watch?v=uANm4i4j8_I

Code: [Select]
void loop() {
 char recvChar;

 if(blueToothSerial.available()){
   recvChar = blueToothSerial.read();
   Serial.print(recvChar);
 }
 if( Serial.available() ) {      // if data is available to read
   recvChar = Serial.read();
   blueToothSerial.print(recvChar);
 }

 // read it and store it in 'val'
 if( recvChar == '0' )               // if '0' was received led 13 is switched off
 {
   digitalWrite(redpin, LOW);    // turn Off pin 13 off
   delay(30);                  // waits for a second  
   digitalWrite(bluepin, LOW);
   delay(30);
   digitalWrite(greenpin, LOW);
   delay(30);
   Serial.println("All On");
 }

 if( recvChar == '1' )               // if '1' was received led 13 on
 {
   digitalWrite(redpin, HIGH);  // turn ON pin 13 on
   delay(30);                  // waits for a second
   digitalWrite(greenpin, HIGH);
   delay(30);
   digitalWrite(bluepin, HIGH);
   delay(30);
   Serial.println("All Off");
 }
 if( recvChar == 'r' )
 {
   digitalWrite(redpin, HIGH);
   delay(30);
   digitalWrite(greenpin, LOW);
   delay(30);
   digitalWrite(bluepin, LOW);
   delay(30);
   Serial.println("Red ON");
 }
 if( recvChar == 'b' )
 {
   digitalWrite(redpin, LOW);
   delay(30);
   digitalWrite(greenpin, LOW);
   delay(30);
   digitalWrite(bluepin, HIGH);
   delay(30);
   Serial.println("Blue ON");
 }
 if( recvChar == 'g' )
 {
   digitalWrite(redpin, LOW);
   delay(30);
   digitalWrite(bluepin, LOW);
   delay(30);
   digitalWrite(greenpin, HIGH);
   delay(30);
   Serial.println("Green ON");
 }
}

sharder

The correction of the code :
Code: [Select]

/* Upload this sketch into Seeeduino and press reset*/
#include <SoftwareSerial.h>   //Software Serial Port
#define RxD 6
#define TxD 7

#define DEBUG_ENABLED  1

SoftwareSerial blueToothSerial(RxD,TxD);
int led = 13;
void setup()
{
  Serial.begin(38400);
  pinMode(RxD, INPUT);
  pinMode(TxD, OUTPUT);
  setupBlueToothConnection();
  pinMode(led, OUTPUT);

}

void loop()
{
  char recvChar;
  while(1){
    if(blueToothSerial.available()){//check if there's any data sent from the remote bluetooth shield
      recvChar = blueToothSerial.read();
      if(recvChar == 'a')
      {
      digitalWrite(led, HIGH);
      }
      if(recvChar == 'b')
     {
      digitalWrite(led, LOW);
     }
      Serial.print(recvChar);
    }
    if(Serial.available()){//check if there's any data sent from the local serial terminal, you can add the other applications here
      recvChar  = Serial.read();
      blueToothSerial.print(recvChar);
    }
  }
}

void setupBlueToothConnection()
{
  blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
  blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
  blueToothSerial.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as "SeeedBTSlave"
  blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
  blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
  delay(2000); // This delay is required.
  blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
  Serial.println("The slave bluetooth is inquirable!");
  delay(2000); // This delay is required.
  blueToothSerial.flush();
}

I have tested it and worked......

Go Up