Arduino to Arduino via BT

Well I want to communicate 2 Arduinos via Bluetooth. I have my HC-05 and a ZS-040 version of the HC-05. I managed to enter to AT mode and pair them, the HC-05 as master and the ZS-040 as slave. I uploaded this to the Arduino with the master:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2,3);

void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
mySerial.println("Conectado");
}
void loop()
{

mySerial.write('a');
Serial.write(mySerial.read());
delay(2000);

mySerial.write('1');
Serial.write(mySerial.read());
delay(1000);
}

and this to the slave:

SoftwareSerial mySerial(2,3);

void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
pinMode(11,OUTPUT);

digitalWrite(11,LOW);
}

void loop()
{

char c=mySerial.read();

if(c=='1') digitalWrite(11,HIGH);

if(c=='a') digitalWrite(11,LOW);

}

I want to send 1 and a and turn on and off a LED light. It does not work.

I included the line Serial.write(mySerial.read()); so I can read in the Arduino software if my HC-05 is sending the 1 and the a but when I put the serial monitor it just reads "ÿ" over and over....

Any advices? or ideas?

I added Serial.write(c) in the slave code, so I can read what it was reciving.... "ÿ" again

Did you cross he wires between the serial pins on the two processors? The Rx line on one processor goes to the Tx line on the other, and vice versa. It's a very common mistake to make.

This is a very unreliable way to receive data.

     char c=mySerial.read();
       if(c=='1') digitalWrite(11,HIGH);
       if(c=='a') digitalWrite(11,LOW);

At the very least you need to check if any data has been received using

if (mySerial.available > 0)

Have a look at the examples in Serial Input Basics

And please use the code button </>

so your code looks like this

and is easy to copy to a text editor

...R

  1. Ensure both BT modules are running on the same baud rate (check AT command).

  2. Ensure your serial terminal is set to read 9600 baud.

  3. Store incoming data in a char array for future analysis. Your code will work...buy future proof it?

I did cross the wires. I've doing a lot of investigation... tho I can't find the problem. I readed Robin's post about serial imput basics, but before applying that I'll check the baud rate on the BTs. Thanks guys, I'll post if it solves the problem

I changed the code, and added the line : if (mySerial.available() > 0)

This is conditional to the HIGH and LOW commands for the pin 11. There's a line which reads what's the slave recieving . It doesn't show anything on the serial monitor, so it's not aviailable. What should I do?

Another thought: do you have a common ground connection between the two processors?

fran_a:
What should I do?

Post the latest code.
Make a pencil drawing showing how everything is connected and post a photo of the drawing.
Study the examples in Serial Input Basics and perhaps try the second example.

...R

Master:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2,3);//2 rx to tx, 3 tx to rx

void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
}
void loop()
{

mySerial.write('a');
Serial.write(mySerial.read());
delay(2000);

mySerial.write('1');
Serial.write(mySerial.read()); //Keeps sending "ÿ" .....
delay(1000);
}

Slave:

#include <SoftwareSerial.h>

SoftwareSerial BT(2,3);//2 rx to tx, 3 tx to rx

void setup()
{
Serial.begin(9600);
BT.begin(9600);
pinMode(8,OUTPUT);
digitalWrite(8,LOW);
}

void loop()
{
char c;

if (BT.available() > 0)
{
c=BT.read();

if(c=='1') digitalWrite(8,HIGH);

if(c=='a') digitalWrite(8,LOW);

Serial.write(c); //It does not write anything in the serial monitor, so it doesn't enter to the IF
conditional

}
}

Modules I'm using:

Checked the baud rate on the bluetooths. Chaged both to 9600. Connection mode set to 0, and binding command set to each others addr.

I'm checking again the Serial Imput Basics in case I missed something.

Btw, both are Arduinos UNO boards.

Are you trying to make the master send a message and have the slave repeat it back to the master?

If so, both ends need reliable receive code. Use the 3rd example in Serial Input Basics

...R

Robin2:
Are you trying to make the master send a message and have the slave repeat it back to the master?

If so, both ends need reliable receive code. Use the 3rd example in Serial Input Basics

...R

Nope, I'm trying to send 2 variables to the slave, with a delay between them, and the arduino that's connected to the slave just uses the data to turn on and off a led.

The Serial.write(c); line is just a proof that the conditional " if (BT.available() > 0) " it's working (it is not)

I added this to the slave loop:

Serial.print("Serial STATE:");
Serial.println(Serial.available());
Serial.print("Bluetooth STATE:");
Serial.println("BT.available());

Mmmhm, I swear I posted the pictures.... mmmhm

Links:
master Dropbox - Error - Simplify your life
Slave: Dropbox - Error - Simplify your life
modules: Dropbox - Error - Simplify your life

fran_a:
Nope, I'm trying to send 2 variables to the slave, with a delay between them,

Then don't complicate matters with irrelevant READs on the master.

Why not send the data with mySerial.println('a'); which can easily be read by the second example in Serial Input Basics or with mySerial.println(""); which can be read by the 3rd example and is more reliable.

...R

Scrap it all to basics:

Try this on the master:

#include <SoftwareSerial.h>

//send a byte with value 1
byte byte_to_send = B00000001;

SoftwareSerial mySerial(2, 3);

void setup() {

  mySerial.begin(9600);

}

void loop() {


  mySerial.print(byte_to_send);
  delay(2000);

}

And this on the slave:

#include <SoftwareSerial.h>

char storage_array[1];

SoftwareSerial mySerial(2, 3);

void setup() {

  mySerial.begin(9600);
  Serial.begin(9600); //For PC Debug
}

void loop() {
  
//If the number of bytes in serial buffer is more than 0...
if (mySerial.available()>0){ 
  
  storage_array[1]=mySerial.read();
 
 Serial.println(storage_array); 
}
  

}

Guys, thanks to you I've learned a lot. I managed to enter to the FULL AT mode on the zs-040. I restored all the settings to the factory defaults, and change the CMODE to 1. Then I link them. I wasn't connecting the ZS with a voltage divider between the RX pin on the BT and the pin 3 on the ARDUINO UNO.

It worked just fine... and, thanks to the examples Robin gave to me, I could finally understand why I was "complicating matters".... I've got a lot to learn.

Then again, thanks guys.