Hello all,
I have a simple project, but I am still working out the kinks on the Bluetooth communication.
Project: 2 Arduino Nanos each with Bluetooth HC-05. One of the Nanos also has an Ultrasonic HR-SR04. The goal is to have the ultrasonic Nano send the data via Bluetooth to the other Nano where that data can be used for the rest of the project.
I have paired and bind the Bluetooth modules and the ultrasonic Nano has the master Bluetooth while the receiver Nano has the Slave Bluetooth. They seem to be paired with synchronized blinking.
The Master code looks something like this:
//Bluetooth Nano Master
//Sends the ultrasonic distance to the Slave
#include <SoftwareSerial.h>
SoftwareSerial BTserial(2,3); // RX | TX
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
BTserial.begin(38400);
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
// Send the distance via Bluetooth
BTserial.write(distance);
}
Question 1: Is this the right way to send the results of the ultrasonic data via Bluetooth?
BTserial.write(distance);
Here is the Receiver Nano
//Bluetooth Nano Slave
//Receives the ultrasonic distance from the Master
#include <SoftwareSerial.h>
SoftwareSerial BTserial(2,3); // RX | TX
void setup() {
Serial.begin(9600);
BTserial.begin(38400);
}
void loop() {
if (BTserial.available()) {
Serial.write(BTserial.read());
Serial.println(BTserial.read());
}
if (Serial.available()) {
BTserial.write(Serial.read());
Serial.println(BTserial.read());
}
}
Question 2: What is the correct way to output what a Bluetooth module receives for working in a script and outputting to the Serial Monitor for debugging?
As a confirmation of this working, I am opening the Serial Monitor with the Slave and expecting to see the distance reading from the Master, but it isn't working.
I am just getting started with Arduinos with this project and I can find many instructions on binding HC-05s and using HC-05s and Android, but I am having difficulties with Arduino-Arduino. Any help would be great!