Simulating Railroad Crossing Lights
Everyone has seen a railway crossing before, and if you're a railfan you've probably spent more than a few hours stuck behind them waiting for their infernal blink-blink-blink to stop so you can continue chasing your train!
How do you make your model crossing blink like that though? The simple answer would be a 555 timer in astable mode with some set and reset triggers. But that would be easy, and when you're an software engineer everything looks like a software problem. So instead, we attack the problem with a sledgehammer and use an Arduino.
Kidding aside, there are very valid reasons why you might want to use an Arduino for such a simple problem. Suppose you're using the excellent Arduino CMRI library to connect your layout to JMRI, and you have some infra red train detectors (coming soon from the Utrainia Electrik Company) wired up. It would then be very easy to get JMRI to set an output bit whenever a train is detected near the crossing; then in your Arduino you can flash the lights as appropriate.
So to turn this into a practical example, I decided to write a small library to achieve just this. Enter: CrossingFlasher! This small Arduino library exposes just three small methods to let you start, stop, and update your crossing lights. It handles all the timing for you, and flashes them at the correct 3Hz flashing rate that we use here in NZ.
And how would one use this on in Arduino C/MRI?
#include <CMRI.h> #include <CrossingFlasher.h> CMRI cmri; CrossingFlasher bucks(2, 3); // crossbucks on pins 2 and 3 void setup() { Serial.begin(9600); } void loop() { // 1: process incoming CMRI data cmri.process(); // 2: update output. Reads bit 0 of T packet and sets the LED to this if (cmri.get_bit(0) == true) bucks.on(); else bucks.off(); // 3: update cross bucks bucks.update(); }
Pretty simple huh? We've just wired up a set of crossbucks to pins 2 and 3 on our Arduino, and told them to flash whenever we set output bit 0 in JMRI (System Name: ML1). Using some Logix rules in JMRI it would be easy to trigger this from a block occupancy detector, a push button, or a broken-beam type detector. Piece of cake. A bit more work and you could even play the crossing sounds through JMRI.
If you need to run more than one pair of crossbucks, just connect multiple LEDs to each Arduino output pin.
Comments