const int GREEN_PIN = 11;
const int YELLOW_PIN = 12;
const int RED_PIN = 13;
const int BUZZER_BUTTON = 10;
const int PUSH_BUTTON = 4;
const int TRIG = 6; //..We are setting the TRIG on the ultrasonic sensor to port 23
const int ECHO = 5;
long previousMillis = 0;
long interval = 500; // First length of time I want the buzzer to stay on set tone
void setup() {
// put your setup code here, to run once:
pinMode(GREEN_PIN, OUTPUT);
pinMode(YELLOW_PIN, OUTPUT);
pinMode(RED_PIN, OUTPUT);
pinMode(BUZZER_BUTTON, OUTPUT);
pinMode(TRIG, OUTPUT); // TRIG pin as output
pinMode(ECHO, INPUT); // ECHO pin as input
Serial.begin(9600);
Serial.println("Train Crossing Active!");
}
//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;
unsigned long endTime = 0;
// Wait for ECHO to go HIGH and record the start time
unsigned long timeout = millis();
while (digitalRead(ECHO) == LOW) {
startTime = micros();
if (millis() - timeout > 50) return -1; // Exit if timeout (no response)
}
// 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;
}
//This is what the output should be when the function green light is to be run
void greenLight()
{
digitalWrite(GREEN_PIN, HIGH); //turn will turn on green light
digitalWrite(YELLOW_PIN, LOW); //turn the yellow led off
digitalWrite(RED_PIN, LOW); //turn the red led off
}
void yellow_light()
{
digitalWrite(GREEN_PIN, LOW); // Turn off green light
digitalWrite(YELLOW_PIN, HIGH); // Turn on yellow light
digitalWrite(RED_PIN, LOW); // Turn off red light
}
void red_light()
{
digitalWrite(GREEN_PIN, LOW); // Turn off green light
digitalWrite(YELLOW_PIN, LOW); // Turn off yellow light
digitalWrite(RED_PIN, HIGH); // Turn on red light
}
void no_light()
{
// Ensure all lights and buzzer are off
digitalWrite(GREEN_PIN, LOW);
digitalWrite(YELLOW_PIN, LOW);
digitalWrite(RED_PIN, LOW);
}
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 > 23 && distance < 90)
{
greenLight();
noTone(BUZZER_BUTTON);
}
else if(distance > 16 && distance < 22)
{
yellow_light();
noTone(BUZZER_BUTTON);
}
else if(distance <=15)
{
red_light();
if(millis() - previousMillis > interval)
{
tone(BUZZER_BUTTON, 500);
previousMillis = millis(); // Reset the timer
}
}
// Check if the buzzer should be turned off
}
}