Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Grade Crossing LED's triggered by IR Sensors
- Adapted from Geoff Bunza's sketch Not using the Servo actuated Gate Arms
- Modified by Gary Granai 25 September, 2020
- https://steamtraininfo.com/arduino-projects
- */
- int sensor1 = 5; // IR sensor==Pin goes LOW when train detected
- int sensor2 = 6; // IR sensor==Pin goes LOW when train detected
- int led1 = 10; // Led 1 pin alternating flasher
- int led2 = 11; // Led 2 pin alternating flasher
- int led3 = 14; // Led 3 pin alternating flasher
- int led4 = 15; // Led 4 pin alternating flasher
- int entering_sensor = 5; // which sensor was triggered first
- int leaving_sensor = 6; // which sensor shows train leaving
- int sequence_started = 0; // this says if the crossing is active
- int flash_state = 0; // variable for function flash_leds()
- long flash_time = 0; // variable for function flash_leds()
- long flash_interval = 900; // time in milliseconds between alternating flashes
- int sensor_count = 0; // declare variable for while statement
- void setup() {
- pinMode(sensor1, INPUT); // sensor1 is an INPUT
- pinMode(sensor2, INPUT); // sensor2 is an INPUT
- pinMode(led1, OUTPUT); // led1 on pin 10 is an OUTPUT
- pinMode(led2, OUTPUT); // led2 on pin 11 is an OUTPUT
- pinMode(led3, OUTPUT); // led3 on pin 14 is an OUTPUT
- pinMode(led4, OUTPUT); // led4 on pin 15 is an OUTPUT
- digitalWrite(led1, LOW); // Start with flashers off
- digitalWrite(led2, LOW); // Start with flashers off
- digitalWrite(led3, LOW); // Start with flashers off
- digitalWrite(led4, LOW); // Start with flashers off
- flash_time = millis(); // for function flash_leds()
- }
- void loop() {
- // The two if statements defines which sensor is which and
- // changes sequesnc_started from FALSE to TRUE then starts
- // the leds flashing
- if ((digitalRead (sensor1) == LOW) && (sequence_started == 0)) {
- sequence_started = 1;
- leaving_sensor = sensor2;
- flash_leds();
- }
- if ((digitalRead (sensor2) == LOW) && (sequence_started == 0)) {
- sequence_started = 1;
- leaving_sensor = sensor1;
- flash_leds();
- }
- // First if== sequence started and continue flashing leds
- // Next if== train is leaving the crossing
- if (sequence_started) flash_leds();
- if ((digitalRead(leaving_sensor) == LOW) && (sequence_started == 1)) {
- // as long as the leaving sensor is active the train is still in the crossing
- // while checks if train is gone yet
- while (sequence_started == 1) {
- sensor_count = 0;
- for (int i = 1; i < 40; i++) {
- if (digitalRead(leaving_sensor) == LOW) sensor_count++;
- delay (30);
- flash_leds();
- }
- if (sensor_count == 0) sequence_started = 0;
- flash_leds();
- }
- // At this point train is no longer over the leaving sensor
- digitalWrite(led1, LOW); // Turn OFF led
- digitalWrite(led2, LOW); // Turn OFF led
- digitalWrite(led3, LOW); // Turn OFF led
- digitalWrite(led4, LOW); // Turn OFF led
- }
- }
- void flash_leds() {
- if (flash_time > millis()) return;
- flash_state = ~flash_state;
- digitalWrite(led1, flash_state);
- digitalWrite(led2, ~flash_state);
- digitalWrite(led3, flash_state);
- digitalWrite(led4, ~flash_state);
- flash_time = millis() + flash_interval;
- }
Advertisement
Add Comment
Please, Sign In to add comment