I have a project where we need to create a virtual cane for blind people and it will have 3 functions:
Pressure button when pressed will connect to an android phone through HC-06 Bluetooth modulo to an app that I developed and will can to someone that I defined
Temperature sensor that is attached to a buzzer that will buzz if the temperature is higher than 40 Degrees
Ultrasonic sensor attached to and vibration motor that will vibrate if there is any object less than 70 cm from the person.
When I combine the code from the ultrasonic and the temperature sensor, everything works fine, when I add the bluetooth code, nothing works, anyone see the problem? Or could you show me the problem and solveit?
Code without Bluetooth:
int DHT11_PIN=8;
dht DHT;
int buzzer=6;
int trigPin=3;
int echoPin=2;
int motor=7;
void setup(){
Serial.begin(115200);
pinMode(DHT11_PIN,INPUT);
pinMode(buzzer, OUTPUT);
delay(5000);
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(motor, OUTPUT);
delay(5000);
}
void loop(){
Stemperature();
Sultrassom();
}
// sensor de tempratura e Buzzer-------------------------------
void Stemperature(){
digitalWrite(buzzer,LOW);
DHT.read11(DHT11_PIN);
float t=DHT.temperature;
Serial.print("Temperature = ");
Serial.println(t);
if (t>40){
digitalWrite(buzzer,HIGH);
{tone(6,100,20);
delay(1);
noTone(1);
delay(1);
}
delay(1000);
Serial.print("ON T=");
}else{
digitalWrite(buzzer,LOW);
Serial.print("OF T=");
}
}
//Sensor de Ultrassom e Motor vibração-----------------
void Sultrassom(){
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) * 0.0340;
Serial.println(distance);
if (distance < 70){ // se a distância for menor que 70 cm o motor vibra
digitalWrite(motor,HIGH);// quando a distância for menor que 70 cm
Serial.print("MOTOR ON=");
}else{
digitalWrite(motor,LOW);// quando a distância for maior que 70 cm
Serial.print("MOTOR OFF=");
}
delay(5);
}
Bluetooth Code:
const int buttonPin = 4; // the number of the pushbutton pin
boolean buttonState; // variable for reading the pushbutton status
void setup(){
Serial.begin(9600);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
buttonState = LOW;
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if(buttonState == HIGH) {
// send char 1 via Bluetooth:
Serial.println("1");
delay(20000);
while (0);
}
}
-
What have you done already? Have you traced the execution? What is the last statement executed?user31481– user314812017年10月11日 10:04:47 +00:00Commented Oct 11, 2017 at 10:04
2 Answers 2
In the bluetooth loop you use:
delay(20000);
while (0);
This means you first wait 20 seconds, and while(0) does not do anything (unuseful instruction).
Actually, this results when the button is pressed, it starts the loop again, but you have to merge both codes into one.
I would add the sending of the serial command in the code above, remove the while and see what happens. If you need some delay for debouncing reaons for the button, implement a decent deboucing algorithm instead of the delay of 20000.
And why there are two }} at the end?
-
4
while(1);
means wait foreverwhile(0);
means never wait, which is pointless.Code Gorilla– Code Gorilla2017年10月11日 10:18:46 +00:00Commented Oct 11, 2017 at 10:18 -
True ... I will change itMichel Keijzers– Michel Keijzers2017年10月11日 10:49:24 +00:00Commented Oct 11, 2017 at 10:49
-
1No - zero is false :)Code Gorilla– Code Gorilla2017年10月11日 12:43:59 +00:00Commented Oct 11, 2017 at 12:43
-
Adapted it more clearlyMichel Keijzers– Michel Keijzers2017年10月11日 13:07:37 +00:00Commented Oct 11, 2017 at 13:07
The following code is not tested in any way. It should give you some help, to detect the problems. First you used the same Serial connection for your PC and the Bluetooth module, which leads to problems. Second, you had some delays, which seemed useless. Third your noTone()
function didn't matched the right pin. Also I added an Interrupt for the Button. It could be, that your DHT reading need the same Interruptprotection, which I added to the Usonic measurment. Last but not least I replaced your Pin definitions to make it more readable.
// New Include for the bluetooth connection
#include <SoftwareSerial.h>
#include <dht.h>
#define DHT11PIN 8
#define BTRXPIN 10 // Pay attention to your wiring
#define BTTXPIN 11
#define BUZZERPIN 6
#define TRIGGERPIN 5 // Changed, to keep an interrupt Pin free !!!
#define ECHOPIN 4 // Changed, to keep an interrupt Pin free !!!
#define MOTORPIN 7
#define BUTTONPIN 2 // Changed to an interrupt Pin
dht DHT;
SoftwareSerial bluetooth(BTRXPIN, BTTXPIN);
void setup(){
Serial.begin(9600); // Only once
bluetooth.begin(9600); // Serial connection for the bluetooth module
// Check your needed Baudrate
pinMode(DHT11PIN, INPUT);
pinMode(BUTTONPIN, INPUT);
pinMode(ECHOPIN, INPUT);
pinMode(TRIGGERPIN, OUTPUT);
pinMode(BUZZERPIN, OUTPUT);
pinMode(MOTORPIN, OUTPUT);
digitalWrite(BUZZERPIN, LOW);
attachInterrupt(digitalPinToInterrupt(BUTTONPIN), buttonISR, RISING); // I did not pay attention to bouncing here!!
// I see no need for 5sec delay, so i cut it out
}
void loop(){
temperatureMeasure();
ultraSonicMeasure();
delay(2000); // 2 sec delay, so it won't measure all the time.
}
float temperatureMeasure(){ // Added return types
DHT.read11(DHT11_PIN);
float t = DHT.temperature;
Serial.print("Temperature = ");
Serial.println(t);
if (t > 40){
tone(BUZZERPIN,100);
delay(20);
noTone(BUZZERPIN);
Serial.print("ON");
}else{
noTone(BUZZERPIN);
Serial.print("OFF");
}
return temperatureMeasure;
}
long ultraSonicMeasure(){
long duration{0};
long distance{0};
noInterrupts();
digitalWrite(TRIGGERPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGERPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGERPIN, LOW);
duration = pulseIn(ECHOPIN, HIGH);
interrupts();
distance = (duration/2) * 0.0340;
Serial.println(distance);
if (distance < 70){
digitalWrite(MOTORPIN, HIGH);
Serial.print("MOTOR ON");
}else{
digitalWrite(MOTORPIN, LOW);
Serial.print("MOTOR OFF");
}
return distance;
}
// Called when button is pressed
void buttonISR(){
bluetooth.println("1"); // Would be better, to put this outside of the ISR, but is is only on character.
}
-
It worked with some modifications!!! Thank you :)Josh Miles– Josh Miles2017年10月13日 13:09:08 +00:00Commented Oct 13, 2017 at 13:09