-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensors.ino
108 lines (86 loc) · 2.39 KB
/
sensors.ino
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <Servo.h>
// Define pin numbers
#define TRIG_PIN 2
#define ECHO_PIN 3
#define PIR_PIN 4
#define BUZZER_PIN 5
#define SERVO_PIN 6
// Define servo parameters
#define SERVO_MIN_ANGLE 15
#define SERVO_MAX_ANGLE 165
#define SERVO_DELAY_MS 30
// Define radar parameters
#define RADAR_SWEEP_DELAY_MS 20
#define RADAR_PULSE_DURATION_US 10
// Define PIR parameters
#define PIR_DELAY_MS 200
// Initialize servo and sensors
Servo servo;
long duration, distance;
int pir_state = LOW;
void setup() {
// Initialize serial communication for debugging
Serial.begin(9600);
// Initialize sensors
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(PIR_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
servo.attach(SERVO_PIN);
// Move servo to starting position
servo.write(SERVO_MIN_ANGLE);
delay(SERVO_DELAY_MS);
}
void loop() {
// Sweep servo back and forth
for (int angle = SERVO_MIN_ANGLE; angle < SERVO_MAX_ANGLE; angle++) {
// Move servo to current angle
servo.write(angle);
delay(SERVO_DELAY_MS);
// Get distance from ultrasonic sensor
distance = getDistance();
// Check if human is detected by PIR sensor
pir_state = digitalRead(PIR_PIN);
if (pir_state == HIGH) {
// Play sound through buzzer if human is detected
tone(BUZZER_PIN, 2000, 1000);
}
// Print debug information
Serial.print(angle);
Serial.print(",");
Serial.print(distance);
Serial.print(".");
}
for (int angle = SERVO_MAX_ANGLE; angle > SERVO_MIN_ANGLE; angle--) {
// Move servo to current angle
servo.write(angle);
delay(SERVO_DELAY_MS);
// Get distance from ultrasonic sensor
distance = getDistance();
// Check if human is detected by PIR sensor
pir_state = digitalRead(PIR_PIN);
if (pir_state == HIGH) {
// Play sound through buzzer if human is detected
tone(BUZZER_PIN, 2000, 1000);
}
// Print debug information
Serial.print(angle);
Serial.print(",");
Serial.print(distance);
Serial.print(".");
}
}
int getDistance() {
// Send ultrasonic pulse
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(RADAR_PULSE_DURATION_US);
digitalWrite(TRIG_PIN, LOW);
// Measure pulse duration
duration = pulseIn(ECHO_PIN, HIGH);
// Calculate distance
distance = duration*0.034/2;
// Return distance in centimeters
return distance;
}