The problem of sending data of the ultrasonic sensor to the computer through Bluetooth.
I would like to send the data of the ultrasonic sensor to my computer through Bluetooth and watch it the serial port. However, when I open the serial port it do not show anything.:
Here is the code:
#include <SoftwareSerial.h>
int In1 = 22; //right motor
int In2 = 23;
int In3 = 24; //left motor
int In4 = 25;
int ENA = 10;
int ENB = 9;
int trigPin1 = 11; // Trigger1
int echoPin1 = 51; // Echo1
int trigPin2 = 12; // Trigger2
int echoPin2 = 52; // Echo2
int trigPin3 = 13; // Trigger3
int echoPin3 = 53; // Echo3
SoftwareSerial BluetoothSerial(0, 1); //RX TX
long duration, distance, leftSensor, frontSensor, rightSensor;
void setup()
{
Serial.begin (9600);
BluetoothSerial.begin(57600);
pinMode(0,INPUT);
pinMode(1,OUTPUT);
pinMode(In1,OUTPUT); //right motor
pinMode(In2,OUTPUT);
pinMode(In3,OUTPUT); //left motor
pinMode(In4,OUTPUT);
pinMode(ENA,OUTPUT);
pinMode(ENB,OUTPUT);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
BluetoothSerial.print("Starting ...");
}
void loop()
{SonarSensor(trigPin1, echoPin1);
leftSensor = distance;
SonarSensor(trigPin2, echoPin2);
frontSensor = distance;
SonarSensor(trigPin3, echoPin3);
rightSensor = distance;
BluetoothSerial.print("leftSensor: ");
BluetoothSerial.print(leftSensor);
BluetoothSerial.print(" cm ");
BluetoothSerial.print(" - ");
BluetoothSerial.print("frontSensor: ");
BluetoothSerial.print(frontSensor);
BluetoothSerial.print(" cm ");
BluetoothSerial.print(" - ");
BluetoothSerial.print("rightSensor: ");
BluetoothSerial.print(rightSensor);
BluetoothSerial.print(" cm ");
BluetoothSerial.println();
delay (20) ;
if (frontSensor > 30) { forward() ;} //if front obstacle distance > 30cm, car move forward
else if (frontSensor <= 30 && frontSensor >= 5) //if front obstacle distance <= 30cm and >=5cm
{
if (rightSensor >= 25 && rightSensor > leftSensor) { rightturn() ; delay(1000) ; forward () ; } //if right obstacle distance >= 25cm and right obstacle distance larger than left obstacle distance.
else if (leftSensor >= 25 && leftSensor > rightSensor) { leftturn() ; delay(1000) ; forward () ;} //if left obstacle distance >= 25cm and left obstacle distance larger than right obstacle distance.
else {stop(); }
}
else { stop() ;}
}
void SonarSensor(int trigPin,int echoPin)
{digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW); // Write a pulse to the HC-SR04 Trigger Pin
duration = pulseIn(echoPin, HIGH); // Measure the response from the HC-SR04 Echo Pin
distance = (duration / 2) * 0.034; // Determine distance from duration
}
void forward() // car move forward
{
analogWrite(ENA, 250); // Sets speed variable via PWM
analogWrite(ENB, 250); // Sets speed variable via PWM
digitalWrite(In1, LOW);
digitalWrite(In2, HIGH);
digitalWrite(In3, HIGH);
digitalWrite(In4, LOW);
}
void stop() // car stop
{
analogWrite(ENA, 250); // Sets speed variable via PWM
analogWrite(ENB, 250); // Sets speed variable via PWM
digitalWrite(In1, LOW);
digitalWrite(In2, LOW);
digitalWrite(In3, LOW);
digitalWrite(In4, LOW);
}
void leftturn() //car turn right
{
analogWrite(ENA, 250); // Sets speed variable via PWM
analogWrite(ENB, 250); // Sets speed variable via PWM
digitalWrite(In1, LOW);
digitalWrite(In2, HIGH);
digitalWrite(In3, LOW);
digitalWrite(In4, LOW);
}
void rightturn() // car turn right
{
analogWrite(ENA, 250); // Sets speed variable via PWM
analogWrite(ENB, 250); // Sets speed variable via PWM
digitalWrite(In1, LOW);
digitalWrite(In2, LOW);
digitalWrite(In3, HIGH);
digitalWrite(In4, LOW);
}
void backward() // motor move backward
{
analogWrite(ENA, 250); // Sets speed variable via PWM
analogWrite(ENB, 250); // Sets speed variable via PWM
digitalWrite(In1, HIGH);
digitalWrite(In2, LOW);
digitalWrite(In3, LOW);
digitalWrite(In4, HIGH);
}
