- Program Starts
- Clear and Close everything %Start with a fresh workspace
- Create Global Variable a, and assign the arduino to it
- Attach the controls for the crossing gate, crossing lights, and lights above the arrival and departure gates to the arduino, train direction to forward (counter-clockwise) and motorSpeed to a value of 170 % all commands to control the lights and gate are done by calling the A_set.m function
- for loop to read the arrival and departure sensors and set their value to variables b and c respectively %b is the arrival gate sensor reading, c is the departure gate sensor reading
- If the trainβs position is not known then
- initialize t1 with tic % t1 = timer 1
- while the trainβs position is not known % Sensors are unbroken
- Turn on crossing lights and lower crossing gate
- Alternate crossing lights using variable Swi % Swi = Switch lights control
III. Call function A_set.m to control which light is on and which is off
- Read from the sensors
- Repeat from b.
- if the train breaks the departure gate sensor
- Turn off crossing lights and raise crossing gate
- Clear variables t1, b, c, Swi
- Create variables ag, dg, first_Train_Timer, second_Train_Timer, ag1
% ag = arrival gate, boolean used to enforce the code for the sensor signal is broken or unbroken is only run once per crossing at the arrival gate
% dg = departure gate, boolean used to enforce the code for the sensor signal is broken or unbroken is only run once per crossing at the arrival gate
% first_Train_Timer = Time since the first train has broken the signal on the arrival gate
% second_Train_Timer = Time since a second train has broken the signal on the arrival gate
% ag1 = arrival gate 1, used as boolean logic to determine if the first train has passed through the arrival gate, allows for a second train to pass without the causing problems in the code, resets on departure gate
- While loop true
- for loop to take sensor readings on arrival gate and save reading in variable sensor_Read
- if the sensor signal is broken and ag1 = 0 % first train is entering the urban area
- Set ag and ag1 to true (1)
- Set motorSpeed value to 170
III. Start first_Train_Timer
- otherwise if the sensor is unbroken and ag = 0 (train has broken the arrival gate sensor)
- ag = false (0)
- Call A_set to change the arrival gate lights to red off and green on and change the departure gate lights to red off and green on
- if first_Train_Timer exists (is not equal to 0)
- If at least Β½ a second has passed since first_Train_Timer started
- if the arrival gate sensor is broken and the ag = 0
- Set ag to true (1)
- Start the second_Train_Timer
- if the sensor is unbroken and ag is true (1)
- Set ag to false (0)
- create variable Swi % Swi = Switch lights control
- if the remainder of the integer Swi when dividing it by 2
- Turn on right crossing light and turn off left crossing light
- otherwise
- Turn on left crossing light and turn off right crossing light
- If at least 1 second has passed since the first_Train_Timer started
- lower the railroad crossing gate
- Otherwise if the second_Train_Timer exists (is not equal to 0)
- If at least Β½ a second has passed since second_Train_Timer
- create variable Swi % Swi = Switch lights control
- if the remainder of the integer Swi when dividing it by 2
- Turn on right crossing light and turn off left crossing light
- otherwise
- Turn on left crossing light and turn off right crossing light
- If at least 1 second has passed since the second_Train_Timer started
- lower the railroad crossing gate
- for loop to take sensor readings on departure gate and save reading in variable sensor_Read
- if the sensor signal is broken and dg = 0 % first train is exiting the urban area
- If the second_Train_Timer does not exist
- raise the crossing gate and turn off the crossing lights
- set the first_Train_Timer to false (0)
- reset ag1 to false (0) % indicates the first train is not on the urban side
- set dg to true (1) % ensures the departure gate broken sensor signal code is not run multiple times per crossing
- Otherwise if the first_Train_Timer exists
- set the first_Train_Timer to false (0)
- reset ag1 to false (0) % indicates the first train is not on the urban side
- set dg to true (1) % ensures the departure gate broken sensor signal code is not run multiple times per crossing
- Otherwise if the departure gate sensor signal is unbroken and dg = 1
- If the second_Train_Timer does not exist
- set dg to false (0) % ensures the unbroken signal code is run once per crossing
- set motorSpeed value to 255
- change the lights above the arrival gate to green light on, red light off and change the lights above the departure gate to green light off, red light on
- Otherwise % second train exists on the urban side
- set dg to false (0) % ensures the unbroken signal code is run once per crossing
- change the lights above the arrival gate to green light on, red light off and change the lights above the departure gate to green light off, red light on
- If first_Train_Timer and second_Train_Timer exist
- If the first_Train_Timer is less than the second_Train_Timer
- set second_Train_Timer to 0
- Repeat the loop