Trying to make a bluetooth connection between an ultrasonic sensor and a buzzer

This is for a school project to my knowledge all the wiring is correct and both bluetooth modules are connected its just the code that isn't quite right. Guidance and help would be much appreciated. Thank you
Updated: I've posted my partners original code as well both don't work.

Back ground:
I am using a HC-05 and HC-06 because i couldn't get 2 HC_05s to pair though i did all the right steps
I am also using 1 arduino uno and 1 arduino nano.
Mine:

Master code:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(3, 2);

const int trigPin = 7; // ultrasonic trigger on pin 7
const int echoPin = 6; // ultrasonic echo on pin 6


void setup() {
  Serial.begin (9600); // set up serial communication
   mySerial.begin(38400);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  long duration, distance; // define variables
  int sound; // frequency of the buzzer
  // generate the 10us pulse as the trigger signal according to the data sheet
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // duration is the time in us from sending the signal to receiving the echo
  duration = pulseIn(echoPin, HIGH);
  
  // distance is the distance in cm from the object to the sensor
  // use the equation from the data sheet: distance(cm) = duration(us)/58
  distance = duration/58;

 // define different sound frequency based on distance  
  if (distance > 0 && distance < 30) { // && represents AND
    mySerial.println("a");
 
   if (distance >= 30 || distance <= 0){ // || represents OR
    Serial.println("Out of range"); // print to IDE serial monitor
  }
  else {
    Serial.print(distance); // print to IDE serial monitor
    Serial.println(" cm");
  }
  
  // have to wait at least 60ms before the next measurement 
  // according to the data sheet
  delay(500); 
 }
}

Slave Code:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(3, 2);
const int buzzer = 3;
void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);
  mySerial.begin(38400);

  pinMode(buzzer, OUTPUT);
}

void loop() {
// define variables
  int sound; // frequency of the buzzer
  // generate the 10us pulse as the trigger signal according to the data sheet
  // Receiving
  if (mySerial.available())
    Serial.write(mySerial.read());

  if (mySerial.read() == 'a')
  {
  sound = 2000;
  
  }
  else
  {
    digitalWrite(buzzer, LOW);
  }

 
}

My Partners:
Master:

#include <SoftwareSerial.h> //include library
#define tx 3
#define rx 2
SoftwareSerial Bluetooth(tx,rx); //TX || RX

const int trigPin = 7; //ultrasonic trigger on pin 7 
const int echoPin = 6; //ultrasonic echo on pin 6  
 
void setup() { 
  Serial.begin (9600); //set up serial communication 
  Bluetooth.begin (38400); //set up bluetooth communication
  pinMode(trigPin, OUTPUT); //set trigpin as output
  pinMode(echoPin, INPUT); //setechopin as input
  pinMode(tx,OUTPUT); //set txpin as ouput
  pinMode(rx,INPUT); //set rxpin as input
  
} 
 
void loop() { 
  long duration, distance; //define variables 
  int sound; //frequency of the buzzer 
  duration = pulseIn(echoPin, HIGH); //duration is the time in us from sending the signal to receiving the echo 
  distance = duration/58; //convert to distance 
  
   if (distance >= 20.32 || distance <= 0){ // || represents OR 
    Serial.println(-1); //negative number will let buzzer know not to sound 
  } 
  else { 
    Serial.print(distance); //prints distance to serial monitor
  } 

  if(Serial.available()>0){
    Bluetooth.print(Serial.read()); //sends bluetooth signal
  }

  delay(50); //slight delay in between measurements 

}

Slave:

#include <SoftwareSerial.h>
#define tx 3
#define rx 2
SoftwareSerial Bluetooth(10,11);





const int buzzer = 5; //buzzer on pin 3 
 
void setup() { 
  Serial.begin (9600); //set up serial communication 
  Bluetooth.begin (38400); //set  up blueooth communication
  pinMode(buzzer, OUTPUT); //define buzzer pin as output
  pinMode(tx,OUTPUT); //define tx pin as output
  pinMode(rx,INPUT); //define rx pin as input

}

void loop() {
  long distance; //define variables
  int sound; //define variables
   
if(Bluetooth.available()){
  Serial.print(Bluetooth.read()); //receives bluetooth signal and sends to serial monitor 
}

if(Serial.available() > 0){ //takes distance and defines sound frequency off of that 
  distance = Serial.read();
    if (distance > 0 && distance <= 20.32) { 
    sound = 1500; 
  } else if (distance > 0 && distance <= 15.24) { 
    sound = 1600; 
  } else if (distance > 0 && distance <= 10.16) { 
    sound = 1800; 
  } else if (distance > 0 && distance <= 5.08) { 
    sound = 2000; 
  } else if (distance > 0 && distance <= 2.54) { 
    sound = 2200; 
  } else { 
    sound = 0; 
  }   
}

tone(buzzer,sound); //sounds buzzer


}

What happens when duration is zero? That is what is returned of pulseln() times out after 1 second.

In the original code it would display out of range. I got rid of it since I thought it wasn't necessary because I don't care about the distance I only want the buzzer to go off when it reads a distance of any kind.

mySerial.println("a");

This line of code is sending the character 'a' and then a carriage return ('\r') and new line ('\n') character.

What do you think happens with the buzzer when the carriage return and new line characters are read by the receiving code.

if (mySerial.read() == 'a')
{
sound = 2000;

}
else
{
digitalWrite(buzzer, LOW);
}

For best results in getting help on the forum you should post your code as a "code section" which is formatted and can be easily copied.

https://forum.arduino.cc/t/make-experienced-users-help-two-ways-to-quickly-post-code-as-a-code-section/937298

So I'm kind lost when it comes to coding. Im more of the wiring and designing type of guy. My partner who was programing had to hand the project over to me because he couldn't finish it and a lot of his could wasn't working I was thinking when 'a' is sent then the buzzer would go off. I can also post the original code that he made.

Ok, but replace that with a display of the actual microsecond time being returned so you know that part is working.

Okay

Thanks for revising the posting of your code. :+1:

I would simplify your task by breaking it down into smaller pieces. I would work with the code you have written as the starting point, as you will understand that better than what the previous partner wrote.

You have a distance sensing piece, and a buzzer operating piece. Get them both working independently with Serial output and Serial input. Forget about blue tooth for now, as that is just serial without wires and will be simple to add at the end.

Get the distance reading portion working as required with Serial output to the monitor.
Get the buzzer portion working as required with input from the Serial monitor to trigger the buzzer. Enter the 'a' from the monitor with no line endings.

When you have those two codes working, you can switch from input and out put from/to the monitor with hardware serial to using software serial. You can connect the two Arduinos with wires between the software serial pins. This will verify the code.

Then when everything is working correctly replace the wired connection with bluetooth.

So I have been working off of instructor provide code that provide that basic function. Do you suggest that I start from scratch instead?

Intructors code:

const int trigPin = 7; // ultrasonic trigger on pin 7
const int echoPin = 6; // ultrasonic echo on pin 6
const int buzzer = 3; // buzzer on pin 3

void setup() {
  Serial.begin (9600); // set up serial communication
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buzzer, OUTPUT);
}

void loop() {
  long duration, distance; // define variables
  int sound; // frequency of the buzzer
  // generate the 10us pulse as the trigger signal according to the data sheet
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // duration is the time in us from sending the signal to receiving the echo
  duration = pulseIn(echoPin, HIGH);
  
  // distance is the distance in cm from the object to the sensor
  // use the equation from the data sheet: distance(cm) = duration(us)/58
  distance = duration/58;

 // define different sound frequency based on distance  
  if (distance > 0 && distance < 5) { // && represents AND
    sound = 2000;
  } else if (distance >= 5 && distance < 10) {
    sound = 1900;
  } else if (distance >= 10 && distance < 15) {
    sound = 1800;
  } else if (distance >= 15 && distance < 20) {
    sound = 1700;
  } else if (distance >= 20 && distance < 25) {
    sound = 1600;
  } else if (distance >= 25 && distance < 30) {
    sound = 1500;
  } else {
    sound = 0;
  }  
 
   if (distance >= 30 || distance <= 0){ // || represents OR
    Serial.println("Out of range"); // print to IDE serial monitor
    noTone(buzzer); // stop the tone
  }
  else {
    Serial.print(distance); // print to IDE serial monitor
    Serial.println(" cm");
    tone(buzzer, sound); // sound the buzzer at the specified frequency in Hz
  }
  
  // have to wait at least 60ms before the next measurement 
  // according to the data sheet
  delay(500); 
} 

No. If you were provided that code and understand it then go ahead and use what you want from it.

What level of course are you taking? What Arduino work has been done in that course so far?

Suggest you continue with what you have. Comment out all the code dealing with the buzzer until you get the distance correct or within reason and then work with the buzzer part of the code.

It's an intro to engineering course at ASU. I have very little experience with Arduino personally. I have been the one to design and wire the projects while i have been leaving the coding to him.

The distance isn't the issue it is nice to have to get instance feed back but I want to just purely get a detection signal and send that to the buzzer to go off. I struggle with this stuff and am very lost right now. lol

Forget about the sending for a moment. If you run the professors code as provided, can you get the buzzer to sound for distances less than 30 cm?

Yes

Yes

Great. Your original master code should be easy to fix.

Let's confirm that if you change the output from software serial to hardware serial and the monitor you see what you expect to send to the other Arduino. Since you only want to send a single character over bluetooth, its best to separate the newline/carriage return.

if (distance > 0 && distance < 30) { // && represents AND
   // mySerial.println("a");
     Serial.print("a"); //print a single character
     Serial.println();

I changed what you suggested and still not getting output.

OK. I guess we'll have to back up a bit and go slower. Can you modify the instructors code you said was working properly in Post #15 to eliminate the output you don't want and just have the output you do.

Post your best effort to do that and if it is not working correctly, please explain what the code is not doing what you want, and what it is doing that you don't want.

Sorry I am getting a distance output but the buzzer is not going off.

There is no buzzer in the original master code. See if you can patch it in.

Post what you are trying.