I can receive data from android but cant send data to android, please help me to solve. The below is my code.
Board I used is arduino uno and external module is HC-06.
#include <SoftwareSerial.h>
int bluetoothTx = 13; // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 14; // RX-I pin of bluetooth mate, Arduino D3
char incomingByte; // incoming data
int sensorPin = A0;
int ledPin = 11; // select the pin for the LED
int ledPin2 = 12;
int sensorValue = 0; // variable to store the value coming from the sensor
int rTrigPin = 6;
int rEchoPin = 7;
int trigPin = 9;
int echoPin = 10;
int lTrigPin = 4;
int lEchoPin = 5;
// defines variables
long rDuration;
int rDistance;
long duration;
int distance;
long lDuration;
int lDistance;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup() {
Serial.begin(115200);
bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps
//bluetooth.print("$"); // Print three times individually
//bluetooth.print("$");
//bluetooth.print("$"); // Enter command mode
//delay(100); // Short delay, wait for the Mate to send back CMD
//bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
// 115200 can be too fast at times for NewSoftSerial to relay the data reliably
bluetooth.begin(9600); // Start bluetooth serial at 9600
pinMode(rTrigPin, OUTPUT); // Sets the rTrigPin as an Output
pinMode(rEchoPin, INPUT); // Sets the rEchoPin as an Input
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(lTrigPin, OUTPUT); // Sets the lTrigPin as an Output
pinMode(lEchoPin, INPUT); // Sets the lEchoPin as an Input
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
/*void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, LOW);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPin, HIGH);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}*/
void loop()
{
digitalWrite(rTrigPin, LOW);
delayMicroseconds(2);
digitalWrite(rTrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(rTrigPin, LOW);
rDuration = pulseIn(rEchoPin, HIGH);
rDistance = (rDuration / 2) / 29.1;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
digitalWrite(lTrigPin, LOW);
delayMicroseconds(2);
digitalWrite(lTrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(lTrigPin, LOW);
lDuration = pulseIn(lEchoPin, HIGH);
lDistance = (lDuration / 2) / 29.1;
sensorValue = analogRead(sensorPin);
if (rDistance <= 221 || distance <= 221 || lDistance <= 221)
{
if (sensorValue <= 140)
{
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, LOW);
}
else
{
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin2, HIGH);
}
}
else if (rDistance > 221 || distance > 221 || lDistance > 221)
{
if (sensorValue <= 140)
{
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, LOW);
}
else
{
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin2, HIGH);
}
}
if (bluetooth.available()) // If the bluetooth sent any characters
{
// Send any characters the bluetooth prints to the serial monitor
Serial.println((char)bluetooth.read());
incomingByte = bluetooth.read();
if(incomingByte == '1') {
Serial.println("Music is pause and now you are in dangerous situation."); // print message
//bluetooth.print("1");
//delay(1000);
}
if(incomingByte == '0') {
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin2, HIGH);
Serial.println("Alert signal OFF and music would not pause when in dangerous situation."); // print message
//bluetooth.print("0");
//delay(1000);
//break;
}
}
if (Serial.available()>0) // If stuff was typed in the serial monitor
{
int bytes=Serial.available();
//Serial.readBytes(buffer, startPosition, bytes);
bluetooth.print((char)Serial.read());
}
}