I am having a project am trying to use an ultrasonic sensor to disable and enable bluetooth master-slave communication. I need the slave to have the ultrasonic sensor so that when an object is detected infront,the bluetooth slave is disabled so cannot stream,transfer data or communicate with the master. I would like your views and some guidance on how i could go about it.
What is the difficulty? Knowing that, helpers know what guidance You need.
Assuming that the Bluetooth module has a serial input/output. You could put the statements that write and read the Bluetooth modules serial signals into functions that can only execute if the distance is grater than your threshold.
Your duplicate topic on the same subject has been deleted
Why did you start a second one ?
groundFungus:
Assuming that the Bluetooth module has a serial input/output. You could put the statements that write and read the Bluetooth modules serial signals into functions that can only execute if the distance is grater than your threshold.
I tried to create a code that could activate the module when an object is not detected but the problem comes at disabling the serial port to which the bluetooth module has been attached to.
Also I am using the Arduino mega because of the number of hardware i inted to incoorporate in the system.So my issue comes at disabling the serial port to which the bluetooth module has been connected to.
Here is a sample code that I need to either enable the Bluetooth module when the object is not detected .. I would like your views and some input suggestions to come up with a statement that can be able to disable the Serial3 port that I intend to attach the Bluetooth module.
#include <NewPing.h>
#include <Wire.h>
#define TrigPin 11
#define EchoPin 12
const int maxDistance = 100;
NewPing ultrasonic(TrigPin, EchoPin, maxDistance);
const char GREEN = 's'; // enable sent data
const char RED = 'd'; // disable sent data
char bluetoothAction;
const int MIN_DISTANCE_FROM_OBJECT = 5;
const float DISTANCE_OFFSET = -0.31;
void setup() {
Serial.begin(115200);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
Serial3.begin(9600);
}
void loop() {
if (Serial.available() > 0)
{
char serial = Serial.read();
if (serial == GREEN || serial == RED)
{
bluetoothAction = serial;
}
}
if (bluetoothAction == GREEN)
{
//your views about this statement to enable serial port connected to the bluetooth module
Serial3.available();
}
else if (bluetoothAction == RED)
{
//i need the statement that will disable the serial port on arduino mega that is connected to the bluetooth module.
//need the statement here
Serial3.()
}
void check(){
// if (obstacledetected()) Serial3.();
while (obstacleDetected())delay (50);
if (Serial.available()>0)
return;
}
boolean obstacleDetected()
{
int distance = ultrasonic.ping_cm();
return distance > 0 && distance <= MIN_DISTANCE_FROM_OBJECT;
}
}
No need to actively disable the Serial port.
Just don't send anything to it and ignore whatever data is coming in while it is in "disable" mode.
wvmarle:
No need to actively disable the Serial port.Just don't send anything to it and ignore whatever data is coming in while it is in "disable" mode.
what statement would u suggest then?
wvmarle:
No need to actively disable the Serial port.Just don't send anything to it and ignore whatever data is coming in while it is in "disable" mode.
have made some editings to the code..but am receiving some error expected declaration before '}' token
any ideas on how to fix the issue
#include <NewPing.h>
#include <Wire.h>
#define TrigPin 11
#define EchoPin 12
const int maxDistance = 100;
NewPing ultrasonic(TrigPin, EchoPin, maxDistance);
const char GREEN = 's'; // sent data
const char RED = 'd'; // dont sent data
char bluetoothAction;
const int MIN_DISTANCE_FROM_OBJECT = 5;
const float DISTANCE_OFFSET = -0.31;
void setup() {
Serial.begin(115200);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
Serial3.begin(9600);
}
void loop() {
if (Serial.available() > 0)
{
char serial = Serial.read();
if (serial == GREEN || serial == RED)
{
bluetoothAction = serial;
}
}
if (bluetoothAction == GREEN)
Serial3.begin(9600);
else if (bluetoothAction == RED)
Serial3.end();
}
void check(){
if (obstacleDetected()) Serial3.end();
while (obstacleDetected())delay (50);
if (Serial.available()>0)
return;
}
boolean obstacleDetected()
{
int distance = ultrasonic.ping_cm();
return distance > 0 && distance <= MIN_DISTANCE_FROM_OBJECT;
}
}
}
What is the purpose of the 2 braces at the end of the program ?
Do an autoformat - it'll help you a lot to see where the extraneous (or missing) braces are.