-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhrc_bluetooth.ino
More file actions
35 lines (27 loc) · 834 Bytes
/
hrc_bluetooth.ino
File metadata and controls
35 lines (27 loc) · 834 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <SoftwareSerial.h>
const int trigPin = 9; // Trigger pin for HC-SR04
const int echoPin = 10; // Echo pin for HC-SR04
SoftwareSerial bluetooth(2, 3); // RX, TX for Bluetooth module
void setup() {
Serial.begin(9600);
bluetooth.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// Trigger the ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the echo duration
unsigned long duration = pulseIn(echoPin, HIGH);
// Calculate distance in centimeters
int distance = duration * 0.034 / 2;
// Send distance over Bluetooth
bluetooth.print("Distance: ");
bluetooth.print(distance);
bluetooth.println(" cm");
delay(1000); // Adjust delay as needed
}