Hi all,
I'm having some difficulty getting my bluetooth modules to send information to eachother. One is configured as master and one as slave, they are connected as seen from the lights flashing every 2 seconds or so. They are configured the same default baud rate of 9600.
The project is to measure a fluid level as distance using an ultrasound sensor, and send the data to the other arduino which displays it on a computer. Attached is the code I've been trying to use.
Distance measuring arduino
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(2,3);
int trigger_pin = 7;
int echo_pin = 6;
float flightTime;
float distance;
void setup() {
// put your setup code here, to run once:
BTSerial.begin(9600);
pinMode (trigger_pin, OUTPUT);
pinMode (echo_pin, INPUT);
}
void loop() {
digitalWrite (trigger_pin, HIGH);
delayMicroseconds (10);
digitalWrite (trigger_pin, LOW);
flightTime = pulseIn(echo_pin, HIGH);
distance = (flightTime * 0.343) / 2;
BTSerial.print(distance);
BTSerial.print("#");
delay(500);
}
Printing to computer arduino
const byte numDigits = 5;
char rc; // rc = Received Character
char recvChars[numDigits]; // Array for number received
boolean newData = false; // Internal print switch
void setup() {
Serial.begin(38400); // PC Communication
Serial1.begin(9600); // Bluetooth connection to other arduino
Serial.println("<Arduino is ready>");
}
void loop() {
recvData(); // Function interpretting incoming series
printData(); // Function printing if new data present
}
//================== Functions Below ===============================
void recvData(){
static byte n = 0;
char endMarker = '#'; // Signifying end of data piece
char rc;
if (Serial1.available() > 0) {
rc = Serial1.read();
if (rc != endMarker) {
recvChars[n] = rc;
n++;
if (n >= numDigits) {
n = numDigits-1;
}
}
else {
recvChars[n] = '\0'; // end string
n = 0;
newData = true;
}
}
}
void printData(){
if (newData == true) {
int dataInt = 0;
dataInt = atoi(recvChars); // COnverting function from char to int
Serial.print("Height of missing fluid: ");
Serial.print(dataInt);
Serial.println(" mm");
newData = false;
}
}
The code did work when using an Arduino Mega (Data hub) and Arduino Uno (FMD), when wired directly to eachothers Serial ports. But when expanding to the HC-05 module it doesn't work, even when wired to pins 0 and 1.
The aim is to get the ultrasound sensor on an arduino nano sending the data, with an arduino mega receiving it.
All help is welcomed, thanks
fmd_code.ino (635 Bytes)
data_hub.ino (1.48 KB)