Let's Make Robots!

bluetooth problem

iam using hs-05 blu tooth module to control my bot using mobile...iam having app in my mobile to communicate ....i connected tx, rx, 3.3, gnd pins to arduino.. i didnt used 31,32  pins as shown in letsmakerobots.com/node/35715#comment-101636. my device is detected in the app...nothing happns that is bot not moving ..iam usin following code






#define lp 9 //to l293
#define ln 8
#define rp 7
#define rn 6

int val;  // Variable to receive data from the serial port
  
void setup() {

  // initialize the digital pins as output
pinMode(lp, OUTPUT);
pinMode(ln, OUTPUT);
pinMode(rp, OUTPUT);
pinMode(rn, OUTPUT);
 

Serial.begin(9600);
}

 
// Read serial port and perform command
void loop () {
  if (Serial.available()>0) {
    val = Serial.read();
  switch (val) {
    case 'D1':  
    digitalWrite(lp, HIGH);
digitalWrite(ln, LOW);
digitalWrite(rp, HIGH);
digitalWrite(rn, LOW);
Serial.println(" go straight");
     
      break;
    case 'D4':
 digitalWrite(lp,  LOW);
digitalWrite(ln, HIGH);
digitalWrite(rn, HIGH);
digitalWrite(rp, LOW);
Serial.println("back");  
    
      break;
    case 'D2':   
    digitalWrite(ln, HIGH);
digitalWrite(lp, LOW);
digitalWrite(rn, LOW);
digitalWrite(rp, HIGH);
Serial.println(" left turn");
     
      break;
    case 'D3':   
digitalWrite(lp, HIGH);
digitalWrite(ln, LOW);
digitalWrite(rn, HIGH);
digitalWrite(rp, LOW);
Serial.println(" right turn");   
break;
  }}}

  


  


  

 
  

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

thank you frinds particularly ctc.....c u again with my first bot

should i use pin 31, 32 and 34 as shown in this http://mbed.org/media/uploads/edodm85/ch05_sch2.png..............it seems no prob with app and my new code....i think problem is with pin connection?any way blutooth is detected by the phone

and iam some time connect usb to arduino for powering while bluetooth is connected?  should rx and tx of uno connected  to tx and rx of bluetooth? some blog showing interchanged connection and some showing direct connection?

 

Here is the code from the manual I sent you. It will basically bounce everything you send back to the phone, with the exception of the tilt-joystick --it will send these values out to 2 PWM pins (to be connected to 2 LEDS).

#define ledA 10

#define ledB 11

 

int qualifier;

int dataByte;

 

 

void setup()

{

  Serial.begin(38400);

  pinMode(ledA,OUTPUT);

  pinMode(ledB,OUTPUT);

}

 

void loop()

{

  if(Serial.available()>1)

  {

    qualifier=Serial.read();

    dataByte=Serial.read();  

  }

  switch (qualifier)

  {

    case 'A':

      Serial.print("aux button was pushed number ");

      delay(1700);

      Serial.print(dataByte);

    break;

    case 'C':

      Serial.print("received command number ");

      delay(1700);

      Serial.print(dataByte);  

    break;

    case 'D':

      Serial.print("Direction button was pushed number ");

      delay(1700);

      Serial.print(dataByte);

    break;

    case 'G':

      Serial.print("Send data box 1 contains  ");

      delay(1700);

      Serial.print(dataByte);    

    break;

    case 'H':

      Serial.print("send data box 2 contains ");

      delay(1700);

      Serial.print(dataByte);    

    break;

    case 'L':

      analogWrite(ledA,dataByte);

    break;

    case 'R':

      analogWrite(ledB,dataByte);

    break;

    case 'X':

      analogWrite(ledA,dataByte);    

    break;

    case 'Y':

      analogWrite(ledB,dataByte);    

    break;  

  }

  qualifier=0;

  dataByte=0

}

The check for Serial.available() should be greater than zero (Serial.available() > 0), because the function throws a -1 if theres nothing in the buffer and an one if there's a byte in the buffer.

You are only reading one byte. "Var" can not contain "D1" for example, it will only contain "D" or "1". You need a second SerialRead in there, and your (Serial.available) should be >1 instead of >0.

If you read the instructions I send you (the manual) you will fiind some example code for the arduino. In this example code, I show how both bytes are read then, using a switch-case, the first and second bytes are divided up. 

ok friend,i think  in the switch case anyway we can have one value passed, for reading D1 i need two value then how i can pass two valu to switch ?

 

   if ( val=='D'&& val1==1)  
  {  digitalWrite(lp, HIGH);
digitalWrite(ln, LOW);
digitalWrite(rp, HIGH);
digitalWrite(rn, LOW);
Serial.println(" go straight");}

Ok, I have sent and posted the example code 3 times now, could you please look at it? It is posted in a comment just below this. It shows clearly how to deal with the 2 bytes.

what i dont understand in above code is we are checking only qulifier value in switch, what about databyte, just printing in serial. for example D1 where  qulifier=D and databyte =1(forward) but in switch only quliferis cheked.

 

You simply have to add more if's or another switch case.... It would go here:

 case 'D':

      //Serial.print("Direction button was pushed number ");

     //delay(1700);

      //Serial.print(dataByte);
      if (dataByte== whatever) then do something
      if (dataByte== somethingelse) then do something else
      if (dataByte == )...

      Etc

      Etc 

    break;