Arduino Leonardo and Bluetooth communication from phone

Hello

I am working on a project using Arduino Leonardo to control a DC motor via Bluetooth using an app on my andriod phone, so i want to control both direction and speed of the motor. I saw a tutorial about such topic showing sending 2Bytes number from android then combine them into one number (Byte1*256 + Byte2) and i thought that is what i need coz i want to send both speed value and direction to Arduino which can receive one Byte at once and as PWM values are 0 - 255 so i need another byte to send the direction too. i used MIT app inventor to create the app..the problem now is : this code works well on Arduino Nano but doesnt work on Leonardo that means it dosent respond and when i send for example 1000, i get on Serial monitor (12337, 12336) !

here is the code :

#define DIR 8
#define PWM 10

void setup(){
pinMode(DIR, OUTPUT);
pinMode(PWM, OUTPUT);
Serial.begin(9600);
// digitalWrite(DIR,LOW);

}

void loop() {
if (Serial.available()>= 2)
{
unsigned int a = Serial.read();
unsigned int b = Serial.read();
unsigned int val = (b * 256) + a ;
Serial.println(val);

if (val == 100) // reverse
{
digitalWrite(DIR, LOW);
}
else if (val == 200) // STOP
{
digitalWrite(DIR, LOW);
digitalWrite(PWM, LOW);
}
else if (val == 300) // FORWARD
{
digitalWrite(DIR,HIGH);
}
else if (val >= 1000 && val <= 1255)
{
analogWrite(PWM, val-1000);
}
}
}

Well for starters neither the Arduino Nano nor the Arduino Leonardo have bluetooth ability's. So you are using an extra part that you didn't tell us about. You are also not showing us how you wired everything together.

If you want help then show us your complete project. Also post your code using the code tags.

Which pins on the Leonardo is the bluetooth device connected to? Pins 0 and 1 go with Serial1, not Serial, on the Leonardo.

The serial monitor sends text, not numbers.

Subtracting the character '0' from the received character will give you the number (0..9).

E.g sending "19" results in receiving characters '1' and '9'. Subtracting '0' will give you the numbers 1 and 9. Multiply 1 by 10 and 9 by 1 and sum and you have the number 19.

Image from Reply #3 so we don't have to download it. See this Image Guide

...R

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text. The technique in the 3rd example will be the most reliable.

As a separate matter, why are there two Arduinos in your diagram?

...R

As a separate matter, why are there two Arduinos in your diagram?

Looks to me like one is a (motor) shield.

thanks but how could that be done in my code

Easily.

   num = num * 10;
   num += val - '0';

Where val is the character read from the serial port (that IS a digit).

So, you made some random changes to your code, and, while it does something mysterious, you expect it to do something else equally mysterious. Without posting any code, or clearly defining what the code does or what you expect, you now want us to use our crystal balls to tell you what the problem is. Sorry, no can do. Mine are brass.

Ferhad88:
this code works well on Arduino Nano but doesnt work on Leonardo
here is the code :

void setup(){
pinMode(DIR, OUTPUT);
pinMode(PWM, OUTPUT);
Serial.begin(9600);

In that event, I think your only problem is that you need to use Serial1 instead of Serial, as pointed out in reply#2, and which you appear not to have changed.

  if (Serial.available()>= 2)
  {
    char a = Serial.read();
    char b = Serial.read();
    char val = (b * 256) + a ;

   int num = num * 10;
   num += val - '0';

    Serial.println(num);

If your sender is sending two bytes, why are you storing the bytes in signed variables that can hold values from -128 to 127?

Why are you then thinking that converting the ASCII representation of a digit to a number is a useful thing to do?

If the sender is sending ASCII data, then the first two statements, to read a value like "100" don't make sense.

Just what IS being sent? Just why are you using char variables?