#include <Servo.h>
const int RED_PIN = 13; //set a button pin for the red flashing led
const int PUSH_BUTTON = 4; //set a button statet when thebutton gets activated
const int TRIG = 6; //..We are setting the TRIG on the ultrasonic sensor to port 23
const int ECHO = 5; //set a button state for the echo pin
long previousMillis = 0; //start timer to activate the ultrasonic motion sensor starts from 0msec
void setup() {
// put your setup code here, to run once:
pinMode(RED_PIN, OUTPUT);
// initialize the LED pins as an output:
pinMode(TRIG, OUTPUT); // set the TRIG pin of the ultrasonic motion sensor as output
pinMode(ECHO, INPUT); // set the ECHO pin as input to the ultrasonic motion sensor
Serial.begin(9600); //serial.bein to start it for 9600msec
Serial.println("Train Crossing Active!"); //to idicate that the train is crossing
}
//define a function that is called get distance gets distance of ultrasonic wave that is sent
long getDistance()// #define a function that is called get distanc
{
digitalWrite(TRIG, HIGH); // Start the ultrasonic pulse
delayMicroseconds(10); // Send a 10-microsecond pulse
digitalWrite(TRIG, LOW); // Stop the pulse
unsigned long startTime = 0; //start timer for the ultrasonic motion sensor
unsigned long endTime = 0; //end timmer for the ultrasonic motion sensor
// Wait for ECHO to go HIGH and record the start time
unsigned long timeout = millis(); //the amount of time needthe timmer to timeout for
while (digitalRead(ECHO) == LOW) { //while the digital read pin for the echo is pressed
startTime = micros(); //start the timer and set it to micros
if (millis() - timeout > 50) return -1; // Exit if timeout (no response) if millis() - timeout >50ms, timer is out of bound than this is invalid
}
// Wait for ECHO to go LOW and record the end time
timeout = millis();
while (digitalRead(ECHO) == HIGH) {
endTime = micros();
if (millis() - timeout > 50) return -1; // Exit if timeout
}
// Calculate the duration in microseconds
long duration = endTime - startTime;
// Convert duration to distance in centimeters
long distance_cm = duration / 58; // 58 us per cm for sound in air
return distance_cm; //return the distance in mm from the ulltrasonic motion sensor
}
//function that turns on the led whenit is callled
void red_light()
{
digitalWrite(RED_PIN, HIGH); // Turn on red light
}
//function that turns off the led when it is called
void no_light()
{
digitalWrite(RED_PIN, LOW);
}
//set the button state to false to indicate that the button has not been activated yet
bool buttonPressed = false;
// delay(flashDelay)
void loop()
{
if (digitalRead(PUSH_BUTTON) == HIGH && !buttonPressed) {
buttonPressed = true; // Track that the button was pressed
}
if(buttonPressed)
{
// put your main code here, to run repeatedly:
long distance = getDistance();
// start the crossing guard actions
if(distance >=90)
{
no_light();
//noTone(BUZZER_BUTTON);
}
else if(distance > 40 && distance < 90)
{
//greenLight();
//FlashLEDs(2, 200, false);
//CrossingGuardsUp();
//noTone(BUZZER_BUTTON);
}
else if(distance > 16 && distance < 40)
{
// flash the LEDs x number of times for YY milliseconds
//yellow_light();
//noTone(BUZZER_BUTTON);
}
else if(distance <=15)
{
red_light();
//FlashLEDs(2, 200, true);
//CrossingGuardsDown();
// flash the LEDs x number of times for YY milliseconds
/*if(millis() - previousMillis > interval)
{
tone(BUZZER_BUTTON, 500);
previousMillis = millis(); // Reset the timer
}
*/
}
// Check if the buzzer should be turned off
}
}