Why my arduino leonardo not working with bluetooth module , However this same code is working with Arduino UNO, Plz suggest me changes

Reciever code
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(6,7);
int xval=366;
int yval = 362;
int zval = 443;
int axisx;
int axisy ;
int axisz ;
void setup() {
Serial.begin(38400);
BTSerial.begin(38400);
pinMode(A5,INPUT);
pinMode(A4,INPUT);
pinMode(A3,INPUT);

}

void loop() {
int val1=analogRead(A5);
int val2=analogRead(A4);
int val3=analogRead(A3);

axisx = val1 - xval ;
axisy = val2 - yval ;
axisz = val3 - zval;

if (axisx <= -60){
Serial.println("l");
BTSerial.write('l');
}
else if(axisx >= 20){
Serial.println("r");
BTSerial.write('r');
}
else if( axisy <= -50){

BTSerial.write('b');

}

else if(axisy >= 15 ){
Serial.println("f");
BTSerial.write('f');

}else{
BTSerial.write('a');

}

}

not all pins on Leonardo work with SoftwareSerial. but Leonardo has Serial1 so you don't need SoftwareSerial

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

Please read How to get the best out of this forum and pay special attention to how to post code; next apply what you've 'learned' to the code in your opening post.

Sir could you please tell me that when I'm using the Serial1 than which pins would I use ??and is there any library to add Serial1 or it is predefined for Leonardo?

Arduino Leonardo is based on ATmega32u4 chip, which has built-in USB communication. That means pins 0 (Rx) and 1(Tx) become Serial1. It´s predefined for Leonardo, there´s no need to call any additional library or define the pins. Just connect BT module to these pins.

In your code, instead of calling BTSerial.write(), call Serial1.write() instead.

I want to communicate between Leonardo and uno using Bluetooth.the uno dosent have the Serial1 so how could I send value to the Leonardo if Leonardo has Serial1 but uno don't have...please help

You will need two BT modules then. One connected to Uno and another connected to Leonardo. You wil also need to write slightly different codes for each of them.
The code above is the transmitter. It uses SoftwareSerial, so it works on the Uno. The other code can use Serial1 of Leonardo, but it shall be written to receive the data that Uno is sending...

What changes can do I have to made in my code in order to read the data sent by the uno to leonardo

First of all, there´s no need to do all the analog readings stuff (this will be done by Uno, the transmitter, right?).

Take a look at this tutorial --> https://create.arduino.cc/projecthub/mayooghgirish/arduino-bluetooth-basic-tutorial-d8b737

What is done in the loop of the example´s code is basically what you will need, but reading from Serial1 and comparing with the characters of your code ( 'r', 'b', 'f' and 'a').

Yes the Leonardo will just read the data.But it can read from serial 1 only so for that I have to make Serial 1 in uno also using software serial?

Nope.

  1. the communication between Uno and the first Bluetooth module is done by SoftwareSerial, as in the code you´ve posted in #1. The Uno sends data through this Bluetooth module (let´s call it BT1);

  2. the Bluetooth module 2 (BT2) will receive the data and pass to Leonardo through Serial1 (BT will be connected to RX/TX of Leonardo).

Something like this:

sending

#include<SoftwareSerial.h>
SoftwareSerial btSerial(5,6);
int xval=366;
int yval = 362;
int zval = 443;
int axisx;
int axisy ;
int axisz ;

void setup() {
btSerial.begin(38400);
//Serial.begin(38400);
pinMode(A5,INPUT);
pinMode(A4,INPUT);
pinMode(A3,INPUT);
}

void loop() {

int val1=analogRead(A5);
int val2=analogRead(A4);
int val3=analogRead(A3);
//Serial.print("x asis");
axisx = val1 - xval ;
axisy = val2 - yval ;
axisz = val3 - zval;
//Serial.println(axisx);
//Serial.println(axisy);
//Serial.println(axisz);
if (axisx <= -60){
//Serial.println("l");
btSerial.write('l');
}
else if(axisx >= 20){
//Serial.println("r");
btSerial.write('r');
}
else if( axisy <= -50){

btSerial.write('b');

}

else if(axisy >= 15 ){
//Serial.println("f");
btSerial.write('f');

}else{
btSerial.write('a');

}

}

recieving by leonardo

void setup() {
Serial1.begin(38400);
pinMode(9,OUTPUT);
pinMode(8,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
// put your setup code here, to run once:
}
void loop() {
char msg;
if (Serial1.available()){
delay(10);
msg=Serial1.read();
if (msg==108){
digitalWrite(11,1);
digitalWrite(9,LOW);
digitalWrite(8,LOW);
digitalWrite(10,LOW);
//Serial.println(msg);

}
else if(msg==114){
digitalWrite(9,HIGH);
digitalWrite(8,LOW);
digitalWrite(11,LOW);
digitalWrite(10,LOW);
//Serial1.println(msg);
}

else if(msg==102){
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
digitalWrite(9,LOW);
digitalWrite(8,LOW);
//Serial.println(msg);

} else if(msg==98){
digitalWrite(8,HIGH);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
digitalWrite(9,LOW);
Serial.println(msg);
}
else{
digitalWrite(9,HIGH);
digitalWrite(8,HIGH);
digitalWrite(11,HIGH);
digitalWrite(10,HIGH);
//Serial.println(msg);
}
}
}

sir kindly check if the code is correct if not what changes could be done?

Sorry, I cannot reproduce your circuit. It´s time for you to begin testing the code.

When posting codes, please use the code tags --> image

Please read and follow the instructions for posting. Incorrectly posted code makes it difficult for people to find problems.

You can edit your previous posts using the "pencil" icon below them, no need to make a new post.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.