-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
144 lines (134 loc) · 3.26 KB
/
main.c
File metadata and controls
144 lines (134 loc) · 3.26 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <Servo.h>
const int trigPin = 11;
const int echoPin = 12;
const int soundPin = A0;
const int ldrPin = 2;
const int ledPin = 3;
const int buzzerPin = 4;
const int buttonPin = 5;
Servo myServo;
long distance = 0;
long sound;
unsigned long startTime = 0;
const unsigned long WARNING_TIMEOUT = 300000;
bool isTimerActive = false;
const int LED_INTERVAL = 100;
const int MIN_FREQ = 500;
const int MAX_FREQ = 2000;
int freqStep = 100;
void setup()
{
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ldrPin, INPUT);
pinMode(soundPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
myServo.attach(13);
startTime = millis();
isTimerActive = true;
}
void loop()
{
for (int i = 15; i <= 115; i++)
{
checkTimer();
if (digitalRead(ldrPin) == HIGH)
moveServoCheck(i);
else
off();
}
for (int i = 115; i > 15; i--)
{
checkTimer();
if (digitalRead(ldrPin) == HIGH)
moveServoCheck(i);
else
off();
}
}
void checkTimer()
{
if (isTimerActive)
{
unsigned long currentTime = millis();
unsigned long elapsedTime = currentTime - startTime;
if (elapsedTime >= WARNING_TIMEOUT)
triggerTimeoutWarning(true);
}
}
void off()
{
Serial.println("OFF");
myServo.detach();
digitalWrite(ledPin, LOW);
noTone(buzzerPin);
}
void moveServoCheck(int angle)
{
myServo.attach(13);
myServo.write(angle);
delay(25);
distance = calculateDistance();
sound = analogRead(soundPin);
Serial.print("ON | Angle: ");
Serial.print(angle);
Serial.print(" | Distance: ");
Serial.print(distance);
Serial.print(" | Sound: ");
Serial.println(sound);
if ((distance <= 10 && distance > 0) || sound >= 1023)
triggerWarning(false);
}
int calculateDistance()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
return pulseIn(echoPin, HIGH) * 0.034 / 2;
}
void triggerWarning(bool timer)
{
myServo.detach();
if (timer)
Serial.println("TIMEOUT WARNING");
else
Serial.println("WARNING");
unsigned long lastLEDChange = 0;
unsigned long lastBuzzerChange = 0;
int currentFreq = MIN_FREQ;
bool ledState = false;
while (1)
{
unsigned long currentMillis = millis();
if (currentMillis - lastLEDChange >= LED_INTERVAL)
{
lastLEDChange = currentMillis;
ledState = !ledState;
digitalWrite(ledPin, ledState);
}
if (currentMillis - lastBuzzerChange >= 50)
{
lastBuzzerChange = currentMillis;
tone(buzzerPin, currentFreq);
if (timer)
currentFreq += freqStep * 2;
else
currentFreq += freqStep;
if (currentFreq >= MAX_FREQ || currentFreq <= MIN_FREQ)
freqStep = -freqStep;
}
if (digitalRead(buttonPin) == LOW)
{
if (timer)
startTime = millis();
break;
}
}
digitalWrite(ledPin, LOW);
noTone(buzzerPin);
}