kartonman

level crossing lghts only Bunza modified

Sep 27th, 2022
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | Source Code | 0 0
  1.  
  2.  
  3.  
  4. /* Grade Crossing LED's triggered by IR Sensors
  5. Adapted from Geoff Bunza's sketch Not using the Servo actuated Gate Arms
  6. Modified by Gary Granai 25 September, 2020
  7. https://steamtraininfo.com/arduino-projects
  8.  
  9. */
  10. int sensor1 = 5; // IR sensor==Pin goes LOW when train detected
  11. int sensor2 = 6; // IR sensor==Pin goes LOW when train detected
  12. int led1 = 10; // Led 1 pin alternating flasher
  13. int led2 = 11; // Led 2 pin alternating flasher
  14. int led3 = 14; // Led 3 pin alternating flasher
  15. int led4 = 15; // Led 4 pin alternating flasher
  16. int entering_sensor = 5; // which sensor was triggered first
  17. int leaving_sensor = 6; // which sensor shows train leaving
  18. int sequence_started = 0; // this says if the crossing is active
  19. int flash_state = 0; // variable for function flash_leds()
  20. long flash_time = 0; // variable for function flash_leds()
  21. long flash_interval = 900; // time in milliseconds between alternating flashes
  22. int sensor_count = 0; // declare variable for while statement
  23.  
  24. void setup() {
  25.  
  26. pinMode(sensor1, INPUT); // sensor1 is an INPUT
  27. pinMode(sensor2, INPUT); // sensor2 is an INPUT
  28. pinMode(led1, OUTPUT); // led1 on pin 10 is an OUTPUT
  29. pinMode(led2, OUTPUT); // led2 on pin 11 is an OUTPUT
  30. pinMode(led3, OUTPUT); // led3 on pin 14 is an OUTPUT
  31. pinMode(led4, OUTPUT); // led4 on pin 15 is an OUTPUT
  32. digitalWrite(led1, LOW); // Start with flashers off
  33. digitalWrite(led2, LOW); // Start with flashers off
  34. digitalWrite(led3, LOW); // Start with flashers off
  35. digitalWrite(led4, LOW); // Start with flashers off
  36. flash_time = millis(); // for function flash_leds()
  37. }
  38.  
  39. void loop() {
  40. // The two if statements defines which sensor is which and
  41. // changes sequesnc_started from FALSE to TRUE then starts
  42. // the leds flashing
  43. if ((digitalRead (sensor1) == LOW) && (sequence_started == 0)) {
  44. sequence_started = 1;
  45. leaving_sensor = sensor2;
  46. flash_leds();
  47. }
  48. if ((digitalRead (sensor2) == LOW) && (sequence_started == 0)) {
  49. sequence_started = 1;
  50. leaving_sensor = sensor1;
  51. flash_leds();
  52. }
  53. // First if== sequence started and continue flashing leds
  54. // Next if== train is leaving the crossing
  55. if (sequence_started) flash_leds();
  56. if ((digitalRead(leaving_sensor) == LOW) && (sequence_started == 1)) {
  57.  
  58. // as long as the leaving sensor is active the train is still in the crossing
  59. // while checks if train is gone yet
  60. while (sequence_started == 1) {
  61. sensor_count = 0;
  62. for (int i = 1; i < 40; i++) {
  63. if (digitalRead(leaving_sensor) == LOW) sensor_count++;
  64. delay (30);
  65. flash_leds();
  66. }
  67. if (sensor_count == 0) sequence_started = 0;
  68. flash_leds();
  69. }
  70. // At this point train is no longer over the leaving sensor
  71. digitalWrite(led1, LOW); // Turn OFF led
  72. digitalWrite(led2, LOW); // Turn OFF led
  73. digitalWrite(led3, LOW); // Turn OFF led
  74. digitalWrite(led4, LOW); // Turn OFF led
  75. }
  76. }
  77.  
  78. void flash_leds() {
  79. if (flash_time > millis()) return;
  80. flash_state = ~flash_state;
  81. digitalWrite(led1, flash_state);
  82. digitalWrite(led2, ~flash_state);
  83. digitalWrite(led3, flash_state);
  84. digitalWrite(led4, ~flash_state);
  85. flash_time = millis() + flash_interval;
  86. }
  87.  
  88.  
Advertisement
Add Comment
Please, Sign In to add comment