-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_code.ino
133 lines (114 loc) · 4.33 KB
/
sample_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
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
#include <Servo.h>
Servo myservo1;
Servo myservo2;
int pos = 0;
// Pin Definitions
const int IR1 = 3; // IR sensor for leaving train (Second Sensor)
const int IR2 = 2; // IR sensor for approaching train (First Sensor)
const int buzzerPin = 8; // Buzzer pin
const int redLED = 6; // Red LED pin (Gate closed)
const int greenLED = 7; // Green LED pin (Gate open)
int IR_Sensor=0; // for to check which is first sensor
bool isGateOpen = true; // Keep track of whether the gate is open
bool trainDetected = false; // Track if the train was detected by the first sensor
void setup() {
// Pin Modes
pinMode(IR1, INPUT); // IR sensor input (train leaving)
pinMode(IR2, INPUT); // IR sensor input (train approaching)
pinMode(buzzerPin, OUTPUT); // Buzzer pin as output
pinMode(redLED, OUTPUT); // Red LED pin as output
pinMode(greenLED, OUTPUT); // Green LED pin as output
// Attach servos
myservo1.attach(9); // Micro servo 1
myservo2.attach(10); // Micro servo 2
// Initialize gate to open
openGate(); // Initially open the gate
}
void loop() {
// If the first sensor (IR2) detects the train (LOW signal)
if(!isGateOpen){
digitalWrite(buzzerPin, HIGH); // Turn the buzzer ON
delay(20); // Wait for 1 second
digitalWrite(buzzerPin, LOW); // Turn the buzzer OFF
delay(200);
}
if(!trainDetected && digitalRead(IR2)==LOW){
IR_Sensor=1;
}
else if(!trainDetected && digitalRead(IR1)==LOW){
IR_Sensor=2;
}
if(IR_Sensor==1){
if (digitalRead(IR2) == LOW ||(digitalRead(IR2) == LOW && digitalRead(IR1) == LOW )) {
if (isGateOpen) { // If the gate is currently open, close it
closeGate();
trainDetected = true; // Train has been detected by the first sensor
alertTrainDetected(); // Sound buzzer when train is detected
}
}
else if (trainDetected && digitalRead(IR1) == LOW) { // If the second sensor detects the train has passed
if (!isGateOpen) { // If the gate is closed, open it
openGate();
trainDetected = false; // Reset train detection
delay(1000);
noTone(buzzerPin); // Turn off the buzzer when the train leaves
}
}
}
else if(IR_Sensor==2){
if (digitalRead(IR1) == LOW ||(digitalRead(IR1) == LOW && digitalRead(IR2) == LOW )) {
if (isGateOpen) { // If the gate is currently open, close it
closeGate();
trainDetected = true; // Train has been detected by the first sensor
alertTrainDetected(); // Sound buzzer when train is detected
}
}
else if (trainDetected && digitalRead(IR2) == LOW) { // If the second sensor detects the train has passed
if (!isGateOpen) { // If the gate is closed, open it
openGate();
trainDetected = false; // Reset train detection
delay(1000);
noTone(buzzerPin); // Turn off the buzzer when the train leaves
}
}
}
else{
IR_Sensor=0;
}
}
// Function to close the gate
void closeGate() {
tone(buzzerPin, 1000); // 1 kHz tone while closing gate
digitalWrite(redLED, HIGH); // Turn on red LED
digitalWrite(greenLED, LOW); // Turn off green LED
for (pos = 120; pos >= 0; pos -= 1) { // Decrement the angle
myservo1.write(pos);
myservo2.write(pos);
delay(2); // Adjusted for smoother motion
}
noTone(buzzerPin); // Stop the buzzer after gate is closed
isGateOpen = false; // Mark gate as closed
}
// Function to open the gate
void openGate() {
tone(buzzerPin, 2000); // 2 kHz tone while opening gate
digitalWrite(redLED, LOW); // Turn off red LED
digitalWrite(greenLED, HIGH); // Turn on green LED
for (pos = 0; pos <= 120; pos += 1) { // Increment the angle
myservo1.write(pos);
myservo2.write(pos);
// Adjust delay dynamically for smoother movement
if (pos >= 60)
delay(1); // Faster after halfway
else
delay(5); // Slower before halfway
}
noTone(buzzerPin); // Stop the buzzer after gate is opened
isGateOpen = true; // Mark gate as open
}
// Function to alert when a train is detected
void alertTrainDetected() {
tone(buzzerPin, 500); // 500 Hz tone for alert when train is detected
delay(200); // Keep the buzzer on for 200 ms
noTone(buzzerPin); // Turn off the buzzer after alert
}