0

I successfully connected two Bluetooth modules, HC-05 and HC-06 like on this website:

http://www.martyncurrey.com/connecting-2-arduinos-by-bluetooth-using-a-hc-05-and-a-hc-06-pair-bind-and-link/

I have an Arduino Nano that's the slave (HC-06) and an Arduino Mega that is the master (HC - 05) and there's is the code for both of them:

Reveiver - Master

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX | TX
String Data = "";
void setup() {
    Serial.begin(9600);
    mySerial.begin(9600);  
}

void loop(){
    while (mySerial.available()){
        Serial.println(mySerial.read())  
    }
}

Sender - Slave

#include <SoftwareSerial.h>
SoftwareSerial BTserial(11, 10); // RX | TX

int c = 100;

void setup() {
Serial.begin(9600);
BTserial.begin(9600);  
}

void loop(){
     BTserial.println(c);
}

I tried

BTserial.write(c) 

and doesn't work either, I receive numbers like 245,255. And when I change the data that I'm sending the numbers are different, so I know that they are communicating, I think it's a variable problem, but I tried a bunch of codes of the internet that supposedly work, but it doesn't seem to work for this case.

  • use write() to write a byte to Bluetooth and to Serial Monitor – Juraj Apr 19 '18 at 11:02
  • A Mega has 4 hardware serial interfaces. Why are you using software serial? – gre_gor Apr 19 '18 at 14:10
  • look at the bytes sent int send=BTserial.write(c); c is not a single byte, and I am not sure how it reacts to 4 byte int. you could also change it to BTserial.write(100); and see if that changes anything – Chad G Apr 19 '18 at 23:44
1

println prints the int variable c as string "100" with /r/n at the end. And you read it as bytes with read and print the bytes as numbers with new line at the end. So you get ascii values of 1, 0, 0, \r and \n.

use write() to write a byte to Bluetooth and to Serial Monitor

  • How could I solve the problem? If i use the write function do I have the same problem? – Josh Miles Apr 19 '18 at 9:15
  • When forwarding serial traffic through the Arduino you have to use only the read() and write() functions, not the print() functions. – chrisl Apr 19 '18 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.