kartonman

arduino level crossing with two gates

Sep 9th, 2021 (edited)
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. /* Level Crossing With Two Gates
  2. By Gary Granai steamtraininfo.com 2020 (This is a modified version of Version 1.4 Geoff Bunza 2018 )
  3. */
  4. #include <Servo.h>
  5. Servo gate1servo; // create servo object to control crossing gate 1
  6. //ASSUMPTION Gate is down at position 30 and up at position 120
  7. Servo gate2servo; // create servo object to control crossing gate 2
  8. //ASSUMPTION Gate is down at position 30 and up at position 120
  9. int gate1flashers = 2; /*connect 3 Leds to the gate flasher pins. be sure that the total current drawn by the Leds is less than the 40 ma Arduino pin load limit. If you don't want gate flashing lights, there is no need to change the code. Just don't add the lights.*/
  10. int gate2flashers = 3;
  11. int alarmbells = 4; /* This is an optional connection to an alarm bell module. If you don't want the bells, there is no need to change the code. Just don't add the the module.*/
  12. int sensor1 = 5; // IR sensor pin Assumption== Pin goes LOW when train detected
  13. int sensor2 = 6; // IR sensor pin Assumption== Pin goes LOW when train detected
  14. int led1 = 10; // Led 1 pin first alternating flasher
  15. int led2 = 11; // Led 2 pin first alternating flasher
  16. int led3 = 12; // Led 3 pin second alternating flasher
  17. int led4 = 13; // Led 4 pin second alternating flasher
  18. int gatelow = 30; // variable to store the servo low gate stop position
  19. int gatehigh = 120; // variable to store the servo high gate stop position
  20. int gateposition = 120; // variable to store the servo gateposition
  21. int entering_sensor = 5; //this will tell which sensor was triggered first
  22. int leaving_sensor = 6; // this will tell which sensor shows train leaving
  23. int gates_started = 0; // this says if the crossing is active
  24. int flash_state = 0;
  25. long flash_time = 0;
  26. long flash_interval = 900; // time in milliseconds between alternating flashes
  27. int sensor_count = 0;
  28. void setup()
  29. {
  30. gate1servo.attach(3); // attaches the servo on pin 3 to the servo object
  31. gate2servo.attach(4); // attaches the servo on pin 4 to the servo object
  32. gate1servo.write(gateposition); //start assuming no train
  33. gate2servo.write(gateposition); //start assuming no train
  34. pinMode(sensor1, INPUT);
  35. pinMode(sensor2, INPUT);
  36. pinMode(led1, OUTPUT);
  37. pinMode(led2, OUTPUT);
  38. pinMode(led3, OUTPUT);
  39. pinMode(led4, OUTPUT);
  40. digitalWrite(led1, LOW); // Start with all flashers off
  41. digitalWrite(led2, LOW);
  42. digitalWrite(led3, LOW);
  43. digitalWrite(gateflashers1, LOW);
  44. digitalWrite(gateflashers2, LOW);
  45. digitalWrite(alarmbells, LOW);
  46.  
  47. flash_time = millis();
  48. }
  49. void loop()
  50. {
  51. if ((digitalRead (sensor1)==LOW)&& (gates_started==0)) {
  52. gates_started = 1;
  53. leaving_sensor = sensor2;
  54. starting_sequence();
  55. }
  56. if ((digitalRead (sensor2)==LOW)&& (gates_started==0)) {
  57. gates_started = 1;
  58. leaving_sensor = sensor1;
  59. starting_sequence();
  60. }
  61. if (gates_started) flash_leds(); //gates are down continue flashing
  62. if ((digitalRead(leaving_sensor)==LOW)&&(gates_started==1)) { //train is starting to leave
  63.  
  64. //as long as the leaving sensor is active the train is still in the crossing
  65. while (gates_started==1) { //now check if train is REALLY gone
  66. sensor_count = 0;
  67. for (int i=1; i<40; i++) {
  68. if (digitalRead(leaving_sensor)==LOW) sensor_count++;
  69. delay (30);
  70. flash_leds();
  71. }
  72. if (sensor_count==0) gates_started=0;
  73. flash_leds();
  74. }
  75. // we only get here if the train has really left the crossing
  76. ending_sequence();
  77. }
  78. }
  79. void starting_sequence() {
  80. long wait_time;
  81. flash_time = millis();
  82. wait_time = millis()+3000;
  83. while (wait_time > millis()) flash_leds(); //flash before dropping gates
  84. for(gateposition = gatehigh; gateposition>gatelow; gateposition-=1) // goes from gatehigh degrees to gatelow degrees
  85. {
  86. gate1servo.write(gateposition); // tell servo to go to gateposition in variable ‘gateposition’
  87. gate2servo.write(gateposition); // tell servo to go to gateposition in variable ‘gateposition’
  88. flash_leds(); // keep flashing leds
  89. delay(40); // waits 40ms to slow servo
  90. }
  91. }
  92. void ending_sequence() {
  93. for(gateposition = gatelow; gateposition<gatehigh; gateposition++) // goes from gatelow degrees to gatehigh degrees
  94. {
  95. gate1servo.write(gateposition); // tell servo to go to gateposition in variable ‘gateposition’
  96. gate2servo.write(gateposition); // tell servo to go to gateposition in variable ‘gateposition’
  97. flash_leds(); // keep flashing leds
  98. delay(40); // waits 40ms to slow servo
  99. }
  100. digitalWrite(led1, LOW); // flashers completely off
  101. digitalWrite(led3, LOW);
  102. digitalWrite(led2, LOW);
  103. digitalWrite(led4, LOW);
  104. digitalWrite(gateflashers1, LOW);
  105. digitalWrite(gateflashers2, LOW);
  106. digitalWrite(alarmbells, LOW);
  107. delay(30000); // 30 second delay to account for the train passing the starting entry sensor
  108. }
  109. void flash_leds() {
  110. if (flash_time > millis()) return;
  111. flash_state = ~flash_state;
  112. digitalWrite(led1, flash_state); // Alternate flashers
  113. digitalWrite(led3, flash_state);
  114. digitalWrite(led2, ~flash_state);
  115. digitalWrite(led4, ~flash_state);
  116. flash_time = millis()+flash_interval;
  117. }
Add Comment
Please, Sign In to add comment