-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHalloweencircuit.ino
More file actions
63 lines (52 loc) · 1.44 KB
/
Copy pathHalloweencircuit.ino
File metadata and controls
63 lines (52 loc) · 1.44 KB
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
#include <Servo.h>
// Define pin numbers
const int trigPin = 7;
const int echoPin = 6;
const int pirPin = 12;
const int servoPin1 = 5;
const int servoPin2 = 3;
// Create servo objects
Servo servo1;
Servo servo2;
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize ultrasonic sensor pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Initialize PIR sensor pin
pinMode(pirPin, INPUT);
// Attach servo motors to their respective pins
servo1.attach(servoPin1);
servo2.attach(servoPin2);
// Set initial position of servo motors
servo1.write(0);
servo2.write(0);
}
void loop() {
// Check if motion is detected by the PIR sensor
if (digitalRead(pirPin) == HIGH) {
// Measure distance from the ultrasonic sensor
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
// Check if the target is within 60 inches (152.4 cm)
if (distance >= 152.4) {
Serial.print("Target detected at ");
Serial.print(distance);
Serial.println(" cm");
// Rotate servo motors by 90 degrees
servo1.write(90);
servo2.write(90);
delay(1000); // Wait for 1 second
// Reset servo motors to initial position
servo1.write(0);
servo2.write(0);
}
}
}