Hello, I recently started an Arduino project. It includes the ultrasonic sensor to measure the distance. Regarding the distance, Arduino sends the proper text. The Arduino sends the text to Android via Bluetooth. The Android displays the text in a label on MIT App Inventor app.
Because I'm not really good at programming with Arduino, I have problems with sending text.
The Arduino and Android connect to each other, but the text isn't shown on the app.
Here is the Arduino code:
/*Bluetooth data send from Arduino to Andoid
* via Bluetooth
*/
#include <SoftwareSerial.h>
#define RxD 11
#define TxD 10
SoftwareSerial Bluetooth(RxD,TxD);
const int trigPin = 3;
const int echoPin = 2;
long duration, cm;
void setup()
{
//initialize serial communication with HC-05
Serial.begin(9600);
Bluetooth.begin(9600);
pinMode(RxD, INPUT);
pinMode(TxD, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
//The sensor is triggered by a HIGH pulse of 10 or more microseconds
//Give a short LOW pulse beforehand to ensure a clean HIGH pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
//Read the signal from the sensor: a high pulse whose
//duration is the time (in microseconds) from the sending
//of the ping to the reception of its echo off of an object
duration = pulseIn(echoPin, HIGH);
//convert the time into a distance
cm = (duration/2)/29.1;
if(cm < 50)
{
Bluetooth.print("A");
Serial.print("A"); //print on serial monitor
delay(500);
}
else
{
Bluetooth.print("N");
Serial.print("N"); //print on serial monitor
delay(500);
}
}