I was working on my 2 hc-sr04 for gathering centimeters and hc-05 for receiving the said data on my app. I was able to get the data of the 2 sensors however it only shows on 1 field and I was planning to get both showing on the app and on different field. Any idea on how I can get them both? To add with, I used MIT App inventor for creating the app and had included the screenshot of my color blocks.
Here's my code:
#define frontTrigPin 2
#define frontEchoPin 3
#define rearTrigPin 4
#define rearEchoPin 5
long duration, distance, frontsensor, rearsensor;
const unsigned int max_distance = 23200;
void setup() {
Serial.begin (9600);
pinMode(frontTrigPin, OUTPUT);
pinMode(frontEchoPin, INPUT);
pinMode(rearTrigPin, OUTPUT);
pinMode(rearEchoPin, INPUT);
}
void loop() {
makeSensorObject(frontTrigPin, frontEchoPin);
frontsensor = distance;
if(frontsensor >= max_distance) {
Serial.println("Out of range!");
} else {
Serial.print(frontsensor);
Serial.println(" meters");
}
makeSensorObject(rearTrigPin, rearEchoPin);
rearsensor = distance;
if(rearsensor >= max_distance) {
Serial.println("Out of range!");
} else {
Serial.print(rearsensor);
Serial.println(" meters");
}
}
//Object for Ultrasonic Motion Sensor.
void makeSensorObject(int trigPin, int echoPin) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
delay(1000);
}
Thanks in advance!