A. Final Pseudocode

Properly formatted Pseudocode

  1. Program Starts
  2. Clear and Close everything %Start with a fresh workspace
  3. Create Global Variable a, and assign the arduino to it
  4. 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
  5. 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
  6. If the train’s position is not known then
  7. initialize t1 with tic % t1 = timer 1
  8. while the train’s position is not known % Sensors are unbroken
  9. Turn on crossing lights and lower crossing gate
  10. 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

  1. Read from the sensors
  2. Repeat from b.
  3. if the train breaks the departure gate sensor
  4. Turn off crossing lights and raise crossing gate
  5. Clear variables t1, b, c, Swi
  6. 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

  1. While loop true
  2. for loop to take sensor readings on arrival gate and save reading in variable sensor_Read
  3. if the sensor signal is broken and ag1 = 0 % first train is entering the urban area
  4. Set ag and ag1 to true (1)
  5. Set motorSpeed value to 170

III. Start first_Train_Timer

  1. otherwise if the sensor is unbroken and ag = 0 (train has broken the arrival gate sensor)
  2. ag = false (0)
  3. 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
  4. if first_Train_Timer exists (is not equal to 0)
  5. If at least Β½ a second has passed since first_Train_Timer started
  6. if the arrival gate sensor is broken and the ag = 0
  7. Set ag to true (1)
  8. Start the second_Train_Timer
  9. if the sensor is unbroken and ag is true (1)
  10. Set ag to false (0)
  11. create variable Swi % Swi = Switch lights control
  12. if the remainder of the integer Swi when dividing it by 2
  13. Turn on right crossing light and turn off left crossing light
  14. otherwise
  15. Turn on left crossing light and turn off right crossing light
  16. If at least 1 second has passed since the first_Train_Timer started
  17. lower the railroad crossing gate
  18. Otherwise if the second_Train_Timer exists (is not equal to 0)
  19. If at least Β½ a second has passed since second_Train_Timer
  20. create variable Swi % Swi = Switch lights control
  21. if the remainder of the integer Swi when dividing it by 2
  22. Turn on right crossing light and turn off left crossing light
  23. otherwise
  24. Turn on left crossing light and turn off right crossing light
  25. If at least 1 second has passed since the second_Train_Timer started
  26. lower the railroad crossing gate
  27. for loop to take sensor readings on departure gate and save reading in variable sensor_Read
  28. if the sensor signal is broken and dg = 0 % first train is exiting the urban area
  29. If the second_Train_Timer does not exist
  30. raise the crossing gate and turn off the crossing lights
  31. set the first_Train_Timer to false (0)
  32. reset ag1 to false (0) % indicates the first train is not on the urban side
  33. set dg to true (1) % ensures the departure gate broken sensor signal code is not run multiple times per crossing
  34. Otherwise if the first_Train_Timer exists
  35. set the first_Train_Timer to false (0)
  36. reset ag1 to false (0) % indicates the first train is not on the urban side
  37. set dg to true (1) % ensures the departure gate broken sensor signal code is not run multiple times per crossing
  38. Otherwise if the departure gate sensor signal is unbroken and dg = 1
  39. If the second_Train_Timer does not exist
  40. set dg to false (0) % ensures the unbroken signal code is run once per crossing
  41. set motorSpeed value to 255
  42. 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
  43. Otherwise % second train exists on the urban side
  44. set dg to false (0) % ensures the unbroken signal code is run once per crossing
  45. 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
  46. If first_Train_Timer and second_Train_Timer exist
  47. If the first_Train_Timer is less than the second_Train_Timer
  48. set second_Train_Timer to 0
  49. Repeat the loop