-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgunshot_code.ino
67 lines (56 loc) · 1.94 KB
/
gunshot_code.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
#include <Servo.h>
const int soundSensorPin = A0; // Sound sensor connected to analog pin A0
const int redLEDPin = 8; // Red LED connected to digital pin 8
const int greenLEDPin = 9; // Green LED connected to digital pin 9
const int servoPin = 12; // Servo motor connected to digital pin 12
const int threshold = 500; // Adjust this threshold value as needed
Servo servoMotor;
bool motorRotating = true; // Flag to indicate if the motor is rotating
void setup() {
pinMode(redLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
pinMode(servoPin, OUTPUT);
servoMotor.attach(servoPin);
Serial.begin(9600);
rotateServoContinuously(); // Start the servo rotation on setup
}
void loop() {
// Read the analog value from the sound sensor
int sensorValue = analogRead(soundSensorPin);
// Check if a clap sound is detected
if (sensorValue > threshold) {
if (motorRotating) {
// If the motor is rotating, stop it
stopServoRotation();
} else {
// If the motor is not rotating, start continuous rotation
rotateServoContinuously();
}
// Toggle the motorRotating flag
motorRotating = !motorRotating;
// Turn on the green LED
digitalWrite(greenLEDPin, LOW);
digitalWrite(redLEDPin, HIGH);
Serial.println("Gunshot detected!");
// A small delay to avoid detecting the same clap multiple times in quick succession
delay(1000);
} else {
// Turn off the green LED
digitalWrite(greenLEDPin, HIGH);
digitalWrite(redLEDPin, LOW);
}
}
// Function to rotate the servo motor continuously
void rotateServoContinuously() {
for (int angle = 0; angle <= 180; angle += 10) {
servoMotor.write(angle);
delay(100); // Adjust the delay to control the servo speed
}
}
// Function to stop the servo motor
void stopServoRotation() {
for (int angle = 180; angle >= 0; angle -= 10) {
servoMotor.write(angle);
delay(100); // Adjust the delay to control the servo speed
}
}