Programming with a Bluetooth Module

Hi everybody!
I’m trying to connect to the arduino one using a serial bluetooth (http://www.seeedstudio.com/wiki/Grove_-_Serial_Bluetooth) and then control it with my mobile, with which I’m using SENA BTerm to send messages to the arduino one.

If I load the following code, I can achieve the general purpose of this basic program. I can send the command “m1” and then I receive the corresponding message from arduino.

#include <SoftwareSerial.h> //Software Serial Port
#define RxD 6
#define TxD 7

SoftwareSerial blueToothSerial(RxD,TxD);
char cadena[24];
int i=0;
int ms3=5;
int ms2=6;
int ms1=7;
int dir=8;
int stp=9;

void setup()
{
Serial.begin(9600);
pinMode(RxD, INPUT);
pinMode(TxD, OUTPUT);
setupBlueToothConnection();
}

void loop()
{

if(blueToothSerial.available()){//check if there’s any data sent from the remote bluetooth shield
blueToothSerial.println(“Recepcion de Datos…”);
while (blueToothSerial.available()>0){
delay(5);
cadena*=blueToothSerial.read();*

  • i++;*

  • }*

  • i=0;*

  • blueToothSerial.print("El texto introducido es… ");*

  • Serial.print("El texto introducido es… "); *

  • blueToothSerial.println(cadena);*

  • Serial.println(cadena); *

  • switch (cadena[0]){*

  • case ‘m’:*

  • blueToothSerial.println(“Opcion M”);*

  • Serial.println(“Opcion M”);*

  • switch (cadena[1]){*

  • case ‘0’:*

  • blueToothSerial.println(“Parada”);*

  • Serial.println(“Parada”);*

  • goto Texto;*

  • case ‘1’:*

  • blueToothSerial.println(“Izquierda”);*

  • Serial.println(“Izquierda”);*

  • goto Mover;*

  • case ‘2’:*

  • blueToothSerial.println(“Derecha”);*

  • Serial.println(“Derecha”);*

  • goto Mover;*

  • default:*

  • goto Texto; *

  • }//Fin casos M *

  • case ‘d’:*

  • blueToothSerial.println(“Opcion D”);*

  • Serial.println(“Opcion D”);*

  • goto Texto;*

  • default:*

  • goto Texto;*

  • }*
    }
    Mover:
    ;
    Texto:
    memset(cadena, 0, sizeof(cadena));
    Fin:
    ;
    }

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+STPIN=0000\r\n");//Set SLAVE pincode"0000"
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
blueToothSerial.println(“The slave bluetooth is inquirable!”);
delay(2000); // This delay is required.
blueToothSerial.flush();
*} *
But if I add some other configuration in the setup zone (like int the following example), it doesn’t work.
void setup()
{
Serial.begin(9600);
pinMode(RxD, INPUT);
pinMode(TxD, OUTPUT);
setupBlueToothConnection();
*pinMode(dir, OUTPUT); *
pinMode(stp, OUTPUT);
pinMode(ms1, OUTPUT);
*pinMode(ms2, OUTPUT); *
pinMode(ms3, OUTPUT);
digitalWrite(stp, LOW); //STEP
digitalWrite(ms1,HIGH); //MS1
digitalWrite(ms2,HIGH); //MS2
digitalWrite(ms3,HIGH); //MS3
digitalWrite(dir,HIGH); //Direccion
}
Can anyone give me some advice about what I can be doing wrong??
Thanks in advance for your wisdom!!!

How to use this forum

Read this before posting a programming question

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the # button above the posting area.

Please don't use "goto".

Sorry. The code that works is:

#include <SoftwareSerial.h> //Software Serial Port
#define RxD 6
#define TxD 7
 
SoftwareSerial blueToothSerial(RxD,TxD);
char cadena[24];
int i=0;
int ms3=5;
int ms2=6;
int ms1=7;
int dir=8;
int stp=9;
 
void setup()
{
 Serial.begin(9600);
 pinMode(RxD, INPUT);
 pinMode(TxD, OUTPUT);
 setupBlueToothConnection();
}
 
void loop()
{

    if(blueToothSerial.available()){//check if there's any data sent from the remote bluetooth shield
    blueToothSerial.println("Recepcion de Datos...");
    while (blueToothSerial.available()>0){
      delay(5);
      cadena=blueToothSerial.read();
      i++;
     }
    i=0;
    blueToothSerial.print("El texto introducido es... ");
    Serial.print("El texto introducido es... ");   
    blueToothSerial.println(cadena);
    Serial.println(cadena); 
    switch (cadena[0]){
     case 'm':
         blueToothSerial.println("Opcion M");
         Serial.println("Opcion M");
         switch (cadena[1]){
           case '0':
             blueToothSerial.println("Parada");
             Serial.println("Parada");
             goto Texto;
           case '1':
             blueToothSerial.println("Izquierda");
             Serial.println("Izquierda");
             goto Mover;
           case '2':
             blueToothSerial.println("Derecha");
             Serial.println("Derecha");
             goto Mover;
           default:
             goto Texto;         
         }//Fin casos M 
         
     case 'd':
         blueToothSerial.println("Opcion D");
         Serial.println("Opcion D");
         goto Texto;
     default:
         goto Texto;
    }
 }
Mover:
;

Texto:
memset(cadena, 0, sizeof(cadena));
Fin:
;
}
 
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+STPIN=0000\r\n");//Set SLAVE pincode"0000"
 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
 blueToothSerial.println("The slave bluetooth is inquirable!");
 delay(2000); // This delay is required.
 blueToothSerial.flush();
}

But when I include the following configuration in the setup area, it doesn’t work.

void setup()
{
 Serial.begin(9600);
 pinMode(RxD, INPUT);
 pinMode(TxD, OUTPUT);
 setupBlueToothConnection();
 pinMode(dir, OUTPUT);     
 pinMode(stp, OUTPUT);
 pinMode(ms1, OUTPUT);
 pinMode(ms2, OUTPUT);   
 pinMode(ms3, OUTPUT);
 digitalWrite(stp, LOW); //STEP
 digitalWrite(ms1,HIGH); //MS1
 digitalWrite(ms2,HIGH); //MS2
 digitalWrite(ms3,HIGH); //MS3
 digitalWrite(dir,HIGH); //Direccion
}
#define RxD 6
#define TxD 7
 
SoftwareSerial blueToothSerial(RxD,TxD);

...

int ms2=6;
int ms1=7;

...

 pinMode(ms1, OUTPUT);
 pinMode(ms2, OUTPUT);   

...

 digitalWrite(ms1,HIGH); //MS1
 digitalWrite(ms2,HIGH); //MS2

Make up your mind whether you are going to use pins 6 and 7 for SoftwareSerial or some other purpose.

           case '2':
             blueToothSerial.println("Derecha");
             Serial.println("Derecha");
             goto Mover;

Just use "break". Don't use "goto".

Thank you Nick. I copied the code from my original program and forgot to change it. Thank you again. I'm sure it works now.

If you have an android mobile then you might want to take a look at the BTInterface app - www.btinterface.com

It's a great way to interface a phone to your Arduino sketch over bluetooth.