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
}