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;
}}}
Try
Try a
Serial.println(val);
after you assign it ( val = Serial.read(); ), and see if you’re actually receiving anything and/or what was expected.
Since ‘val’ is an int type
Since ‘val’ is an int type you should first try sending ONLY integer values like 1, 2, 3 etc. and not something like ‘D1’. I remember i had similar problems when i first tried it, If int val; doesn’t work try using a character type variable as char val; and send only a single character at a time and as Kariloy said, send back what is received, this way you will know what is being received if any.
Var=D1
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.
Code from the manual
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
}
ok friend,i think in the
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 ?
The check for
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.
is this ok
if ( val==‘D’&& val1==1)
{ digitalWrite(lp, HIGH);
digitalWrite(ln, LOW);
digitalWrite(rp, HIGH);
digitalWrite(rn, LOW);
Serial.println(" go straight");}
Example code
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
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.
More If’s or another switch-case
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;
regarding pin
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?
got it fixed
thank you frinds particularly ctc…c u again with my first bot